| 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.cpp |
| 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 | #include "chuck_emit.h" |
| 35 | #include "chuck_vm.h" |
| 36 | #include "chuck_errmsg.h" |
| 37 | #include "chuck_instr.h" |
| 38 | #include "chuck_globals.h" // added 1.4.1.0 |
| 39 | #include "chuck_parse.h" // added 1.5.0.0 |
| 40 | #include "util_string.h" // added 1.5.0.5 |
| 41 | #include <sstream> |
| 42 | #include <iostream> |
| 43 | using namespace std; |
| 44 | |
| 45 | |
| 46 | // forward references |
| 47 | struct Chuck_FuncCall_Options; |
| 48 | |
| 49 | |
| 50 | //----------------------------------------------------------------------------- |
| 51 | // function prototypes |
| 52 | //----------------------------------------------------------------------------- |
| 53 | t_CKBOOL emit_engine_emit_stmt_list( Chuck_Emitter * emit, a_Stmt_List list ); |
| 54 | t_CKBOOL emit_engine_emit_stmt( Chuck_Emitter * emit, a_Stmt stmt, t_CKBOOL pop = TRUE ); |
| 55 | t_CKBOOL emit_engine_emit_if( Chuck_Emitter * emit, a_Stmt_If stmt ); |
| 56 | t_CKBOOL emit_engine_emit_for( Chuck_Emitter * emit, a_Stmt_For stmt ); |
| 57 | t_CKBOOL emit_engine_emit_foreach( Chuck_Emitter * emit, a_Stmt_ForEach stmt ); |
| 58 | t_CKBOOL emit_engine_emit_while( Chuck_Emitter * emit, a_Stmt_While stmt ); |
| 59 | t_CKBOOL emit_engine_emit_do_while( Chuck_Emitter * emit, a_Stmt_While stmt ); |
| 60 | t_CKBOOL emit_engine_emit_until( Chuck_Emitter * emit, a_Stmt_Until stmt ); |
| 61 | t_CKBOOL emit_engine_emit_do_until( Chuck_Emitter * emit, a_Stmt_Until stmt ); |
| 62 | t_CKBOOL emit_engine_emit_loop( Chuck_Emitter * emit, a_Stmt_Loop stmt ); |
| 63 | t_CKBOOL emit_engine_emit_break( Chuck_Emitter * emit, a_Stmt_Break br ); |
| 64 | t_CKBOOL emit_engine_emit_continue( Chuck_Emitter * emit, a_Stmt_Continue cont ); |
| 65 | t_CKBOOL emit_engine_emit_return( Chuck_Emitter * emit, a_Stmt_Return stmt ); |
| 66 | t_CKBOOL emit_engine_emit_switch( Chuck_Emitter * emit, a_Stmt_Switch stmt ); |
| 67 | t_CKBOOL emit_engine_emit_exp( Chuck_Emitter * emit, a_Exp exp, t_CKBOOL doAddRef = FALSE, a_Stmt enclosingStmt = NULL ); |
| 68 | t_CKBOOL emit_engine_emit_exp_binary( Chuck_Emitter * emit, a_Exp_Binary binary ); |
| 69 | t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary ); |
| 70 | t_CKBOOL emit_engine_emit_op_chuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary ); |
| 71 | t_CKBOOL emit_engine_emit_op_unchuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs ); |
| 72 | t_CKBOOL emit_engine_emit_op_upchuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs ); |
| 73 | t_CKBOOL emit_engine_emit_op_at_chuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs ); |
| 74 | t_CKBOOL emit_engine_emit_op_overload_binary( Chuck_Emitter * emit, a_Exp_Binary binary ); // 1.5.1.5 |
| 75 | t_CKBOOL emit_engine_emit_op_overload_unary( Chuck_Emitter * emit, a_Exp_Unary unary ); // 1.5.1.5 |
| 76 | t_CKBOOL emit_engine_emit_op_overload_postfix( Chuck_Emitter * emit, a_Exp_Postfix postfix ); // 1.5.1.5 |
| 77 | t_CKBOOL emit_engine_emit_exp_unary( Chuck_Emitter * emit, a_Exp_Unary unary ); |
| 78 | t_CKBOOL emit_engine_emit_exp_primary( Chuck_Emitter * emit, a_Exp_Primary exp ); |
| 79 | t_CKBOOL emit_engine_emit_exp_cast( Chuck_Emitter * emit, a_Exp_Cast cast ); |
| 80 | t_CKBOOL emit_engine_emit_exp_postfix( Chuck_Emitter * emit, a_Exp_Postfix postfix ); |
| 81 | t_CKBOOL emit_engine_emit_exp_dur( Chuck_Emitter * emit, a_Exp_Dur dur ); |
| 82 | t_CKBOOL emit_engine_emit_exp_array( Chuck_Emitter * emit, a_Exp_Array array ); |
| 83 | t_CKBOOL emit_engine_emit_exp_func_call( Chuck_Emitter * emit, Chuck_Func * func, |
| 84 | Chuck_Type * type, t_CKUINT line, t_CKUINT where, |
| 85 | t_CKBOOL spork = FALSE, |
| 86 | Chuck_FuncCall_Options * options = NULL ); |
| 87 | t_CKBOOL emit_engine_emit_exp_func_call( Chuck_Emitter * emit, a_Exp_Func_Call func_call, |
| 88 | t_CKBOOL spork = FALSE ); |
| 89 | t_CKBOOL emit_engine_emit_func_args( Chuck_Emitter * emit, a_Exp_Func_Call func_call ); |
| 90 | t_CKBOOL emit_engine_emit_exp_dot_member( Chuck_Emitter * emit, a_Exp_Dot_Member member ); |
| 91 | t_CKBOOL emit_engine_emit_exp_if( Chuck_Emitter * emit, a_Exp_If exp_if ); |
| 92 | t_CKBOOL emit_engine_emit_exp_decl( Chuck_Emitter * emit, a_Exp_Decl decl, t_CKBOOL first_exp ); |
| 93 | t_CKBOOL emit_engine_emit_array_lit( Chuck_Emitter * emit, a_Array_Sub array ); |
| 94 | t_CKBOOL emit_engine_emit_complex_lit( Chuck_Emitter * emit, a_Complex val ); |
| 95 | t_CKBOOL emit_engine_emit_polar_lit( Chuck_Emitter * emit, a_Polar val ); |
| 96 | t_CKBOOL emit_engine_emit_vec_lit( Chuck_Emitter * emit, a_Vec val ); |
| 97 | t_CKBOOL emit_engine_emit_code_segment( Chuck_Emitter * emit, a_Stmt_Code stmt, |
| 98 | t_CKBOOL push = TRUE ); |
| 99 | t_CKBOOL emit_engine_emit_func_def( Chuck_Emitter * emit, a_Func_Def func_def ); |
| 100 | t_CKBOOL emit_engine_emit_class_def( Chuck_Emitter * emit, a_Class_Def class_def ); |
| 101 | t_CKBOOL emit_engine_pre_constructor( Chuck_Emitter * emit, Chuck_Type * type, a_Ctor_Call ctor_info ); |
| 102 | t_CKBOOL emit_engine_instantiate_object( Chuck_Emitter * emit, Chuck_Type * type, |
| 103 | a_Ctor_Call ctor_info, a_Array_Sub array, t_CKBOOL is_ref, |
| 104 | t_CKBOOL is_array_ref ); |
| 105 | t_CKBOOL emit_engine_emit_spork( Chuck_Emitter * emit, a_Exp_Func_Call exp ); |
| 106 | t_CKBOOL emit_engine_emit_cast( Chuck_Emitter * emit, Chuck_Type * to, Chuck_Type * from, uint32_t where ); |
| 107 | t_CKBOOL emit_engine_emit_symbol( Chuck_Emitter * emit, S_Symbol symbol, |
| 108 | Chuck_Value * v, t_CKBOOL emit_var, |
| 109 | t_CKUINT line, t_CKUINT where, a_Exp_Primary exp ); |
| 110 | Chuck_Instr_Stmt_Start * emit_engine_track_stmt_refs_start( Chuck_Emitter * emit, a_Stmt stmt ); |
| 111 | void emit_engine_track_stmt_refs_cleanup( Chuck_Emitter * emit, Chuck_Instr_Stmt_Start * start ); |
| 112 | // disabled until further notice (added 1.3.0.0) |
| 113 | // t_CKBOOL emit_engine_emit_spork( Chuck_Emitter * emit, a_Stmt stmt ); |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | //----------------------------------------------------------------------------- |
| 119 | // name: emit_engine_init() |
| 120 | // desc: ... |
| 121 | //----------------------------------------------------------------------------- |
| 122 | Chuck_Emitter * emit_engine_init( Chuck_Env * env ) |
| 123 | { |
| 124 | // log |
| 125 | EM_log( CK_LOG_HERALD, "initializing emitter..." ); |
| 126 | |
| 127 | // TODO: ensure this in a better way? |
| 128 | // whatever t_CKUINT is defined as, it must be the same size as a pointer |
| 129 | // system architecture can vary (e.g., 32-bit vs 64-bit) but this condition |
| 130 | // is assumed to be true in the virtual machine |
| 131 | assert( sizeof(t_CKUINT) == sizeof(void *) ); |
| 132 | |
| 133 | // allocate new emit |
| 134 | Chuck_Emitter * emit = new Chuck_Emitter; |
| 135 | // set the reference |
| 136 | emit->env = env; |
| 137 | // add ref |
| 138 | CK_SAFE_ADD_REF( emit->env ); |
| 139 | |
| 140 | return emit; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | //----------------------------------------------------------------------------- |
| 147 | // name: emit_engine_shutdown() |
| 148 | // desc: ... |
| 149 | //----------------------------------------------------------------------------- |
| 150 | t_CKBOOL emit_engine_shutdown( Chuck_Emitter *& emit ) |
| 151 | { |
| 152 | // check |
| 153 | if( !emit ) return FALSE; |
| 154 | |
| 155 | // log |
| 156 | EM_log( CK_LOG_SYSTEM, "shutting down emitter..." ); |
| 157 | // shut it down; this is system cleanup -- delete instead of release |
| 158 | CK_SAFE_DELETE( emit ); |
| 159 | |
| 160 | return TRUE; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | //----------------------------------------------------------------------------- |
| 167 | // name: emit_engine_emit_prog() |
| 168 | // desc: emit a chuck program/AST into VM bytecode |
| 169 | //----------------------------------------------------------------------------- |
| 170 | Chuck_VM_Code * emit_engine_emit_prog( Chuck_Emitter * emit, a_Program prog, |
| 171 | te_HowMuch how_much ) |
| 172 | { |
| 173 | // make sure the code is NULL |
| 174 | assert( emit->code == NULL ); |
| 175 | // make sure the stack is empty |
| 176 | assert( emit->code_stack.size() == 0 ); |
| 177 | // make sure there is a context to emit |
| 178 | assert( emit->env->context != NULL ); |
| 179 | // make sure no code |
| 180 | assert( emit->env->context->nspc->pre_ctor == NULL ); |
| 181 | |
| 182 | // log |
| 183 | EM_log( CK_LOG_FINER, "(pass 4) emitting VM code..." ); |
| 184 | // emit->env->context->filename.c_str() ); |
| 185 | // push indent |
| 186 | EM_pushlog(); |
| 187 | // log how much |
| 188 | EM_log( CK_LOG_FINEST, "target: %s", howmuch2str( how_much ) ); |
| 189 | |
| 190 | // return |
| 191 | t_CKBOOL ret = TRUE; |
| 192 | // allocate the code |
| 193 | emit->code = new Chuck_Code; |
| 194 | // set the current context |
| 195 | emit->context = emit->env->context; |
| 196 | // set the namespace |
| 197 | emit->nspc = emit->context->nspc; |
| 198 | // reset the func |
| 199 | emit->func = NULL; |
| 200 | // clear the code stack |
| 201 | emit->code_stack.clear(); |
| 202 | // whether code need this |
| 203 | emit->code->need_this = TRUE; |
| 204 | // keep track of full path (added 1.3.0.0) |
| 205 | emit->code->filename = emit->context->full_path; |
| 206 | // name the code |
| 207 | emit->code->name = emit->code->filename != "" ? mini(emit->code->filename.c_str()) : "[anonymous]"; |
| 208 | // push global scope (added 1.3.0.0) |
| 209 | emit->push_scope(); |
| 210 | |
| 211 | // for separating out function defs and class defs | 1.5.4.4 (ge) added |
| 212 | // this is to re-order code emission by stmt_lists -> func defs -> class defs |
| 213 | // ensures that local (file-scope) variables have a chance to acquire a stack offset |
| 214 | // before their use from functions and classes -- even if the local var declaration |
| 215 | // appears after the function or class definintion |
| 216 | vector<a_Func_Def> func_defs; |
| 217 | vector<a_Class_Def> class_defs; |
| 218 | |
| 219 | // loop over the program sections |
| 220 | while( prog && ret ) |
| 221 | { |
| 222 | switch( prog->section->s_type ) |
| 223 | { |
| 224 | case ae_section_stmt: // code section |
| 225 | // if only classes, then skip |
| 226 | if( how_much == te_do_import_only ) break; |
| 227 | // emit statement list |
| 228 | ret = emit_engine_emit_stmt_list( emit, prog->section->stmt_list ); |
| 229 | break; |
| 230 | |
| 231 | case ae_section_func: // function definition |
| 232 | // check the compilation criteria | 1.5.2.5 (ge) added |
| 233 | if( !howMuch_criteria_match( how_much, prog->section->func_def ) ) break; |
| 234 | // add function def for emission |
| 235 | func_defs.push_back( prog->section->func_def ); |
| 236 | break; |
| 237 | |
| 238 | case ae_section_class: // class definition |
| 239 | // 1.5.2.5 (ge) check the compilation criteria |
| 240 | // if( !howMuch_criteria_match( how_much, prog->section->class_def ) ) break; |
| 241 | // 1.5.4.0 (ge) commented out (see type_engine_prog0_scan() for explanation) |
| 242 | // add class def for emission |
| 243 | class_defs.push_back( prog->section->class_def ); |
| 244 | break; |
| 245 | |
| 246 | default: // bad |
| 247 | EM_error2( 0, |
| 248 | "internal compiler error: unrecognized program section..." ); |
| 249 | ret = FALSE; |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | // check success code |
| 254 | if( !ret ) break; |
| 255 | |
| 256 | // the next |
| 257 | prog = prog->next; |
| 258 | } |
| 259 | |
| 260 | // iterate over func defs, as long as `ret` is true | 1.5.4.4 (ge) moved out to here |
| 261 | for( size_t i = 0; ret && (i<func_defs.size()); i++ ) |
| 262 | { |
| 263 | // check function definition |
| 264 | ret = emit_engine_emit_func_def( emit, func_defs[i] ); |
| 265 | } |
| 266 | |
| 267 | // iterate over class defs, as long as `ret` is true | 1.5.4.4 (ge) moved out to here |
| 268 | for( size_t i = 0; ret && (i<class_defs.size()); i++ ) |
| 269 | { |
| 270 | // emit class definition |
| 271 | ret = emit_engine_emit_class_def( emit, class_defs[i] ); |
| 272 | } |
| 273 | |
| 274 | // 1.4.1.0 (jack): error-checking: was dac-replacement initted? |
| 275 | // (see chuck_compile.h for an explanation on replacement dacs) |
| 276 | // 1.5.4.4 (ge) added ret check |
| 277 | if( ret && emit->should_replace_dac ) |
| 278 | { |
| 279 | if( !emit->env->vm()->globals_manager()->is_global_ugen_init( emit->dac_replacement ) ) |
| 280 | { |
| 281 | EM_error2( 0, "compiler error: dac replacement '%s' was never initialized...", |
| 282 | emit->dac_replacement.c_str() ); |
| 283 | EM_error2( 0, "...(hint: need to declare this variable as a global UGen)" ); |
| 284 | ret = FALSE; |
| 285 | } |
| 286 | |
| 287 | // (also need to make sure it was constructed; see runtime error in |
| 288 | // Chuck_Instr_Reg_Push_Global) |
| 289 | } |
| 290 | |
| 291 | // check success code |
| 292 | if( ret ) |
| 293 | { |
| 294 | // get next instruction index |
| 295 | t_CKUINT index = emit->next_index(); |
| 296 | // pop global scope (added 1.3.0.0) |
| 297 | emit->pop_scope(); |
| 298 | // append end of code |
| 299 | emit->append( new Chuck_Instr_EOC ); |
| 300 | // add code str to whichever instruction began this section | 1.5.4.3 (ge) added |
| 301 | emit->code->code[index]->prepend_codestr( "/* end of code */" ); |
| 302 | |
| 303 | // make sure |
| 304 | assert( emit->context->nspc->pre_ctor == NULL ); |
| 305 | // converted to virtual machine code |
| 306 | emit->context->nspc->pre_ctor = emit_to_code( emit->code, NULL, emit->dump ); |
| 307 | // add reference |
| 308 | emit->context->nspc->pre_ctor->add_ref(); |
| 309 | } |
| 310 | else // error | 1.5.0.8 (ge) added in case emit errors out |
| 311 | { |
| 312 | // clear the code stack |
| 313 | Chuck_Code * code = NULL; |
| 314 | // while stack not empty |
| 315 | while( emit->code_stack.size() ) |
| 316 | { |
| 317 | // get the top of the stack |
| 318 | code = emit->code_stack.back(); |
| 319 | // manually delete the instructions |
| 320 | for( t_CKUINT i = 0; i < code->code.size(); i++ ) |
| 321 | { CK_SAFE_DELETE( code->code[i] ); } |
| 322 | // delete the code |
| 323 | CK_SAFE_DELETE( code ); |
| 324 | // pop the stack |
| 325 | emit->code_stack.pop_back(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // clear the code |
| 330 | CK_SAFE_DELETE( emit->code ); |
| 331 | |
| 332 | // clear stmt_stack (no need to delete contents; only contains instructions that should be cleaned up above) |
| 333 | emit->stmt_stack.clear(); |
| 334 | |
| 335 | // pop indent |
| 336 | EM_poplog(); |
| 337 | |
| 338 | // return the code |
| 339 | return emit->context->nspc->pre_ctor; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | //----------------------------------------------------------------------------- |
| 346 | // name: emit_to_code() |
| 347 | // desc: ... |
| 348 | //----------------------------------------------------------------------------- |
| 349 | Chuck_VM_Code * emit_to_code( Chuck_Code * in, |
| 350 | Chuck_VM_Code * out, |
| 351 | t_CKBOOL dump ) |
| 352 | { |
| 353 | // log |
| 354 | EM_log( CK_LOG_FINER, "emitting: %d VM instructions...", |
| 355 | in->code.size() ); |
| 356 | // allocate the vm code |
| 357 | Chuck_VM_Code * code = out ? out : new Chuck_VM_Code; |
| 358 | // make sure |
| 359 | assert( code->num_instr == 0 ); |
| 360 | // size |
| 361 | code->num_instr = in->code.size(); |
| 362 | // allocate instruction pointers+ |
| 363 | code->instr = new Chuck_Instr *[code->num_instr]; |
| 364 | // set the stack depth |
| 365 | code->stack_depth = in->stack_depth; |
| 366 | // set whether code need this base pointer |
| 367 | code->need_this = in->need_this; |
| 368 | // set whether static inside class | 1.4.1.0 (ge) added |
| 369 | code->is_static = in->is_static; |
| 370 | // set name |
| 371 | code->name = in->name; |
| 372 | // set filename for me.sourceDir() (added 1.3.0.0) |
| 373 | code->filename = in->filename; |
| 374 | |
| 375 | // copy |
| 376 | for( t_CKUINT i = 0; i < code->num_instr; i++ ) |
| 377 | code->instr[i] = in->code[i]; |
| 378 | |
| 379 | // dump |
| 380 | if( dump ) |
| 381 | { |
| 382 | // name of what we are dumping |
| 383 | EM_print2orange( "dumping %s:", in->name.c_str() ); |
| 384 | |
| 385 | // uh |
| 386 | EM_print2vanilla( "-------" ); |
| 387 | for( t_CKUINT i = 0; i < code->num_instr; i++ ) |
| 388 | { |
| 389 | // check code str (PRE) | 1.5.0.0 (ge) added |
| 390 | if( code->instr[i]->m_codestr_pre ) |
| 391 | { |
| 392 | const vector<string> & codestr = *(code->instr[i]->m_codestr_pre); |
| 393 | // loop |
| 394 | for( t_CKINT c = 0; c < codestr.size(); c++ ) |
| 395 | { |
| 396 | // print the reconstructed code str |
| 397 | EM_print2blue( "%s", codestr[c].c_str() ); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // print the instruction |
| 402 | EM_print2vanilla( "[%i] %s( %s )", i, |
| 403 | code->instr[i]->name(), code->instr[i]->params() ); |
| 404 | |
| 405 | // check code str (POST) | 1.5.0.8 (ge) added |
| 406 | if( code->instr[i]->m_codestr_post ) |
| 407 | { |
| 408 | const vector<string> & codestr = *(code->instr[i]->m_codestr_post); |
| 409 | // loop |
| 410 | for( t_CKINT c = 0; c < codestr.size(); c++ ) |
| 411 | { |
| 412 | // print the reconstructed code str |
| 413 | EM_print2blue( "%s", codestr[c].c_str() ); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | EM_print2vanilla( "-------\n" ); |
| 418 | } |
| 419 | |
| 420 | return code; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | //----------------------------------------------------------------------------- |
| 427 | // name: |
| 428 | // desc: ... |
| 429 | //----------------------------------------------------------------------------- |
| 430 | // t_CKBOOL emit_engine_addr_map( Chuck_Emitter * emit, Chuck_VM_Shred * shred ); |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | //----------------------------------------------------------------------------- |
| 436 | // name: |
| 437 | // desc: ... |
| 438 | //----------------------------------------------------------------------------- |
| 439 | // t_CKBOOL emit_engine_resolve( ); |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | //----------------------------------------------------------------------------- |
| 445 | // name: emit_engine_emit_stmt_list() |
| 446 | // desc: ... |
| 447 | //----------------------------------------------------------------------------- |
| 448 | t_CKBOOL emit_engine_emit_stmt_list( Chuck_Emitter * emit, a_Stmt_List list ) |
| 449 | { |
| 450 | t_CKBOOL ret = TRUE; |
| 451 | |
| 452 | // emit->push(); |
| 453 | while( list && ret ) |
| 454 | { |
| 455 | ret = emit_engine_emit_stmt( emit, list->stmt ); |
| 456 | list = list->next; |
| 457 | } |
| 458 | // emit->pop(); |
| 459 | |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | //----------------------------------------------------------------------------- |
| 467 | // name: emit_engine_emit_stmt() |
| 468 | // desc: emit a statement |
| 469 | //----------------------------------------------------------------------------- |
| 470 | t_CKBOOL emit_engine_emit_stmt( Chuck_Emitter * emit, a_Stmt stmt, t_CKBOOL pop ) |
| 471 | { |
| 472 | // empty stmt list |
| 473 | if( !stmt ) |
| 474 | return TRUE; |
| 475 | |
| 476 | // push stmt stack | 1.5.4.3 (ge) added to match the type system |
| 477 | // in order to do additional checks in the emitter #2024-static-init |
| 478 | emit->env->stmt_stack.push_back(stmt); |
| 479 | |
| 480 | // static initialization / initializer statements | 1.5.4.3 (ge) added #2024-static-init |
| 481 | // FYI semantics for static initializer (within class def but outside function def): |
| 482 | // 0) a single statement CANNOT read member data/call member functions AND init static data |
| 483 | // 1) otherwise, a single statement can access both static and member data |
| 484 | // these semantic rules should have been enforced in the type checker before this point |
| 485 | // so these are pre-conditions should we reach this point |
| 486 | // RUNTIME checks: |
| 487 | // 3) static initializer statements will be run in immediate mode, meaning no time advances; |
| 488 | // this is enforced at runtime; time advances will result in an exception |
| 489 | |
| 490 | // check if statement is a static initializer statement | 1.5.4.3 (ge) added |
| 491 | if( stmt->hasStaticDecl ) |
| 492 | { |
| 493 | // check pre-conditions |
| 494 | assert( emit->env->class_def != NULL ); |
| 495 | assert( emit->env->class_def->static_code_emit != NULL ); |
| 496 | |
| 497 | // get associated class |
| 498 | Chuck_Type * type = emit->env->class_def; |
| 499 | // push the current code sequencer |
| 500 | emit->code_stack.push_back( emit->code ); |
| 501 | // set the static code sequencer as the current |
| 502 | emit->code = type->static_code_emit; |
| 503 | } |
| 504 | |
| 505 | // return |
| 506 | t_CKBOOL ret = TRUE; |
| 507 | // next index |
| 508 | t_CKUINT nextIndex = 0; |
| 509 | // expression string |
| 510 | string codestr; |
| 511 | // closing codestr, as applicable |
| 512 | string codestr_close; |
| 513 | // codestr prefix, e.g., (/** loop conditional **/) for code dump |
| 514 | string codestr_prefix = emit->codestr_context_top(); |
| 515 | t_CKBOOL hasPrefix = codestr_prefix.length() > 0; |
| 516 | |
| 517 | // get str |
| 518 | codestr = absyn2str( stmt ); |
| 519 | // prefix? |
| 520 | if( hasPrefix ) codestr = codestr_prefix + " " + codestr; |
| 521 | // get next index | 1.5.0.0 (ge) added |
| 522 | nextIndex = emit->next_index(); |
| 523 | |
| 524 | // loop over statements |
| 525 | switch( stmt->s_type ) |
| 526 | { |
| 527 | case ae_stmt_exp: // expression statement |
| 528 | { |
| 529 | // emit the expression(s) that make up this stmt |
| 530 | ret = emit_engine_emit_exp( emit, stmt->stmt_exp, FALSE, stmt ); |
| 531 | if( !ret ) |
| 532 | return FALSE; |
| 533 | |
| 534 | // need to pop the final value from stack |
| 535 | if( pop ) |
| 536 | { |
| 537 | // exp |
| 538 | a_Exp exp = stmt->stmt_exp; |
| 539 | |
| 540 | // HACK: this won't work if <<< >>> is not the first in an expression chain |
| 541 | // <<< 1 >>>, 2, 3, 4 won't clean up the reg stack correctly |
| 542 | // 1, <<< 2 >>>, 3 needs to be checked |
| 543 | if( exp->s_type == ae_exp_primary && exp->primary.s_type == ae_primary_hack ) |
| 544 | exp = exp->primary.exp; |
| 545 | |
| 546 | // need to pop in reverse order |
| 547 | vector<a_Exp> pxe; |
| 548 | // go |
| 549 | while( exp ) |
| 550 | { |
| 551 | pxe.push_back( exp ); |
| 552 | exp = exp->next; |
| 553 | } |
| 554 | |
| 555 | // loop over the expressions that is the statement (prior to 1.5.0.0 comment: "HACK" hmmm...) |
| 556 | for( t_CKINT i = pxe.size()-1; i >= 0; i-- ) |
| 557 | { |
| 558 | // exp to work with |
| 559 | exp = pxe[i]; |
| 560 | |
| 561 | // if zero size, move on |
| 562 | if( exp->type->size == 0 ) continue; |
| 563 | // make sure, the stmt-exp cannot be a cast-to |
| 564 | assert( exp->cast_to == NULL ); |
| 565 | |
| 566 | // if decl, then expect only one word per var |
| 567 | // added 1.3.1.0: iskindofint -- since on some 64-bit systems sz_INT == sz_FLOAT |
| 568 | if( exp->s_type == ae_exp_decl ) |
| 569 | { |
| 570 | // (added 1.3.1.0 -- multiply by type size; #64-bit) |
| 571 | // (fixed 1.3.1.2 -- uh... decl should also be int-sized, so changed to INT/WORD) |
| 572 | emit->append( new Chuck_Instr_Reg_Pop_WordsMulti( exp->decl.num_var_decls * sz_INT / sz_WORD ) ); |
| 573 | } |
| 574 | else if( exp->type->size == sz_INT && iskindofint(emit->env, exp->type) ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 575 | { |
| 576 | // pop int value |
| 577 | // NOTE if obj left from exp being a func_call, the ref is now released by stmt mechanic | 1.5.1.7 |
| 578 | emit->append( new Chuck_Instr_Reg_Pop_Int ); |
| 579 | |
| 580 | // is an object left on the stack for the stmt |
| 581 | // if( exp->s_type == ae_exp_func_call && isobj(emit->env, exp->type) ) |
| 582 | // { emit->append( new Chuck_Instr_Release_Object3_Pop_Int ); } |
| 583 | // else // not an object |
| 584 | // { emit->append( new Chuck_Instr_Reg_Pop_Int ); } |
| 585 | } |
| 586 | else if( exp->type->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 587 | { |
| 588 | emit->append( new Chuck_Instr_Reg_Pop_Float ); |
| 589 | } |
| 590 | else if( exp->type->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 591 | { |
| 592 | emit->append( new Chuck_Instr_Reg_Pop_Vec2ComplexPolar ); |
| 593 | } |
| 594 | else if( exp->type->size == sz_VEC3 ) // ge: added 1.3.5.3 |
| 595 | { |
| 596 | emit->append( new Chuck_Instr_Reg_Pop_WordsMulti(sz_VEC3/sz_WORD) ); |
| 597 | } |
| 598 | else if( exp->type->size == sz_VEC4 ) // ge: added 1.3.5.3 |
| 599 | { |
| 600 | emit->append( new Chuck_Instr_Reg_Pop_WordsMulti(sz_VEC4/sz_WORD) ); |
| 601 | } |
| 602 | else |
| 603 | { |
| 604 | EM_error2( exp->where, |
| 605 | "(emit): internal error: %i byte stack item unhandled...", |
| 606 | exp->type->size ); |
| 607 | return FALSE; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | // add semicolon to code str |
| 613 | codestr += ";"; |
| 614 | break; |
| 615 | } |
| 616 | |
| 617 | case ae_stmt_import: // 1.5.4.0 (ge) added |
| 618 | // do nothing here (return true to bypass) |
| 619 | ret = TRUE; |
| 620 | break; |
| 621 | |
| 622 | case ae_stmt_doc: // 1.5.4.4 (ge) added |
| 623 | // do nothing here (return true to bypass) |
| 624 | ret = TRUE; |
| 625 | break; |
| 626 | |
| 627 | case ae_stmt_example: // 1.5.5.8 (ge,nick,alex) added |
| 628 | // do nothing here (return true to bypass) |
| 629 | ret = TRUE; |
| 630 | break; |
| 631 | |
| 632 | case ae_stmt_if: // if statement |
| 633 | ret = emit_engine_emit_if( emit, &stmt->stmt_if ); |
| 634 | codestr_close = "} /** " + codestr + " **/"; |
| 635 | codestr += " {"; |
| 636 | break; |
| 637 | |
| 638 | case ae_stmt_for: // for statement |
| 639 | ret = emit_engine_emit_for( emit, &stmt->stmt_for ); |
| 640 | codestr_close = "} /** " + codestr + " **/"; |
| 641 | codestr += " {"; |
| 642 | break; |
| 643 | |
| 644 | case ae_stmt_foreach: // for statement |
| 645 | ret = emit_engine_emit_foreach( emit, &stmt->stmt_foreach ); |
| 646 | codestr_close = "} /** " + codestr + " **/"; |
| 647 | codestr += " {"; |
| 648 | break; |
| 649 | |
| 650 | case ae_stmt_while: // while statement |
| 651 | if( stmt->stmt_while.is_do ) |
| 652 | { |
| 653 | ret = emit_engine_emit_do_while( emit, &stmt->stmt_while ); |
| 654 | codestr_close = "} " + codestr + ";"; |
| 655 | codestr = "do { /** " + codestr + " **/"; |
| 656 | } |
| 657 | else |
| 658 | { |
| 659 | ret = emit_engine_emit_while( emit, &stmt->stmt_while ); |
| 660 | codestr_close = "} /** " + codestr + " **/"; |
| 661 | codestr += " {"; |
| 662 | } |
| 663 | break; |
| 664 | |
| 665 | case ae_stmt_until: // until statement |
| 666 | if( stmt->stmt_until.is_do ) |
| 667 | { |
| 668 | ret = emit_engine_emit_do_until( emit, &stmt->stmt_until ); |
| 669 | codestr_close = "} " + codestr + ";"; |
| 670 | codestr = "do { /** " + codestr + " **/"; |
| 671 | } |
| 672 | else |
| 673 | { |
| 674 | ret = emit_engine_emit_until( emit, &stmt->stmt_until ); |
| 675 | codestr_close = "} /** " + codestr + " **/"; |
| 676 | codestr += " {"; |
| 677 | } |
| 678 | break; |
| 679 | |
| 680 | case ae_stmt_loop: // loop statement |
| 681 | ret = emit_engine_emit_loop( emit, &stmt->stmt_loop ); |
| 682 | codestr += " {"; |
| 683 | codestr_close = "}"; |
| 684 | break; |
| 685 | |
| 686 | case ae_stmt_break: // break statement |
| 687 | ret = emit_engine_emit_break( emit, &stmt->stmt_break ); |
| 688 | break; |
| 689 | |
| 690 | case ae_stmt_continue: // continue statement |
| 691 | ret = emit_engine_emit_continue( emit, &stmt->stmt_continue ); |
| 692 | break; |
| 693 | |
| 694 | case ae_stmt_return: // return statement |
| 695 | ret = emit_engine_emit_return( emit, &stmt->stmt_return ); |
| 696 | break; |
| 697 | |
| 698 | case ae_stmt_code: // code segment |
| 699 | // push scope | 1.5.2.5 (ge) |
| 700 | emit->push_scope(); |
| 701 | // { code } |
| 702 | ret = emit_engine_emit_code_segment( emit, &stmt->stmt_code ); |
| 703 | // pop scope | 1.5.2.5 (ge) |
| 704 | emit->pop_scope(); |
| 705 | break; |
| 706 | |
| 707 | case ae_stmt_switch: // switch statement |
| 708 | // not implemented |
| 709 | ret = FALSE; |
| 710 | break; |
| 711 | |
| 712 | case ae_stmt_case: // case statement |
| 713 | // not implemented |
| 714 | ret = FALSE; |
| 715 | // ret = emit_engine_emit_case( emit, &stmt->stmt_case ); |
| 716 | break; |
| 717 | |
| 718 | case ae_stmt_gotolabel: // goto label statement |
| 719 | // not implemented |
| 720 | ret = FALSE; |
| 721 | // ret = emit_engine_emit_gotolabel( emit, &stmt->stmt_case ); |
| 722 | break; |
| 723 | |
| 724 | default: // bad |
| 725 | EM_error2( stmt->where, |
| 726 | "(emit): internal error: unhandled statement type '%i'...", |
| 727 | stmt->s_type ); |
| 728 | break; |
| 729 | } |
| 730 | |
| 731 | // see if we added at least one instruction |
| 732 | if( codestr != "" && nextIndex < emit->next_index() ) |
| 733 | { |
| 734 | // PREPEND codestr (for instruction dump) |
| 735 | emit->code->code[nextIndex]->prepend_codestr( codestr ); |
| 736 | } |
| 737 | |
| 738 | // see if need to add closing |
| 739 | if( codestr_close != "" && nextIndex < emit->next_index()-1 ) |
| 740 | { |
| 741 | // APPPEND closing |
| 742 | emit->code->code[emit->next_index()-1]->append_codestr( codestr_close ); |
| 743 | } |
| 744 | |
| 745 | // if static initializer statement, restore |
| 746 | if( stmt->hasStaticDecl ) |
| 747 | { |
| 748 | // set the static code sequencer as the current |
| 749 | emit->code = emit->code_stack.back(); |
| 750 | // push the current code sequender |
| 751 | emit->code_stack.pop_back(); |
| 752 | } |
| 753 | |
| 754 | // pop stmt stack | 1.5.4.3 (ge) added as part of #2024-static-init |
| 755 | emit->env->stmt_stack.pop_back(); |
| 756 | |
| 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | |
| 762 | |
| 763 | //----------------------------------------------------------------------------- |
| 764 | // name: emit_engine_emit_if() |
| 765 | // desc: ... |
| 766 | //----------------------------------------------------------------------------- |
| 767 | t_CKBOOL emit_engine_emit_if( Chuck_Emitter * emit, a_Stmt_If stmt ) |
| 768 | { |
| 769 | t_CKBOOL ret = TRUE; |
| 770 | Chuck_Instr_Branch_Op * op = NULL, * op2 = NULL; |
| 771 | |
| 772 | // push the stack, allowing for new local variables |
| 773 | emit->push_scope(); |
| 774 | |
| 775 | // emit the condition |
| 776 | ret = emit_engine_emit_exp( emit, stmt->cond, FALSE, stmt->self ); |
| 777 | if( !ret ) |
| 778 | return FALSE; |
| 779 | |
| 780 | // type of the condition |
| 781 | switch( stmt->cond->type->xid ) |
| 782 | { |
| 783 | case te_int: |
| 784 | case te_object: // 1.5.5.8 (azaday) allow if(obj) |
| 785 | case te_user: // 1.5.5.8 (azaday) allow if(obj) |
| 786 | // push 0 |
| 787 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 788 | op = new Chuck_Instr_Branch_Eq_int( 0 ); |
| 789 | break; |
| 790 | case te_float: |
| 791 | case te_dur: |
| 792 | case te_time: |
| 793 | // push 0 |
| 794 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 795 | op = new Chuck_Instr_Branch_Eq_double( 0 ); |
| 796 | break; |
| 797 | |
| 798 | default: |
| 799 | // check for IO |
| 800 | if( isa( stmt->cond->type, emit->env->ckt_io ) ) |
| 801 | { |
| 802 | // push 0 |
| 803 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 804 | op = new Chuck_Instr_Branch_Eq_int_IO_good( 0 ); |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | EM_error2( stmt->cond->where, |
| 809 | "(emit): internal error: unhandled type '%s' in if condition", |
| 810 | stmt->cond->type->base_name.c_str() ); |
| 811 | return FALSE; |
| 812 | } |
| 813 | |
| 814 | if( !ret ) return FALSE; |
| 815 | |
| 816 | // append the op |
| 817 | emit->append( op ); |
| 818 | |
| 819 | // ge: 2012 april: scope for if body (added 1.3.0.0) |
| 820 | { |
| 821 | // push the stack, allowing for new local variables |
| 822 | emit->push_scope(); |
| 823 | // emit the body |
| 824 | ret = emit_engine_emit_stmt( emit, stmt->if_body ); |
| 825 | if( !ret ) |
| 826 | return FALSE; |
| 827 | // pop stack |
| 828 | emit->pop_scope(); |
| 829 | } |
| 830 | |
| 831 | // emit the skip to the end |
| 832 | emit->append( op2 = new Chuck_Instr_Goto(0) ); |
| 833 | |
| 834 | // remember the next index |
| 835 | t_CKUINT topOfElse = emit->next_index(); |
| 836 | // set the op's target |
| 837 | op->set( emit->next_index() ); |
| 838 | |
| 839 | // ge: 2012 april: scope for else body (added 1.3.0.0) |
| 840 | { |
| 841 | // push the stack, allowing for new local variables |
| 842 | emit->push_scope(); |
| 843 | // emit the body (else) |
| 844 | ret = emit_engine_emit_stmt( emit, stmt->else_body ); |
| 845 | if( !ret ) |
| 846 | return FALSE; |
| 847 | // add else to dump string |
| 848 | if( topOfElse < emit->next_index() ) |
| 849 | emit->code->code[topOfElse]->prepend_codestr( "} else {" ); |
| 850 | // pop stack |
| 851 | emit->pop_scope(); |
| 852 | } |
| 853 | |
| 854 | // set the op2's target |
| 855 | op2->set( emit->next_index() ); |
| 856 | |
| 857 | // pop stack |
| 858 | emit->pop_scope(); |
| 859 | |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | //----------------------------------------------------------------------------- |
| 867 | // name: emit_engine_emit_for() |
| 868 | // desc: for( C1; C2; C3 ) { BODY } |
| 869 | //----------------------------------------------------------------------------- |
| 870 | t_CKBOOL emit_engine_emit_for( Chuck_Emitter * emit, a_Stmt_For stmt ) |
| 871 | { |
| 872 | t_CKBOOL ret = TRUE; |
| 873 | Chuck_Instr_Branch_Op * op = NULL; |
| 874 | // codestr prefix for better description |
| 875 | string codestr_prefix = "/** loop conditional **/"; |
| 876 | // codestr push the context for instruction description / dump |
| 877 | emit->codestr_context_push( codestr_prefix ); |
| 878 | |
| 879 | // push the stack |
| 880 | emit->push_scope(); |
| 881 | |
| 882 | // C1 could be empty |
| 883 | if( stmt->c1 ) |
| 884 | { |
| 885 | // emit the cond |
| 886 | ret = emit_engine_emit_stmt( emit, stmt->c1 ); |
| 887 | if( !ret ) return FALSE; |
| 888 | } |
| 889 | |
| 890 | // the start index |
| 891 | t_CKUINT start_index = emit->next_index(); |
| 892 | t_CKUINT cont_index = 0; |
| 893 | // mark the stack of continue |
| 894 | emit->code->stack_cont.push_back( NULL ); |
| 895 | // mark the stack of break |
| 896 | emit->code->stack_break.push_back( NULL ); |
| 897 | |
| 898 | // emit the cond (C2) -- keep the result on the stack |
| 899 | ret = emit_engine_emit_stmt( emit, stmt->c2, FALSE ); |
| 900 | if( !ret ) return FALSE; |
| 901 | |
| 902 | // should not be NULL; in chuck C2 is not optional |
| 903 | // (enforced earlier in the compilation) |
| 904 | if( stmt->c2 ) |
| 905 | { |
| 906 | switch( stmt->c2->stmt_exp->type->xid ) |
| 907 | { |
| 908 | case te_int: |
| 909 | case te_object: // 1.5.5.8 (azaday) allow for( C1; obj; C3 ) |
| 910 | case te_user: // 1.5.5.8 (azaday) allow for( C1; obj; C3 ) |
| 911 | // push 0 |
| 912 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 913 | op = new Chuck_Instr_Branch_Eq_int( 0 ); |
| 914 | break; |
| 915 | case te_float: |
| 916 | case te_dur: |
| 917 | case te_time: |
| 918 | // push 0 |
| 919 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 920 | op = new Chuck_Instr_Branch_Eq_double( 0 ); |
| 921 | break; |
| 922 | |
| 923 | default: |
| 924 | // check for IO |
| 925 | if( isa( stmt->c2->stmt_exp->type, emit->env->ckt_io ) ) |
| 926 | { |
| 927 | // push 0 |
| 928 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 929 | op = new Chuck_Instr_Branch_Eq_int_IO_good( 0 ); |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | EM_error2( stmt->c2->stmt_exp->where, |
| 934 | "(emit): internal error: unhandled type '%s' in for conditional", |
| 935 | stmt->c2->stmt_exp->type->base_name.c_str() ); |
| 936 | return FALSE; |
| 937 | } |
| 938 | // append the op |
| 939 | emit->append( op ); |
| 940 | } |
| 941 | |
| 942 | // added 1.3.1.1: new scope just for loop body |
| 943 | emit->push_scope(); |
| 944 | |
| 945 | // pop the codestr context |
| 946 | emit->codestr_context_pop(); |
| 947 | |
| 948 | // emit the body |
| 949 | ret = emit_engine_emit_stmt( emit, stmt->body ); |
| 950 | if( !ret ) return FALSE; |
| 951 | |
| 952 | // added 1.3.1.1: pop scope for loop body |
| 953 | emit->pop_scope(); |
| 954 | |
| 955 | // continue here |
| 956 | cont_index = emit->next_index(); |
| 957 | |
| 958 | // emit the action (C3) |
| 959 | if( stmt->c3 ) |
| 960 | { |
| 961 | // get the code str |
| 962 | string codestr = absyn2str( stmt->c3 ); |
| 963 | // get the index |
| 964 | t_CKUINT c3_index = emit->next_index(); |
| 965 | // emit the expression |
| 966 | ret = emit_engine_emit_exp( emit, stmt->c3, FALSE, stmt->self ); |
| 967 | if( !ret ) return FALSE; |
| 968 | // add code str |
| 969 | emit->code->code[c3_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 970 | |
| 971 | // HACK! |
| 972 | // count the number of words to pop |
| 973 | a_Exp e = stmt->c3; |
| 974 | t_CKUINT num_words = 0; |
| 975 | while( e ) |
| 976 | { |
| 977 | if( e->type->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 978 | num_words += sz_FLOAT / sz_WORD; // changed to compute number of words; 1.3.1.0 |
| 979 | else if( e->type->size == sz_INT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 980 | num_words += sz_INT / sz_WORD; // changed to compute number of words; 1.3.1.0 |
| 981 | else if( e->type->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 982 | num_words += sz_VEC2 / sz_WORD; // changed to compute number of words; 1.3.1.0 |
| 983 | else if( e->type->size != 0 ) |
| 984 | { |
| 985 | EM_error2( e->where, |
| 986 | "(emit): internal error: non-void type size %i unhandled...", |
| 987 | e->type->size ); |
| 988 | return FALSE; |
| 989 | } |
| 990 | |
| 991 | // advance |
| 992 | e = e->next; |
| 993 | } |
| 994 | |
| 995 | // pop (changed to Chuck_Instr_Reg_Pop_WordsMulti in 1.3.1.0) |
| 996 | if( num_words > 0 ) emit->append( new Chuck_Instr_Reg_Pop_WordsMulti( num_words ) ); |
| 997 | } |
| 998 | |
| 999 | // go back to do check the condition |
| 1000 | emit->append( new Chuck_Instr_Goto( start_index ) ); |
| 1001 | |
| 1002 | // could be NULL |
| 1003 | if( stmt->c2 ) |
| 1004 | // set the op's target |
| 1005 | op->set( emit->next_index() ); |
| 1006 | |
| 1007 | // stack of continue |
| 1008 | while( emit->code->stack_cont.size() && emit->code->stack_cont.back() ) |
| 1009 | { |
| 1010 | emit->code->stack_cont.back()->set( cont_index ); |
| 1011 | emit->code->stack_cont.pop_back(); |
| 1012 | } |
| 1013 | |
| 1014 | // stack of break |
| 1015 | while( emit->code->stack_break.size() && emit->code->stack_break.back() ) |
| 1016 | { |
| 1017 | emit->code->stack_break.back()->set( emit->next_index() ); |
| 1018 | emit->code->stack_break.pop_back(); |
| 1019 | } |
| 1020 | |
| 1021 | // pop stack |
| 1022 | emit->pop_scope(); |
| 1023 | // pop continue stack |
| 1024 | emit->code->stack_cont.pop_back(); |
| 1025 | // pop break stack |
| 1026 | emit->code->stack_break.pop_back(); |
| 1027 | |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | //----------------------------------------------------------------------------- |
| 1035 | // name: emit_engine_emit_foreach() | 1.5.0.8 (ge) added |
| 1036 | // desc: emit instructions for( VAR : ARRAY ) |
| 1037 | //----------------------------------------------------------------------------- |
| 1038 | t_CKBOOL emit_engine_emit_foreach( Chuck_Emitter * emit, a_Stmt_ForEach stmt ) |
| 1039 | { |
| 1040 | Chuck_Instr_ForEach_Inc_And_Branch * op = NULL; |
| 1041 | t_CKBOOL ret = TRUE; |
| 1042 | // codestr prefix for better description | 1.5.0.8 |
| 1043 | string codestr_prefix = "/** loop control **/"; |
| 1044 | string codestr; |
| 1045 | t_CKUINT cond_index = 0; |
| 1046 | |
| 1047 | // push the stack |
| 1048 | emit->push_scope(); |
| 1049 | |
| 1050 | //------------------------------------------------------------------------- |
| 1051 | // emit VAR |
| 1052 | //------------------------------------------------------------------------- |
| 1053 | // get the code str | 1.5.0.8 |
| 1054 | codestr = absyn2str( stmt->theIter ); |
| 1055 | // get the index |
| 1056 | cond_index = emit->next_index(); |
| 1057 | // emit the iter part (can/should only be a decl; no releasing of stmt->exp dangling references) |
| 1058 | ret = emit_engine_emit_exp( emit, stmt->theIter /*, FALSE, stmt->self */ ); |
| 1059 | if( !ret ) return FALSE; |
| 1060 | // add code str | 1.5.0.8 |
| 1061 | emit->code->code[cond_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 1062 | |
| 1063 | // check everything is in place |
| 1064 | if( stmt->theIter->s_type != ae_exp_decl || |
| 1065 | !stmt->theIter->decl.var_decl_list || |
| 1066 | !stmt->theIter->decl.var_decl_list->var_decl || |
| 1067 | !stmt->theIter->decl.var_decl_list->var_decl->value ) |
| 1068 | { |
| 1069 | EM_error2( stmt->theIter->where, |
| 1070 | "(emit): internal error: cannot accessing foreach ITER value..." ); |
| 1071 | return FALSE; |
| 1072 | } |
| 1073 | |
| 1074 | // get the iter |
| 1075 | Chuck_Value * theIter = stmt->theIter->decl.var_decl_list->var_decl->value; |
| 1076 | |
| 1077 | //------------------------------------------------------------------------- |
| 1078 | // emit ARRAY |
| 1079 | //------------------------------------------------------------------------ |
| 1080 | // get the code str | 1.5.0.8 |
| 1081 | codestr = absyn2str( stmt->theArray ); |
| 1082 | // get the index |
| 1083 | cond_index = emit->next_index(); |
| 1084 | // track dangling refs, but instead of doing this inside the emit_exp(), |
| 1085 | // we need to this here, outside, since we must ensure that the array left |
| 1086 | // on the stack can't be released until after we make the assignment to |
| 1087 | // @foreach_array | 1.5.1.7 |
| 1088 | Chuck_Instr_Stmt_Start * start = emit_engine_track_stmt_refs_start( emit, stmt->self ); |
| 1089 | // emit the array part |
| 1090 | ret = emit_engine_emit_exp( emit, stmt->theArray /*, FALSE, stmt->self */ ); |
| 1091 | if( !ret ) return FALSE; |
| 1092 | // add code str | 1.5.0.8 |
| 1093 | emit->code->code[cond_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 1094 | |
| 1095 | //------------------------------------------------------------------------- |
| 1096 | // emit implicit ARRAY REF |
| 1097 | //------------------------------------------------------------------------- |
| 1098 | // allocate a local variable to hold the array pointer |
| 1099 | Chuck_Local * localArray = emit->alloc_local( emit->env->ckt_int->size, "@foreach_array", |
| 1100 | TRUE, TRUE, FALSE ); |
| 1101 | // check |
| 1102 | if( !localArray ) |
| 1103 | { |
| 1104 | EM_error2( stmt->theArray->where, |
| 1105 | "(emit): internal error: cannot allocate local @foreach_array..." ); |
| 1106 | return FALSE; |
| 1107 | } |
| 1108 | // emit our local array reference |
| 1109 | emit->append( new Chuck_Instr_Alloc_Word( localArray->offset, TRUE ) ); |
| 1110 | // copy object |
| 1111 | emit->append( new Chuck_Instr_Assign_Object() ); |
| 1112 | // clean up any dangling object refs | 1.5.1.7 |
| 1113 | emit_engine_track_stmt_refs_cleanup( emit, start ); |
| 1114 | |
| 1115 | //------------------------------------------------------------------------- |
| 1116 | // emit implicit iter COUNTER |
| 1117 | //------------------------------------------------------------------------- |
| 1118 | // allocate a local variable to hold the iterator |
| 1119 | Chuck_Local * localCounter = emit->alloc_local( emit->env->ckt_int->size, "@foreach_counter", |
| 1120 | FALSE, FALSE, FALSE ); |
| 1121 | // check |
| 1122 | if( !localCounter ) |
| 1123 | { |
| 1124 | EM_error2( stmt->where, |
| 1125 | "(emit): internal error: cannot allocate local @foreach_counter..." ); |
| 1126 | return FALSE; |
| 1127 | } |
| 1128 | // emit our local counter; borrow theIter's is_context_global |
| 1129 | emit->append( new Chuck_Instr_Alloc_Word( localCounter->offset, FALSE ) ); |
| 1130 | |
| 1131 | //------------------------------------------------------------------------- |
| 1132 | // emit conditional foreach BRANCH instruction |
| 1133 | //------------------------------------------------------------------------- |
| 1134 | // the start index |
| 1135 | t_CKUINT start_index = emit->next_index(); |
| 1136 | |
| 1137 | // get data kind and size relevant to array |
| 1138 | te_KindOf dataKind = getkindof( emit->env, stmt->theIter->type ); |
| 1139 | t_CKUINT dataSize = stmt->theIter->type->size; |
| 1140 | // compare branch condition and increment loop counter |
| 1141 | op = new Chuck_Instr_ForEach_Inc_And_Branch( dataKind, dataSize ); |
| 1142 | // emit the instruction |
| 1143 | emit->append( op ); |
| 1144 | |
| 1145 | //------------------------------------------------------------------------- |
| 1146 | // add STACK markers |
| 1147 | //------------------------------------------------------------------------- |
| 1148 | t_CKUINT cont_index = 0; |
| 1149 | // mark the stack of continue |
| 1150 | emit->code->stack_cont.push_back( NULL ); |
| 1151 | // mark the stack of break |
| 1152 | emit->code->stack_break.push_back( NULL ); |
| 1153 | |
| 1154 | //------------------------------------------------------------------------- |
| 1155 | // emit BODY |
| 1156 | //------------------------------------------------------------------------- |
| 1157 | // added 1.3.1.1: new scope just for loop body |
| 1158 | emit->push_scope(); |
| 1159 | |
| 1160 | // emit the body |
| 1161 | ret = emit_engine_emit_stmt( emit, stmt->body ); |
| 1162 | if( !ret ) |
| 1163 | return FALSE; |
| 1164 | |
| 1165 | // added 1.3.1.1: pop scope for loop body |
| 1166 | emit->pop_scope(); |
| 1167 | |
| 1168 | //------------------------------------------------------------------------- |
| 1169 | // mark CONTINUE point |
| 1170 | //------------------------------------------------------------------------- |
| 1171 | // continue here |
| 1172 | cont_index = emit->next_index(); |
| 1173 | |
| 1174 | //------------------------------------------------------------------------- |
| 1175 | // emit foreach ARRAY and ITER state |
| 1176 | //------------------------------------------------------------------------- |
| 1177 | // emit our local iter VAR |
| 1178 | emit->append( new Chuck_Instr_Reg_Push_Mem_Addr( theIter->offset, theIter->is_context_global ) ); |
| 1179 | // emit our local array reference; borrow theIter's is_context_global |
| 1180 | emit->append( new Chuck_Instr_Reg_Push_Mem( localArray->offset, theIter->is_context_global ) ); |
| 1181 | // emit our local counter; borry theIter's is_context_global |
| 1182 | emit->append( new Chuck_Instr_Reg_Push_Mem_Addr( localCounter->offset, theIter->is_context_global ) ); |
| 1183 | |
| 1184 | // mark it in the instruction description |
| 1185 | emit->code->code[cont_index]->prepend_codestr( codestr_prefix ); |
| 1186 | |
| 1187 | //------------------------------------------------------------------------- |
| 1188 | // emit GOTO |
| 1189 | //------------------------------------------------------------------------- |
| 1190 | // go back to do check the condition |
| 1191 | emit->append( new Chuck_Instr_Goto( start_index ) ); |
| 1192 | |
| 1193 | //------------------------------------------------------------------------- |
| 1194 | // immediately AFTER loop |
| 1195 | //------------------------------------------------------------------------- |
| 1196 | // set the op's target |
| 1197 | op->set( emit->next_index() ); |
| 1198 | |
| 1199 | //------------------------------------------------------------------------- |
| 1200 | // deal with CONTINUEs |
| 1201 | //------------------------------------------------------------------------- |
| 1202 | // stack of continue |
| 1203 | while( emit->code->stack_cont.size() && emit->code->stack_cont.back() ) |
| 1204 | { |
| 1205 | emit->code->stack_cont.back()->set( cont_index ); |
| 1206 | emit->code->stack_cont.pop_back(); |
| 1207 | } |
| 1208 | |
| 1209 | //------------------------------------------------------------------------- |
| 1210 | // deal with BREAKs |
| 1211 | //------------------------------------------------------------------------- |
| 1212 | // stack of break |
| 1213 | while( emit->code->stack_break.size() && emit->code->stack_break.back() ) |
| 1214 | { |
| 1215 | emit->code->stack_break.back()->set( emit->next_index() ); |
| 1216 | emit->code->stack_break.pop_back(); |
| 1217 | } |
| 1218 | |
| 1219 | //------------------------------------------------------------------------- |
| 1220 | // end of SCOPE |
| 1221 | //------------------------------------------------------------------------- |
| 1222 | // pop stack |
| 1223 | emit->pop_scope(); |
| 1224 | // pop continue stack |
| 1225 | emit->code->stack_cont.pop_back(); |
| 1226 | // pop break stack |
| 1227 | emit->code->stack_break.pop_back(); |
| 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | //----------------------------------------------------------------------------- |
| 1236 | // name: emit_engine_emit_while() |
| 1237 | // desc: while( COND ) { BODY } |
| 1238 | //----------------------------------------------------------------------------- |
| 1239 | t_CKBOOL emit_engine_emit_while( Chuck_Emitter * emit, a_Stmt_While stmt ) |
| 1240 | { |
| 1241 | t_CKBOOL ret = TRUE; |
| 1242 | Chuck_Instr_Branch_Op * op = NULL; |
| 1243 | // codestr prefix for better description | 1.5.0.8 |
| 1244 | string codestr_prefix = "/** loop conditional **/"; |
| 1245 | |
| 1246 | // push stack |
| 1247 | emit->push_scope(); |
| 1248 | |
| 1249 | // get the index |
| 1250 | t_CKUINT start_index = emit->next_index(); |
| 1251 | // mark the stack of continue |
| 1252 | emit->code->stack_cont.push_back( NULL ); |
| 1253 | // mark the stack of break |
| 1254 | emit->code->stack_break.push_back( NULL ); |
| 1255 | |
| 1256 | // get the code str | 1.5.0.8 |
| 1257 | string codestr = absyn2str( stmt->cond ); |
| 1258 | // get the index |
| 1259 | t_CKUINT cond_index = emit->next_index(); |
| 1260 | |
| 1261 | // emit the cond |
| 1262 | ret = emit_engine_emit_exp( emit, stmt->cond, FALSE, stmt->self ); |
| 1263 | if( !ret ) return FALSE; |
| 1264 | |
| 1265 | // add code str | 1.5.0.8 |
| 1266 | emit->code->code[cond_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 1267 | |
| 1268 | // the condition |
| 1269 | switch( stmt->cond->type->xid ) |
| 1270 | { |
| 1271 | case te_int: |
| 1272 | case te_object: // 1.5.5.8 (azaday) allow while(obj) |
| 1273 | case te_user: // 1.5.5.8 (azaday) allow while(obj) |
| 1274 | // push 0 |
| 1275 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1276 | op = new Chuck_Instr_Branch_Eq_int( 0 ); |
| 1277 | break; |
| 1278 | case te_float: |
| 1279 | case te_dur: |
| 1280 | case te_time: |
| 1281 | // push 0 |
| 1282 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 1283 | op = new Chuck_Instr_Branch_Eq_double( 0 ); |
| 1284 | break; |
| 1285 | |
| 1286 | default: |
| 1287 | // check for IO |
| 1288 | if( isa( stmt->cond->type, emit->env->ckt_io ) ) |
| 1289 | { |
| 1290 | // push 0 |
| 1291 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1292 | op = new Chuck_Instr_Branch_Eq_int_IO_good( 0 ); |
| 1293 | break; |
| 1294 | } |
| 1295 | |
| 1296 | EM_error2( stmt->cond->where, |
| 1297 | "(emit): internal error: unhandled type '%s' in while conditional", |
| 1298 | stmt->cond->type->base_name.c_str() ); |
| 1299 | return FALSE; |
| 1300 | } |
| 1301 | |
| 1302 | // append the op |
| 1303 | emit->append( op ); |
| 1304 | |
| 1305 | // added 1.3.1.1: new scope just for loop body |
| 1306 | emit->push_scope(); |
| 1307 | |
| 1308 | // emit the body |
| 1309 | ret = emit_engine_emit_stmt( emit, stmt->body ); |
| 1310 | if( !ret ) |
| 1311 | return FALSE; |
| 1312 | |
| 1313 | // added 1.3.1.1: pop scope for loop body |
| 1314 | emit->pop_scope(); |
| 1315 | |
| 1316 | // go back to do check the condition |
| 1317 | emit->append( new Chuck_Instr_Goto( start_index ) ); |
| 1318 | |
| 1319 | // set the op's target |
| 1320 | op->set( emit->next_index() ); |
| 1321 | |
| 1322 | // stack of continue |
| 1323 | while( emit->code->stack_cont.size() && emit->code->stack_cont.back() ) |
| 1324 | { |
| 1325 | emit->code->stack_cont.back()->set( start_index ); |
| 1326 | emit->code->stack_cont.pop_back(); |
| 1327 | } |
| 1328 | |
| 1329 | // stack of break |
| 1330 | while( emit->code->stack_break.size() && emit->code->stack_break.back() ) |
| 1331 | { |
| 1332 | emit->code->stack_break.back()->set( emit->next_index() ); |
| 1333 | emit->code->stack_break.pop_back(); |
| 1334 | } |
| 1335 | |
| 1336 | // pop stack |
| 1337 | emit->pop_scope(); |
| 1338 | // pop continue stack |
| 1339 | emit->code->stack_cont.pop_back(); |
| 1340 | // pop break stack |
| 1341 | emit->code->stack_break.pop_back(); |
| 1342 | |
| 1343 | return ret; |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | //----------------------------------------------------------------------------- |
| 1350 | // name: emit_engine_emit_do_while() |
| 1351 | // desc: ... |
| 1352 | //----------------------------------------------------------------------------- |
| 1353 | t_CKBOOL emit_engine_emit_do_while( Chuck_Emitter * emit, a_Stmt_While stmt ) |
| 1354 | { |
| 1355 | t_CKBOOL ret = TRUE; |
| 1356 | Chuck_Instr_Branch_Op * op = NULL; |
| 1357 | t_CKUINT start_index = emit->next_index(); |
| 1358 | // codestr prefix for better description | 1.5.0.8 |
| 1359 | string codestr_prefix = "/** loop conditional **/"; |
| 1360 | |
| 1361 | // push stack |
| 1362 | emit->push_scope(); |
| 1363 | |
| 1364 | // mark the stack of continue |
| 1365 | emit->code->stack_cont.push_back( NULL ); |
| 1366 | // mark the stack of break |
| 1367 | emit->code->stack_break.push_back( NULL ); |
| 1368 | |
| 1369 | // added 1.3.1.1: new scope just for loop body |
| 1370 | emit->push_scope(); |
| 1371 | |
| 1372 | // emit the body |
| 1373 | ret = emit_engine_emit_stmt( emit, stmt->body ); |
| 1374 | if( !ret ) |
| 1375 | return FALSE; |
| 1376 | |
| 1377 | // added 1.3.1.1: pop scope for loop body |
| 1378 | emit->pop_scope(); |
| 1379 | |
| 1380 | // get the code str | 1.5.0.8 |
| 1381 | string codestr = absyn2str( stmt->cond ); |
| 1382 | // get the index |
| 1383 | t_CKUINT cond_index = emit->next_index(); |
| 1384 | |
| 1385 | // emit the cond |
| 1386 | ret = emit_engine_emit_exp( emit, stmt->cond, FALSE, stmt->self ); |
| 1387 | if( !ret ) return FALSE; |
| 1388 | |
| 1389 | // add code str | 1.5.0.8 |
| 1390 | emit->code->code[cond_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 1391 | |
| 1392 | // the condition |
| 1393 | switch( stmt->cond->type->xid ) |
| 1394 | { |
| 1395 | case te_int: |
| 1396 | case te_object: // 1.5.5.8 (azaday) allow do{ }while(obj) |
| 1397 | case te_user: // 1.5.5.8 (azaday) allow do{ }while(obj) |
| 1398 | // push 0 |
| 1399 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1400 | op = new Chuck_Instr_Branch_Neq_int( 0 ); |
| 1401 | break; |
| 1402 | case te_float: |
| 1403 | case te_dur: |
| 1404 | case te_time: |
| 1405 | // push 0 |
| 1406 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 1407 | op = new Chuck_Instr_Branch_Neq_double( 0 ); |
| 1408 | break; |
| 1409 | |
| 1410 | default: |
| 1411 | // check for IO |
| 1412 | if( isa( stmt->cond->type, emit->env->ckt_io ) ) |
| 1413 | { |
| 1414 | // push 0 |
| 1415 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1416 | op = new Chuck_Instr_Branch_Eq_int_IO_good( 0 ); |
| 1417 | break; |
| 1418 | } |
| 1419 | |
| 1420 | EM_error2( stmt->cond->where, |
| 1421 | "(emit): internal error: unhandled type '%s' in do/while conditional", |
| 1422 | stmt->cond->type->c_name() ); |
| 1423 | return FALSE; |
| 1424 | } |
| 1425 | |
| 1426 | // append the op |
| 1427 | emit->append( op ); |
| 1428 | |
| 1429 | // set the op's target |
| 1430 | op->set( start_index ); |
| 1431 | |
| 1432 | // stack of continue |
| 1433 | while( emit->code->stack_cont.size() && emit->code->stack_cont.back() ) |
| 1434 | { |
| 1435 | emit->code->stack_cont.back()->set( start_index ); |
| 1436 | emit->code->stack_cont.pop_back(); |
| 1437 | } |
| 1438 | |
| 1439 | // stack of break |
| 1440 | while( emit->code->stack_break.size() && emit->code->stack_break.back() ) |
| 1441 | { |
| 1442 | emit->code->stack_break.back()->set( emit->next_index() ); |
| 1443 | emit->code->stack_break.pop_back(); |
| 1444 | } |
| 1445 | |
| 1446 | // pop stack |
| 1447 | emit->pop_scope(); |
| 1448 | // pop continue stack |
| 1449 | emit->code->stack_cont.pop_back(); |
| 1450 | // pop break stack |
| 1451 | emit->code->stack_break.pop_back(); |
| 1452 | |
| 1453 | return ret; |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | |
| 1459 | //----------------------------------------------------------------------------- |
| 1460 | // name: emit_engine_emit_until() |
| 1461 | // desc: ... |
| 1462 | //----------------------------------------------------------------------------- |
| 1463 | t_CKBOOL emit_engine_emit_until( Chuck_Emitter * emit, a_Stmt_Until stmt ) |
| 1464 | { |
| 1465 | t_CKBOOL ret = TRUE; |
| 1466 | Chuck_Instr_Branch_Op * op = NULL; |
| 1467 | // codestr prefix for better description | 1.5.0.8 |
| 1468 | string codestr_prefix = "/** loop conditional **/"; |
| 1469 | |
| 1470 | // push stack |
| 1471 | emit->push_scope(); |
| 1472 | |
| 1473 | // get index |
| 1474 | t_CKUINT start_index = emit->next_index(); |
| 1475 | // mark the stack of continue |
| 1476 | emit->code->stack_cont.push_back( NULL ); |
| 1477 | // mark the stack of break |
| 1478 | emit->code->stack_break.push_back( NULL ); |
| 1479 | |
| 1480 | // get the code str | 1.5.0.8 |
| 1481 | string codestr = absyn2str( stmt->cond ); |
| 1482 | // get the index |
| 1483 | t_CKUINT cond_index = emit->next_index(); |
| 1484 | |
| 1485 | // emit the cond |
| 1486 | ret = emit_engine_emit_exp( emit, stmt->cond, FALSE, stmt->self ); |
| 1487 | if( !ret ) return FALSE; |
| 1488 | |
| 1489 | // add code str | 1.5.0.8 |
| 1490 | emit->code->code[cond_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 1491 | |
| 1492 | // condition |
| 1493 | switch( stmt->cond->type->xid ) |
| 1494 | { |
| 1495 | case te_int: |
| 1496 | case te_object: // 1.5.5.8 (azaday) allow until(obj) |
| 1497 | case te_user: // 1.5.5.8 (azaday) allow until(obj) |
| 1498 | // push 0 |
| 1499 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1500 | op = new Chuck_Instr_Branch_Neq_int( 0 ); |
| 1501 | break; |
| 1502 | case te_float: |
| 1503 | case te_dur: |
| 1504 | case te_time: |
| 1505 | // push 0 |
| 1506 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 1507 | op = new Chuck_Instr_Branch_Neq_double( 0 ); |
| 1508 | break; |
| 1509 | |
| 1510 | default: |
| 1511 | // check for IO |
| 1512 | if( isa( stmt->cond->type, emit->env->ckt_io ) ) |
| 1513 | { |
| 1514 | // push 0 |
| 1515 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1516 | op = new Chuck_Instr_Branch_Neq_int_IO_good( 0 ); |
| 1517 | break; |
| 1518 | } |
| 1519 | |
| 1520 | EM_error2( stmt->cond->where, |
| 1521 | "(emit): internal error: unhandled type '%s' in until conditional", |
| 1522 | stmt->cond->type->base_name.c_str() ); |
| 1523 | return FALSE; |
| 1524 | } |
| 1525 | |
| 1526 | // append the op |
| 1527 | emit->append( op ); |
| 1528 | |
| 1529 | // added 1.3.1.1: new scope just for loop body |
| 1530 | emit->push_scope(); |
| 1531 | |
| 1532 | // emit the body |
| 1533 | emit_engine_emit_stmt( emit, stmt->body ); |
| 1534 | |
| 1535 | // added 1.3.1.1: pop scope for loop body |
| 1536 | emit->pop_scope(); |
| 1537 | |
| 1538 | // go back to do check the condition |
| 1539 | emit->append( new Chuck_Instr_Goto( start_index ) ); |
| 1540 | |
| 1541 | // set the op's target |
| 1542 | op->set( emit->next_index() ); |
| 1543 | |
| 1544 | // stack of continue |
| 1545 | while( emit->code->stack_cont.size() && emit->code->stack_cont.back() ) |
| 1546 | { |
| 1547 | emit->code->stack_cont.back()->set( start_index ); |
| 1548 | emit->code->stack_cont.pop_back(); |
| 1549 | } |
| 1550 | |
| 1551 | // stack of break |
| 1552 | while( emit->code->stack_break.size() && emit->code->stack_break.back() ) |
| 1553 | { |
| 1554 | emit->code->stack_break.back()->set( emit->next_index() ); |
| 1555 | emit->code->stack_break.pop_back(); |
| 1556 | } |
| 1557 | |
| 1558 | // pop stack |
| 1559 | emit->pop_scope(); |
| 1560 | // pop continue stack |
| 1561 | emit->code->stack_cont.pop_back(); |
| 1562 | // pop break stack |
| 1563 | emit->code->stack_break.pop_back(); |
| 1564 | |
| 1565 | return ret; |
| 1566 | } |
| 1567 | |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | //----------------------------------------------------------------------------- |
| 1572 | // name: emit_engine_emit_do_until() |
| 1573 | // desc: ... |
| 1574 | //----------------------------------------------------------------------------- |
| 1575 | t_CKBOOL emit_engine_emit_do_until( Chuck_Emitter * emit, a_Stmt_Until stmt ) |
| 1576 | { |
| 1577 | t_CKBOOL ret = TRUE; |
| 1578 | Chuck_Instr_Branch_Op * op = NULL; |
| 1579 | // codestr prefix for better description | 1.5.0.8 |
| 1580 | string codestr_prefix = "/** loop conditional **/"; |
| 1581 | |
| 1582 | // push stack |
| 1583 | emit->push_scope(); |
| 1584 | |
| 1585 | // the index |
| 1586 | t_CKUINT start_index = emit->next_index(); |
| 1587 | // mark the stack of continue |
| 1588 | emit->code->stack_cont.push_back( NULL ); |
| 1589 | // mark the stack of break |
| 1590 | emit->code->stack_break.push_back( NULL ); |
| 1591 | |
| 1592 | // added 1.3.1.1: new scope just for loop body |
| 1593 | emit->push_scope(); |
| 1594 | |
| 1595 | // emit the body |
| 1596 | ret = emit_engine_emit_stmt( emit, stmt->body ); |
| 1597 | if( !ret ) |
| 1598 | return FALSE; |
| 1599 | |
| 1600 | // added 1.3.1.1: pop scope for loop body |
| 1601 | emit->pop_scope(); |
| 1602 | |
| 1603 | // get the code str | 1.5.0.8 |
| 1604 | string codestr = absyn2str( stmt->cond ); |
| 1605 | // get the index |
| 1606 | t_CKUINT cond_index = emit->next_index(); |
| 1607 | |
| 1608 | // emit the cond |
| 1609 | ret = emit_engine_emit_exp( emit, stmt->cond, FALSE, stmt->self ); |
| 1610 | if( !ret ) return FALSE; |
| 1611 | |
| 1612 | // add code str | 1.5.0.8 |
| 1613 | emit->code->code[cond_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 1614 | |
| 1615 | // condition |
| 1616 | switch( stmt->cond->type->xid ) |
| 1617 | { |
| 1618 | case te_int: |
| 1619 | case te_object: // 1.5.5.8 (azaday) allow do{ }until(obj) |
| 1620 | case te_user: // 1.5.5.8 (azaday) allow do{ }until(obj) |
| 1621 | // push 0 |
| 1622 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1623 | op = new Chuck_Instr_Branch_Eq_int( 0 ); |
| 1624 | break; |
| 1625 | case te_float: |
| 1626 | case te_dur: |
| 1627 | case te_time: |
| 1628 | // push 0 |
| 1629 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 1630 | op = new Chuck_Instr_Branch_Eq_double( 0 ); |
| 1631 | break; |
| 1632 | |
| 1633 | default: |
| 1634 | EM_error2( stmt->cond->where, |
| 1635 | "(emit): internal error: unhandled type '%s' in do/until conditional", |
| 1636 | stmt->cond->type->base_name.c_str() ); |
| 1637 | return FALSE; |
| 1638 | } |
| 1639 | |
| 1640 | // append the op |
| 1641 | emit->append( op ); |
| 1642 | |
| 1643 | // set the op's target |
| 1644 | op->set( start_index ); |
| 1645 | |
| 1646 | // stack of continue |
| 1647 | while( emit->code->stack_cont.size() && emit->code->stack_cont.back() ) |
| 1648 | { |
| 1649 | emit->code->stack_cont.back()->set( start_index ); |
| 1650 | emit->code->stack_cont.pop_back(); |
| 1651 | } |
| 1652 | |
| 1653 | // stack of break |
| 1654 | while( emit->code->stack_break.size() && emit->code->stack_break.back() ) |
| 1655 | { |
| 1656 | emit->code->stack_break.back()->set( emit->next_index() ); |
| 1657 | emit->code->stack_break.pop_back(); |
| 1658 | } |
| 1659 | |
| 1660 | // pop stack |
| 1661 | emit->pop_scope(); |
| 1662 | // pop continue stack |
| 1663 | emit->code->stack_cont.pop_back(); |
| 1664 | // pop break stack |
| 1665 | emit->code->stack_break.pop_back(); |
| 1666 | |
| 1667 | return ret; |
| 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | //----------------------------------------------------------------------------- |
| 1674 | // name: emit_engine_emit_loop() |
| 1675 | // desc: ... |
| 1676 | //----------------------------------------------------------------------------- |
| 1677 | t_CKBOOL emit_engine_emit_loop( Chuck_Emitter * emit, a_Stmt_Loop stmt ) |
| 1678 | { |
| 1679 | t_CKBOOL ret = TRUE; |
| 1680 | Chuck_Instr_Branch_Op * op = NULL; |
| 1681 | // codestr prefix for better description | 1.5.0.8 |
| 1682 | string codestr_prefix = "/** loop conditional **/"; |
| 1683 | |
| 1684 | Chuck_Instr * instr = NULL; |
| 1685 | |
| 1686 | // push stack |
| 1687 | emit->push_scope(); |
| 1688 | |
| 1689 | // get the code str | 1.5.0.8 |
| 1690 | string codestr = absyn2str( stmt->cond ); |
| 1691 | // get the index |
| 1692 | t_CKUINT cond_index = emit->next_index(); |
| 1693 | |
| 1694 | // emit the cond |
| 1695 | ret = emit_engine_emit_exp( emit, stmt->cond, FALSE, stmt->self ); |
| 1696 | if( !ret ) return FALSE; |
| 1697 | |
| 1698 | // add code str | 1.5.0.8 |
| 1699 | emit->code->code[cond_index]->prepend_codestr( codestr_prefix + " " + codestr ); |
| 1700 | |
| 1701 | // initialize our loop counter (1.3.5.3) |
| 1702 | emit->append( new Chuck_Instr_Init_Loop_Counter() ); |
| 1703 | |
| 1704 | // get the index |
| 1705 | t_CKUINT start_index = emit->next_index(); |
| 1706 | // mark the stack of continue |
| 1707 | emit->code->stack_cont.push_back( NULL ); |
| 1708 | // mark the stack of break |
| 1709 | emit->code->stack_break.push_back( NULL ); |
| 1710 | |
| 1711 | // push the value of the loop counter |
| 1712 | emit->append( instr = new Chuck_Instr_Reg_Push_Loop_Counter_Deref() ); |
| 1713 | instr->set_linepos( stmt->line ); |
| 1714 | |
| 1715 | // get the type, taking cast into account |
| 1716 | Chuck_Type * type = stmt->cond->cast_to ? stmt->cond->cast_to : stmt->cond->type; |
| 1717 | |
| 1718 | // the condition |
| 1719 | switch( type->xid ) |
| 1720 | { |
| 1721 | case te_int: |
| 1722 | // push 0 |
| 1723 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 1724 | op = new Chuck_Instr_Branch_Eq_int( 0 ); |
| 1725 | break; |
| 1726 | |
| 1727 | default: |
| 1728 | EM_error2( stmt->cond->where, |
| 1729 | "(emit): internal error: unhandled type '%s' in loop conditional", |
| 1730 | type->base_name.c_str() ); |
| 1731 | |
| 1732 | return FALSE; |
| 1733 | } |
| 1734 | |
| 1735 | // append the op |
| 1736 | emit->append( op ); |
| 1737 | |
| 1738 | // decrement the counter |
| 1739 | emit->append( instr = new Chuck_Instr_Dec_Loop_Counter() ); |
| 1740 | instr->set_linepos( stmt->line ); |
| 1741 | |
| 1742 | // added 1.3.1.1: new scope just for loop body |
| 1743 | emit->push_scope(); |
| 1744 | |
| 1745 | // emit the body |
| 1746 | ret = emit_engine_emit_stmt( emit, stmt->body ); |
| 1747 | if( !ret ) |
| 1748 | return FALSE; |
| 1749 | |
| 1750 | // added 1.3.1.1: pop scope for loop body |
| 1751 | emit->pop_scope(); |
| 1752 | |
| 1753 | // go back to do check the condition |
| 1754 | emit->append( new Chuck_Instr_Goto( start_index ) ); |
| 1755 | |
| 1756 | // set the op's target |
| 1757 | op->set( emit->next_index() ); |
| 1758 | |
| 1759 | // pop the loop counter |
| 1760 | emit->append( new Chuck_Instr_Pop_Loop_Counter ); |
| 1761 | |
| 1762 | // stack of continue |
| 1763 | while( emit->code->stack_cont.size() && emit->code->stack_cont.back() ) |
| 1764 | { |
| 1765 | emit->code->stack_cont.back()->set( start_index ); |
| 1766 | emit->code->stack_cont.pop_back(); |
| 1767 | } |
| 1768 | |
| 1769 | // stack of break |
| 1770 | while( emit->code->stack_break.size() && emit->code->stack_break.back() ) |
| 1771 | { |
| 1772 | emit->code->stack_break.back()->set( emit->next_index() ); |
| 1773 | emit->code->stack_break.pop_back(); |
| 1774 | } |
| 1775 | |
| 1776 | // pop stack |
| 1777 | emit->pop_scope(); |
| 1778 | // pop continue stack |
| 1779 | emit->code->stack_cont.pop_back(); |
| 1780 | // pop break stack |
| 1781 | emit->code->stack_break.pop_back(); |
| 1782 | |
| 1783 | return ret; |
| 1784 | } |
| 1785 | |
| 1786 | |
| 1787 | |
| 1788 | |
| 1789 | //----------------------------------------------------------------------------- |
| 1790 | // name: emit_engine_emit_break() |
| 1791 | // desc: ... |
| 1792 | //----------------------------------------------------------------------------- |
| 1793 | t_CKBOOL emit_engine_emit_break( Chuck_Emitter * emit, a_Stmt_Break br ) |
| 1794 | { |
| 1795 | // append |
| 1796 | Chuck_Instr_Goto * op = new Chuck_Instr_Goto( 0 ); |
| 1797 | emit->append( op ); |
| 1798 | // remember |
| 1799 | emit->code->stack_break.push_back( op ); |
| 1800 | |
| 1801 | return TRUE; |
| 1802 | } |
| 1803 | |
| 1804 | |
| 1805 | |
| 1806 | |
| 1807 | //----------------------------------------------------------------------------- |
| 1808 | // name: emit_engine_emit_continue() |
| 1809 | // desc: ... |
| 1810 | //----------------------------------------------------------------------------- |
| 1811 | t_CKBOOL emit_engine_emit_continue( Chuck_Emitter * emit, a_Stmt_Continue cont ) |
| 1812 | { |
| 1813 | // append |
| 1814 | Chuck_Instr_Goto * op = new Chuck_Instr_Goto( 0 ); |
| 1815 | emit->append( op ); |
| 1816 | // remember |
| 1817 | emit->code->stack_cont.push_back( op ); |
| 1818 | |
| 1819 | return TRUE; |
| 1820 | } |
| 1821 | |
| 1822 | |
| 1823 | |
| 1824 | |
| 1825 | //----------------------------------------------------------------------------- |
| 1826 | // name: emit_engine_emit_return() |
| 1827 | // desc: ... |
| 1828 | //----------------------------------------------------------------------------- |
| 1829 | t_CKBOOL emit_engine_emit_return( Chuck_Emitter * emit, a_Stmt_Return stmt ) |
| 1830 | { |
| 1831 | // track dangling refs, but instead of doing this inside the emit_exp(), |
| 1832 | // we need to this here, outside, since we must ensure that the object left |
| 1833 | // on the stack can't be released until after we make add_ref below | 1.5.1.7 |
| 1834 | Chuck_Instr_Stmt_Start * start = emit_engine_track_stmt_refs_start( emit, stmt->self ); |
| 1835 | |
| 1836 | // emit the value |
| 1837 | if( !emit_engine_emit_exp( emit, stmt->val /*, FALSE, stmt->self */ ) ) |
| 1838 | return FALSE; |
| 1839 | |
| 1840 | // if return is an object type |
| 1841 | if( stmt->val && isobj( emit->env, stmt->val->type ) ) |
| 1842 | { |
| 1843 | // add reference (to be released by the function caller) |
| 1844 | emit->append( new Chuck_Instr_Reg_AddRef_Object3 ); |
| 1845 | } |
| 1846 | |
| 1847 | // clean up any dangling object refs | 1.5.1.7 |
| 1848 | emit_engine_track_stmt_refs_cleanup( emit, start ); |
| 1849 | |
| 1850 | // 1.5.0.0 (ge) | traverse and unwind current scope frames, release object |
| 1851 | // references given the current state of the stack frames; every `return` |
| 1852 | // statement needs its own scope unwinding; FYI: this does not change the |
| 1853 | // scope frames, which is needed to continue compilation for the rest |
| 1854 | // of the function; this does emit instructions (e.g., Release_Object2) |
| 1855 | // associated with this traversal action (also see 'break' and 'continue') |
| 1856 | emit->traverse_scope_on_jump(); |
| 1857 | |
| 1858 | // emit return |
| 1859 | // emit->append( new Chuck_Instr_Func_Return ); |
| 1860 | // determine where later |
| 1861 | Chuck_Instr_Goto * instr = new Chuck_Instr_Goto( 0 ); |
| 1862 | emit->append( instr ); |
| 1863 | emit->code->stack_return.push_back( instr ); |
| 1864 | |
| 1865 | return TRUE; |
| 1866 | } |
| 1867 | |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | //----------------------------------------------------------------------------- |
| 1872 | // name: |
| 1873 | // desc: ... |
| 1874 | //----------------------------------------------------------------------------- |
| 1875 | t_CKBOOL emit_engine_emit_switch( Chuck_Emitter * emit, a_Stmt_Switch stmt ); |
| 1876 | |
| 1877 | |
| 1878 | |
| 1879 | |
| 1880 | //----------------------------------------------------------------------------- |
| 1881 | // name: emit_engine_track_stmt_refs_start() |
| 1882 | // desc: as needed start tracking stmt-level dangling object refs |
| 1883 | //----------------------------------------------------------------------------- |
| 1884 | Chuck_Instr_Stmt_Start * emit_engine_track_stmt_refs_start( Chuck_Emitter * emit, a_Stmt stmt ) |
| 1885 | { |
| 1886 | // the stmt_start to push into stack | 1.5.1.7 |
| 1887 | Chuck_Instr_Stmt_Start * start = NULL; |
| 1888 | // see if we need to emit an instruction |
| 1889 | if( stmt && stmt->numObjsToRelease ) |
| 1890 | { |
| 1891 | // emit statement release |
| 1892 | emit->append( start = new Chuck_Instr_Stmt_Start( stmt->numObjsToRelease ) ); |
| 1893 | // push stack |
| 1894 | emit->stmt_stack.push_back( start ); |
| 1895 | } |
| 1896 | // done; could be NULL |
| 1897 | return start; |
| 1898 | } |
| 1899 | |
| 1900 | |
| 1901 | |
| 1902 | |
| 1903 | //----------------------------------------------------------------------------- |
| 1904 | // name: emit_engine_track_stmt_refs_cleanup() |
| 1905 | // desc: as needed cleanup tracking stmt-level dangling object refs |
| 1906 | //----------------------------------------------------------------------------- |
| 1907 | void emit_engine_track_stmt_refs_cleanup( Chuck_Emitter * emit, Chuck_Instr_Stmt_Start * start ) |
| 1908 | { |
| 1909 | // if objs to release | 1.5.1.7 |
| 1910 | if( start ) |
| 1911 | { |
| 1912 | // emit statement release |
| 1913 | emit->append( new Chuck_Instr_Stmt_Cleanup( start ) ); |
| 1914 | // pop stmt stack | 1.5.1.7 (ge) added |
| 1915 | emit->stmt_stack.pop_back(); |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | |
| 1920 | |
| 1921 | |
| 1922 | //----------------------------------------------------------------------------- |
| 1923 | // name: emit_engine_emit_exp() |
| 1924 | // desc: emit code for an expression |
| 1925 | // 1.3.0.0 | added doAddRef -- typically this is set to TRUE for function |
| 1926 | // calls so that arguments on reg stack are accounted for; this is important |
| 1927 | // in case the object is released/reclaimed before the value is used; |
| 1928 | // one particular case is when sporking with a local object as argument) |
| 1929 | // 1.5.1.7 | added enclosingStmt -- this should only be set by an enclosing |
| 1930 | // statement to track object references that needs releasing, e.g., |
| 1931 | // returning Object from a function, or a `new` allocation; this argument |
| 1932 | // enables checking and emitting instructions to track and clean these |
| 1933 | // "dangling" reference at the stmt->exp level |
| 1934 | //----------------------------------------------------------------------------- |
| 1935 | t_CKBOOL emit_engine_emit_exp( Chuck_Emitter * emit, a_Exp exp, t_CKBOOL doAddRef, |
| 1936 | a_Stmt enclosingStmt ) |
| 1937 | { |
| 1938 | // for now... |
| 1939 | // assert( exp->next == NULL ); |
| 1940 | |
| 1941 | // the stmt_start to push into stack | 1.5.1.7 |
| 1942 | Chuck_Instr_Stmt_Start * start = emit_engine_track_stmt_refs_start( emit, enclosingStmt ); |
| 1943 | |
| 1944 | // loop over |
| 1945 | while( exp ) |
| 1946 | { |
| 1947 | // check expression format |
| 1948 | switch( exp->s_type ) |
| 1949 | { |
| 1950 | case ae_exp_binary: |
| 1951 | if( !emit_engine_emit_exp_binary( emit, &exp->binary ) ) |
| 1952 | return FALSE; |
| 1953 | break; |
| 1954 | |
| 1955 | case ae_exp_unary: |
| 1956 | if( !emit_engine_emit_exp_unary( emit, &exp->unary ) ) |
| 1957 | return FALSE; |
| 1958 | break; |
| 1959 | |
| 1960 | case ae_exp_cast: |
| 1961 | if( !emit_engine_emit_exp_cast( emit, &exp->cast ) ) |
| 1962 | return FALSE; |
| 1963 | break; |
| 1964 | |
| 1965 | case ae_exp_postfix: |
| 1966 | if( !emit_engine_emit_exp_postfix( emit, &exp->postfix ) ) |
| 1967 | return FALSE; |
| 1968 | break; |
| 1969 | |
| 1970 | case ae_exp_dur: |
| 1971 | if( !emit_engine_emit_exp_dur( emit, &exp->dur ) ) |
| 1972 | return FALSE; |
| 1973 | break; |
| 1974 | |
| 1975 | case ae_exp_primary: |
| 1976 | if( !emit_engine_emit_exp_primary( emit, &exp->primary ) ) |
| 1977 | return FALSE; |
| 1978 | break; |
| 1979 | |
| 1980 | case ae_exp_array: |
| 1981 | if( !emit_engine_emit_exp_array( emit, &exp->array ) ) |
| 1982 | return FALSE; |
| 1983 | break; |
| 1984 | |
| 1985 | case ae_exp_func_call: |
| 1986 | if( !emit_engine_emit_exp_func_call( emit, &exp->func_call ) ) |
| 1987 | return FALSE; |
| 1988 | |
| 1989 | // if the function returns an Object | 1.5.1.7 |
| 1990 | if( isobj( emit->env, exp->func_call.ret_type ) ) |
| 1991 | { |
| 1992 | // the return needs to be released (later at the end of the stmt that contains this) |
| 1993 | Chuck_Instr_Stmt_Start * onStack = emit->stmt_stack.size() ? emit->stmt_stack.back() : NULL; |
| 1994 | // check it |
| 1995 | if( onStack ) |
| 1996 | { |
| 1997 | t_CKUINT offset = 0; |
| 1998 | // acquire next offset |
| 1999 | if( !onStack->nextOffset( offset ) ) return FALSE; |
| 2000 | // append instruction |
| 2001 | emit->append( new Chuck_Instr_Stmt_Remember_Object( onStack, offset ) ); |
| 2002 | } |
| 2003 | } |
| 2004 | break; |
| 2005 | |
| 2006 | case ae_exp_dot_member: |
| 2007 | if( !emit_engine_emit_exp_dot_member( emit, &exp->dot_member ) ) |
| 2008 | return FALSE; |
| 2009 | break; |
| 2010 | |
| 2011 | case ae_exp_if: |
| 2012 | if( !emit_engine_emit_exp_if( emit, &exp->exp_if ) ) |
| 2013 | return FALSE; |
| 2014 | break; |
| 2015 | |
| 2016 | case ae_exp_decl: |
| 2017 | if( !emit_engine_emit_exp_decl( emit, &exp->decl, FALSE ) ) |
| 2018 | return FALSE; |
| 2019 | break; |
| 2020 | |
| 2021 | //case ae_exp_namespace: |
| 2022 | // if( !emit_engine_emit_exp_namespace( emit, &exp->name_space ) ) |
| 2023 | // return FALSE; |
| 2024 | // break; |
| 2025 | |
| 2026 | default: |
| 2027 | EM_error2( exp->where, |
| 2028 | "(emit): internal error: unhandled expression type '%i'...", |
| 2029 | exp->s_type ); |
| 2030 | return FALSE; |
| 2031 | } |
| 2032 | |
| 2033 | // implicit cast |
| 2034 | if( exp->cast_to != NULL ) |
| 2035 | if( !emit_engine_emit_cast( emit, exp->cast_to, exp->type, exp->where ) ) |
| 2036 | return FALSE; |
| 2037 | |
| 2038 | // check if we need to handle ref (added 1.3.0.0) |
| 2039 | // (NOTE: cast shouldn't matter since pointer width should remain constant) |
| 2040 | if( doAddRef && isobj( emit->env, exp->type ) ) |
| 2041 | { |
| 2042 | // add ref in place on the stack |
| 2043 | emit->append( new Chuck_Instr_Reg_AddRef_Object3() ); |
| 2044 | } |
| 2045 | |
| 2046 | // next exp |
| 2047 | exp = exp->next; |
| 2048 | } |
| 2049 | |
| 2050 | // clean up any dangling object refs |
| 2051 | emit_engine_track_stmt_refs_cleanup( emit, start ); |
| 2052 | |
| 2053 | return TRUE; |
| 2054 | } |
| 2055 | |
| 2056 | |
| 2057 | |
| 2058 | |
| 2059 | //----------------------------------------------------------------------------- |
| 2060 | // name: emit_engine_emit_exp_binary() |
| 2061 | // desc: ... |
| 2062 | //----------------------------------------------------------------------------- |
| 2063 | t_CKBOOL emit_engine_emit_exp_binary( Chuck_Emitter * emit, a_Exp_Binary binary ) |
| 2064 | { |
| 2065 | // sanity |
| 2066 | assert( binary->self->emit_var == FALSE ); |
| 2067 | |
| 2068 | t_CKBOOL left = FALSE; |
| 2069 | t_CKBOOL right = FALSE; |
| 2070 | |
| 2071 | // hack to allow && to terminate as soon as possible - spencer |
| 2072 | // i.e. in ( 0 && f() ) the function f() will not be called |
| 2073 | // (obviates need for boolean AND instruction) |
| 2074 | if( binary->op == ae_op_and ) |
| 2075 | { |
| 2076 | Chuck_Instr_Branch_Op * op; |
| 2077 | |
| 2078 | // push default result |
| 2079 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 2080 | |
| 2081 | left = emit_engine_emit_exp( emit, binary->lhs ); |
| 2082 | if( !left ) |
| 2083 | return FALSE; |
| 2084 | |
| 2085 | // compare to 0; use default result if zero |
| 2086 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 2087 | emit->append( op = new Chuck_Instr_Branch_Eq_int( 0 ) ); |
| 2088 | |
| 2089 | // pop default result |
| 2090 | emit->append( new Chuck_Instr_Reg_Pop_Int ); |
| 2091 | |
| 2092 | // result of whole expression is now result of rhs |
| 2093 | right = emit_engine_emit_exp( emit, binary->rhs ); |
| 2094 | if( !right ) |
| 2095 | return FALSE; |
| 2096 | |
| 2097 | // boolify | 1.5.5.8 (ge,nick) added |
| 2098 | emit->append( new Chuck_Instr_Boolify ); |
| 2099 | |
| 2100 | // set branch location |
| 2101 | op->set( emit->next_index() ); |
| 2102 | |
| 2103 | return TRUE; |
| 2104 | } |
| 2105 | |
| 2106 | // hack to allow || to terminate as soon as possible - spencer |
| 2107 | // i.e. in ( 1 || f() ) the function f() will not be called |
| 2108 | // (obviates need for boolean OR instruction) |
| 2109 | else if( binary->op == ae_op_or ) |
| 2110 | { |
| 2111 | Chuck_Instr_Branch_Op * op; |
| 2112 | |
| 2113 | // push default result |
| 2114 | emit->append( new Chuck_Instr_Reg_Push_Imm( 1 ) ); |
| 2115 | |
| 2116 | left = emit_engine_emit_exp( emit, binary->lhs ); |
| 2117 | if( !left ) |
| 2118 | return FALSE; |
| 2119 | |
| 2120 | // compare to 0; use default result if non-zero |
| 2121 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 2122 | emit->append( op = new Chuck_Instr_Branch_Neq_int( 0 ) ); |
| 2123 | |
| 2124 | // pop default result |
| 2125 | emit->append( new Chuck_Instr_Reg_Pop_Int ); |
| 2126 | |
| 2127 | // result of whole expression is now result of rhs |
| 2128 | right = emit_engine_emit_exp( emit, binary->rhs ); |
| 2129 | if( !right ) |
| 2130 | return FALSE; |
| 2131 | |
| 2132 | // boolify | 1.5.5.8 (ge,nick) added |
| 2133 | emit->append( new Chuck_Instr_Boolify ); |
| 2134 | |
| 2135 | // set branch location |
| 2136 | op->set( emit->next_index() ); |
| 2137 | |
| 2138 | return TRUE; |
| 2139 | } |
| 2140 | |
| 2141 | // whether to track object references on stack (added 1.3.0.2) |
| 2142 | t_CKBOOL doRefLeft = FALSE; |
| 2143 | t_CKBOOL doRefRight = FALSE; |
| 2144 | |
| 2145 | // check to see if this is a function call (added 1.3.0.2) |
| 2146 | // i.e., does LHS => RHS resolve to function call RHS(LHS)? |
| 2147 | // added ae_op_chuck and make sure not null, latter has type-equivalence with object types in certain contexts | 1.5.1.7 |
| 2148 | // if( binary->op == ae_op_chuck && isa( binary->rhs->type, emit->env->ckt_function ) && !isnull( emit->env, binary->rhs->type ) ) |
| 2149 | if( type_engine_binary_is_func_call( emit->env, binary->op, binary->lhs, binary->rhs ) ) |
| 2150 | { |
| 2151 | // take care of objects in terms of reference counting |
| 2152 | doRefLeft = TRUE; |
| 2153 | |
| 2154 | // need to know here so the RHS can properly emit |
| 2155 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 2156 | binary->rhs->emit_as_funccall = TRUE; |
| 2157 | } |
| 2158 | // check operator overload | 1.5.1.5 (ge) |
| 2159 | t_CKBOOL op_overload = (binary->ck_overload_func != NULL); |
| 2160 | if( op_overload && isobj( emit->env, binary->lhs->type ) ) |
| 2161 | { |
| 2162 | // treat as arguments |
| 2163 | doRefLeft = TRUE; |
| 2164 | } |
| 2165 | // check operator overload |
| 2166 | if( op_overload && isobj( emit->env, binary->rhs->type ) ) |
| 2167 | { |
| 2168 | // treat as arguments |
| 2169 | doRefRight = TRUE; |
| 2170 | } |
| 2171 | |
| 2172 | // emit (doRef added 1.3.0.2) |
| 2173 | left = emit_engine_emit_exp( emit, binary->lhs, doRefLeft ); |
| 2174 | if( !left ) return FALSE; |
| 2175 | right = emit_engine_emit_exp( emit, binary->rhs, doRefRight ); |
| 2176 | if( !right ) return FALSE; |
| 2177 | // check |
| 2178 | // if( !left || !right ) return FALSE; |
| 2179 | |
| 2180 | // emit the op |
| 2181 | if( !emit_engine_emit_op( emit, binary->op, binary->lhs, binary->rhs, binary ) ) |
| 2182 | return FALSE; |
| 2183 | |
| 2184 | return TRUE; |
| 2185 | } |
| 2186 | |
| 2187 | |
| 2188 | |
| 2189 | |
| 2190 | //----------------------------------------------------------------------------- |
| 2191 | // name: emit_engine_emit_op() |
| 2192 | // desc: emit binary operator |
| 2193 | //----------------------------------------------------------------------------- |
| 2194 | t_CKBOOL emit_engine_emit_op( Chuck_Emitter * emit, ae_Operator op, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary ) |
| 2195 | { |
| 2196 | // any implicit cast happens before this |
| 2197 | Chuck_Type * t_left = lhs->cast_to ? lhs->cast_to : lhs->type; |
| 2198 | Chuck_Type * t_right = rhs->cast_to ? rhs->cast_to : rhs->type; |
| 2199 | // get the types of the left and right |
| 2200 | te_Type left = t_left->xid; |
| 2201 | te_Type right = t_right->xid; |
| 2202 | // op |
| 2203 | Chuck_Instr * instr = NULL; |
| 2204 | |
| 2205 | // check operator overload; unlike in type_engine by this point we know |
| 2206 | // whether an operator should be using an explicit overloading |
| 2207 | if( binary->ck_overload_func ) |
| 2208 | { |
| 2209 | // emit operator overload | 1.5.1.5 (ge) added |
| 2210 | return emit_engine_emit_op_overload_binary( emit, binary ); |
| 2211 | } |
| 2212 | |
| 2213 | // emit op |
| 2214 | switch( op ) |
| 2215 | { |
| 2216 | // ----------------------------- num -------------------------------------- |
| 2217 | case ae_op_plus: |
| 2218 | // time + dur |
| 2219 | if( ( left == te_dur && right == te_time ) || |
| 2220 | ( left == te_time && right == te_dur ) ) |
| 2221 | { |
| 2222 | emit->append( instr = new Chuck_Instr_Add_double ); |
| 2223 | } |
| 2224 | else // other types |
| 2225 | { |
| 2226 | switch( left ) |
| 2227 | { |
| 2228 | case te_int: |
| 2229 | emit->append( instr = new Chuck_Instr_Add_int ); |
| 2230 | break; |
| 2231 | case te_float: |
| 2232 | case te_dur: |
| 2233 | emit->append( instr = new Chuck_Instr_Add_double ); |
| 2234 | break; |
| 2235 | case te_complex: |
| 2236 | emit->append( instr = new Chuck_Instr_Add_complex ); |
| 2237 | break; |
| 2238 | case te_polar: |
| 2239 | emit->append( instr = new Chuck_Instr_Add_polar ); |
| 2240 | break; |
| 2241 | case te_vec2: |
| 2242 | emit->append( instr = new Chuck_Instr_Add_vec2 ); |
| 2243 | break; |
| 2244 | case te_vec3: |
| 2245 | emit->append( instr = new Chuck_Instr_Add_vec3 ); |
| 2246 | break; |
| 2247 | case te_vec4: |
| 2248 | emit->append( instr = new Chuck_Instr_Add_vec4 ); |
| 2249 | break; |
| 2250 | |
| 2251 | default: break; |
| 2252 | } |
| 2253 | } |
| 2254 | break; |
| 2255 | |
| 2256 | case ae_op_plus_chuck: |
| 2257 | // time advance |
| 2258 | if( left == te_dur && right == te_time && rhs->s_meta == ae_meta_var && |
| 2259 | rhs->s_type == ae_exp_primary && !strcmp( "now", S_name(rhs->primary.var) ) ) |
| 2260 | { |
| 2261 | // add the two |
| 2262 | emit->append( new Chuck_Instr_Add_double ); |
| 2263 | // advance time (TODO: verify having two instr is OK) |
| 2264 | emit->append( instr = new Chuck_Instr_Time_Advance ); |
| 2265 | instr->set_linepos( lhs->line ); |
| 2266 | } |
| 2267 | // time +=> dur |
| 2268 | else if( ( left == te_dur && right == te_time ) || |
| 2269 | ( left == te_time && right == te_dur ) ) |
| 2270 | { |
| 2271 | emit->append( instr = new Chuck_Instr_Add_double_Assign ); |
| 2272 | } |
| 2273 | // string +=> string |
| 2274 | else if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) ) |
| 2275 | { |
| 2276 | // concatenate |
| 2277 | emit->append( instr = new Chuck_Instr_Add_string_Assign ); |
| 2278 | instr->set_linepos( lhs->line ); |
| 2279 | } |
| 2280 | // right: string |
| 2281 | else if( isa( t_right, emit->env->ckt_string ) ) |
| 2282 | { |
| 2283 | // + int |
| 2284 | if( isa( t_left, emit->env->ckt_int ) ) |
| 2285 | { |
| 2286 | emit->append( instr = new Chuck_Instr_Add_int_string_Assign ); |
| 2287 | // null exceptions will occur from string: use rhs, not lhs |
| 2288 | instr->set_linepos( rhs->line ); |
| 2289 | } |
| 2290 | else if( isa( t_left, emit->env->ckt_float ) ) |
| 2291 | { |
| 2292 | emit->append( instr = new Chuck_Instr_Add_float_string_Assign ); |
| 2293 | instr->set_linepos( rhs->line ); |
| 2294 | } |
| 2295 | else |
| 2296 | { |
| 2297 | EM_error2( lhs->where, |
| 2298 | "(emit): internal error: unhandled op '%s' %s '%s'", |
| 2299 | t_left->c_name(), op2str( op ), t_right->c_name() ); |
| 2300 | return FALSE; |
| 2301 | } |
| 2302 | } |
| 2303 | else // other types |
| 2304 | { |
| 2305 | switch( left ) |
| 2306 | { |
| 2307 | case te_int: |
| 2308 | emit->append( instr = new Chuck_Instr_Add_int_Assign ); |
| 2309 | break; |
| 2310 | case te_float: |
| 2311 | case te_dur: |
| 2312 | emit->append( instr = new Chuck_Instr_Add_double_Assign ); |
| 2313 | break; |
| 2314 | case te_complex: |
| 2315 | emit->append( instr = new Chuck_Instr_Add_complex_Assign ); |
| 2316 | break; |
| 2317 | case te_polar: |
| 2318 | emit->append( instr = new Chuck_Instr_Add_polar_Assign ); |
| 2319 | break; |
| 2320 | case te_vec2: |
| 2321 | emit->append( instr = new Chuck_Instr_Add_vec2_Assign ); |
| 2322 | break; |
| 2323 | case te_vec3: |
| 2324 | emit->append( instr = new Chuck_Instr_Add_vec3_Assign ); |
| 2325 | break; |
| 2326 | case te_vec4: |
| 2327 | emit->append( instr = new Chuck_Instr_Add_vec4_Assign ); |
| 2328 | break; |
| 2329 | |
| 2330 | default: break; |
| 2331 | } |
| 2332 | } |
| 2333 | break; |
| 2334 | |
| 2335 | case ae_op_minus: |
| 2336 | if( ( left == te_time && right == te_dur ) ) // time - dur = time |
| 2337 | emit->append( instr = new Chuck_Instr_Minus_double ); |
| 2338 | else if( ( left == te_time && right == te_time ) ) // time - time = dur |
| 2339 | emit->append( instr = new Chuck_Instr_Minus_double ); |
| 2340 | else // other types |
| 2341 | { |
| 2342 | switch( left ) |
| 2343 | { |
| 2344 | case te_int: |
| 2345 | emit->append( instr = new Chuck_Instr_Minus_int ); |
| 2346 | break; |
| 2347 | case te_float: |
| 2348 | case te_dur: |
| 2349 | emit->append( instr = new Chuck_Instr_Minus_double ); |
| 2350 | break; |
| 2351 | case te_complex: |
| 2352 | emit->append( instr = new Chuck_Instr_Minus_complex ); |
| 2353 | break; |
| 2354 | case te_polar: |
| 2355 | emit->append( instr = new Chuck_Instr_Minus_polar ); |
| 2356 | break; |
| 2357 | case te_vec2: |
| 2358 | emit->append( instr = new Chuck_Instr_Minus_vec2 ); |
| 2359 | break; |
| 2360 | case te_vec3: |
| 2361 | emit->append( instr = new Chuck_Instr_Minus_vec3 ); |
| 2362 | break; |
| 2363 | case te_vec4: |
| 2364 | emit->append( instr = new Chuck_Instr_Minus_vec4 ); |
| 2365 | break; |
| 2366 | |
| 2367 | default: break; |
| 2368 | } |
| 2369 | } |
| 2370 | break; |
| 2371 | |
| 2372 | case ae_op_minus_chuck: |
| 2373 | if( ( left == te_dur && right == te_time ) ) // time - dur = time |
| 2374 | emit->append( instr = new Chuck_Instr_Minus_double_Assign ); |
| 2375 | else if( ( left == te_time && right == te_time ) ) // time - time = dur |
| 2376 | emit->append( instr = new Chuck_Instr_Minus_double_Assign ); |
| 2377 | else // other types |
| 2378 | { |
| 2379 | switch( left ) |
| 2380 | { |
| 2381 | case te_int: |
| 2382 | emit->append( instr = new Chuck_Instr_Minus_int_Assign ); |
| 2383 | break; |
| 2384 | case te_float: |
| 2385 | case te_dur: |
| 2386 | emit->append( instr = new Chuck_Instr_Minus_double_Assign ); |
| 2387 | break; |
| 2388 | case te_complex: |
| 2389 | emit->append( instr = new Chuck_Instr_Minus_complex_Assign ); |
| 2390 | break; |
| 2391 | case te_polar: |
| 2392 | emit->append( instr = new Chuck_Instr_Minus_polar_Assign ); |
| 2393 | break; |
| 2394 | case te_vec2: |
| 2395 | emit->append( instr = new Chuck_Instr_Minus_vec2_Assign ); |
| 2396 | break; |
| 2397 | case te_vec3: |
| 2398 | emit->append( instr = new Chuck_Instr_Minus_vec3_Assign ); |
| 2399 | break; |
| 2400 | case te_vec4: |
| 2401 | emit->append( instr = new Chuck_Instr_Minus_vec4_Assign ); |
| 2402 | break; |
| 2403 | |
| 2404 | default: break; |
| 2405 | } |
| 2406 | } |
| 2407 | break; |
| 2408 | |
| 2409 | case ae_op_times: |
| 2410 | // check |
| 2411 | if( left == te_float && right == te_vec2 ) // 1.5.1.7 (ge) added |
| 2412 | { |
| 2413 | emit->append( instr = new Chuck_Instr_float_Times_vec2 ); |
| 2414 | } |
| 2415 | else if( left == te_vec2 && right == te_float ) |
| 2416 | { |
| 2417 | emit->append( instr = new Chuck_Instr_vec2_Times_float ); |
| 2418 | } |
| 2419 | else if( left == te_float && right == te_vec3 ) |
| 2420 | { |
| 2421 | emit->append( instr = new Chuck_Instr_float_Times_vec3 ); |
| 2422 | } |
| 2423 | else if( left == te_vec3 && right == te_float ) |
| 2424 | { |
| 2425 | emit->append( instr = new Chuck_Instr_vec3_Times_float ); |
| 2426 | } |
| 2427 | else if( left == te_float && right == te_vec4 ) |
| 2428 | { |
| 2429 | emit->append( instr = new Chuck_Instr_float_Times_vec4 ); |
| 2430 | } |
| 2431 | else if( left == te_vec4 && right == te_float ) |
| 2432 | { |
| 2433 | emit->append( instr = new Chuck_Instr_vec4_Times_float ); |
| 2434 | } |
| 2435 | else // other cases |
| 2436 | { |
| 2437 | switch( left ) |
| 2438 | { |
| 2439 | case te_int: |
| 2440 | emit->append( instr = new Chuck_Instr_Times_int ); |
| 2441 | break; |
| 2442 | case te_float: |
| 2443 | case te_dur: |
| 2444 | emit->append( instr = new Chuck_Instr_Times_double ); |
| 2445 | break; |
| 2446 | case te_complex: |
| 2447 | emit->append( instr = new Chuck_Instr_Times_complex ); |
| 2448 | break; |
| 2449 | case te_polar: |
| 2450 | emit->append( instr = new Chuck_Instr_Times_polar ); |
| 2451 | break; |
| 2452 | case te_vec3: |
| 2453 | emit->append( instr = new Chuck_Instr_XProduct_vec3 ); |
| 2454 | break; |
| 2455 | case te_vec4: |
| 2456 | emit->append( instr = new Chuck_Instr_XProduct_vec4 ); |
| 2457 | break; |
| 2458 | |
| 2459 | default: break; |
| 2460 | } |
| 2461 | } |
| 2462 | break; |
| 2463 | |
| 2464 | case ae_op_times_chuck: |
| 2465 | // check |
| 2466 | if( left == te_float && right == te_vec2 ) |
| 2467 | { |
| 2468 | emit->append( instr = new Chuck_Instr_float_Times_vec2_Assign ); |
| 2469 | } |
| 2470 | else if( left == te_float && right == te_vec3 ) |
| 2471 | { |
| 2472 | emit->append( instr = new Chuck_Instr_float_Times_vec3_Assign ); |
| 2473 | } |
| 2474 | else if( left == te_float && right == te_vec4 ) |
| 2475 | { |
| 2476 | emit->append( instr = new Chuck_Instr_float_Times_vec4_Assign ); |
| 2477 | } |
| 2478 | else |
| 2479 | { |
| 2480 | switch( left ) |
| 2481 | { |
| 2482 | case te_int: |
| 2483 | emit->append( instr = new Chuck_Instr_Times_int_Assign ); |
| 2484 | break; |
| 2485 | case te_float: |
| 2486 | emit->append( instr = new Chuck_Instr_Times_double_Assign ); |
| 2487 | break; |
| 2488 | case te_complex: |
| 2489 | emit->append( instr = new Chuck_Instr_Times_complex_Assign ); |
| 2490 | break; |
| 2491 | case te_polar: |
| 2492 | emit->append( instr = new Chuck_Instr_Times_polar_Assign ); |
| 2493 | break; |
| 2494 | |
| 2495 | default: break; |
| 2496 | } |
| 2497 | } |
| 2498 | break; |
| 2499 | |
| 2500 | case ae_op_divide: |
| 2501 | if( ( left == te_time && right == te_dur ) ) // time / dur = float |
| 2502 | { |
| 2503 | emit->append( instr = new Chuck_Instr_Divide_double ); |
| 2504 | } |
| 2505 | else if( left == te_vec2 && right == te_float ) |
| 2506 | { |
| 2507 | emit->append( instr = new Chuck_Instr_vec2_Divide_float ); |
| 2508 | } |
| 2509 | else if( left == te_vec3 && right == te_float ) |
| 2510 | { |
| 2511 | emit->append( instr = new Chuck_Instr_vec3_Divide_float ); |
| 2512 | } |
| 2513 | else if( left == te_vec4 && right == te_float ) |
| 2514 | { |
| 2515 | emit->append( instr = new Chuck_Instr_vec4_Divide_float ); |
| 2516 | } |
| 2517 | else // other types |
| 2518 | { |
| 2519 | switch( left ) |
| 2520 | { |
| 2521 | case te_int: |
| 2522 | emit->append( instr = new Chuck_Instr_Divide_int ); |
| 2523 | instr->set_linepos( rhs->line ); |
| 2524 | break; |
| 2525 | case te_float: |
| 2526 | case te_dur: |
| 2527 | emit->append( instr = new Chuck_Instr_Divide_double ); |
| 2528 | break; |
| 2529 | case te_complex: |
| 2530 | emit->append( instr = new Chuck_Instr_Divide_complex ); |
| 2531 | break; |
| 2532 | case te_polar: |
| 2533 | emit->append( instr = new Chuck_Instr_Divide_polar ); |
| 2534 | break; |
| 2535 | |
| 2536 | default: break; |
| 2537 | } |
| 2538 | } |
| 2539 | break; |
| 2540 | |
| 2541 | case ae_op_divide_chuck: |
| 2542 | // reverse |
| 2543 | if( ( left == te_dur && right == te_time ) ) // time / dur = float |
| 2544 | { |
| 2545 | emit->append( instr = new Chuck_Instr_Divide_double_Assign ); |
| 2546 | } |
| 2547 | else if( left == te_float && right == te_vec2 ) |
| 2548 | { |
| 2549 | emit->append( instr = new Chuck_Instr_vec2_Divide_float_Assign ); |
| 2550 | } |
| 2551 | else if( left == te_float && right == te_vec3 ) |
| 2552 | { |
| 2553 | emit->append( instr = new Chuck_Instr_vec3_Divide_float_Assign ); |
| 2554 | } |
| 2555 | else if( left == te_float && right == te_vec4 ) |
| 2556 | { |
| 2557 | emit->append( instr = new Chuck_Instr_vec4_Divide_float_Assign ); |
| 2558 | } |
| 2559 | else // other types |
| 2560 | { |
| 2561 | switch( left ) |
| 2562 | { |
| 2563 | case te_int: |
| 2564 | emit->append( instr = new Chuck_Instr_Divide_int_Assign ); |
| 2565 | instr->set_linepos( lhs->line ); |
| 2566 | break; |
| 2567 | case te_float: |
| 2568 | emit->append( instr = new Chuck_Instr_Divide_double_Assign ); |
| 2569 | break; |
| 2570 | case te_complex: |
| 2571 | emit->append( instr = new Chuck_Instr_Divide_complex_Assign ); |
| 2572 | break; |
| 2573 | case te_polar: |
| 2574 | emit->append( instr = new Chuck_Instr_Divide_polar_Assign ); |
| 2575 | break; |
| 2576 | |
| 2577 | default: break; |
| 2578 | } |
| 2579 | } |
| 2580 | break; |
| 2581 | |
| 2582 | case ae_op_s_or: |
| 2583 | switch( left ) |
| 2584 | { |
| 2585 | case te_int: |
| 2586 | emit->append( instr = new Chuck_Instr_Binary_Or ); |
| 2587 | break; |
| 2588 | |
| 2589 | default: break; |
| 2590 | } |
| 2591 | break; |
| 2592 | |
| 2593 | case ae_op_s_or_chuck: |
| 2594 | switch( left ) |
| 2595 | { |
| 2596 | case te_int: |
| 2597 | emit->append( instr = new Chuck_Instr_Binary_Or_Assign ); |
| 2598 | break; |
| 2599 | |
| 2600 | default: break; |
| 2601 | } |
| 2602 | break; |
| 2603 | |
| 2604 | case ae_op_s_and: |
| 2605 | switch( left ) |
| 2606 | { |
| 2607 | case te_int: |
| 2608 | emit->append( instr = new Chuck_Instr_Binary_And ); |
| 2609 | break; |
| 2610 | |
| 2611 | default: break; |
| 2612 | } |
| 2613 | break; |
| 2614 | |
| 2615 | case ae_op_s_and_chuck: |
| 2616 | switch( left ) |
| 2617 | { |
| 2618 | case te_int: |
| 2619 | emit->append( instr = new Chuck_Instr_Binary_And_Assign ); |
| 2620 | break; |
| 2621 | |
| 2622 | default: break; |
| 2623 | } |
| 2624 | break; |
| 2625 | |
| 2626 | case ae_op_shift_left: |
| 2627 | switch( left ) |
| 2628 | { |
| 2629 | case te_int: |
| 2630 | emit->append( instr = new Chuck_Instr_Binary_Shift_Left ); |
| 2631 | break; |
| 2632 | |
| 2633 | case te_array: |
| 2634 | { |
| 2635 | // check size (1.3.1.0: changed to getkindof) |
| 2636 | t_CKUINT kind = getkindof( emit->env, t_left->array_type ); |
| 2637 | // but if array depth > 1, we are actually dealing with pointers, hence back to INT |
| 2638 | if( t_left->array_depth > 1 ) kind = kindof_INT; |
| 2639 | emit->append( instr = new Chuck_Instr_Array_Append( kind ) ); |
| 2640 | instr->set_linepos( lhs->line ); |
| 2641 | break; |
| 2642 | } |
| 2643 | |
| 2644 | default: break; |
| 2645 | } |
| 2646 | break; |
| 2647 | |
| 2648 | case ae_op_shift_left_chuck: |
| 2649 | // reverse |
| 2650 | switch( left ) |
| 2651 | { |
| 2652 | case te_int: |
| 2653 | emit->append( instr = new Chuck_Instr_Binary_Shift_Left_Assign ); |
| 2654 | break; |
| 2655 | |
| 2656 | default: break; |
| 2657 | } |
| 2658 | break; |
| 2659 | |
| 2660 | case ae_op_shift_right: |
| 2661 | switch( left ) |
| 2662 | { |
| 2663 | case te_int: |
| 2664 | emit->append( instr = new Chuck_Instr_Binary_Shift_Right ); |
| 2665 | break; |
| 2666 | |
| 2667 | // case te_array: |
| 2668 | // emit->append( instr = new Chuck_Instr_Array_Prepend( t_left->array_type->size ) ); |
| 2669 | // break; |
| 2670 | |
| 2671 | default: break; |
| 2672 | } |
| 2673 | break; |
| 2674 | |
| 2675 | case ae_op_shift_right_chuck: |
| 2676 | // reverse |
| 2677 | switch( left ) |
| 2678 | { |
| 2679 | case te_int: |
| 2680 | emit->append( instr = new Chuck_Instr_Binary_Shift_Right_Assign ); |
| 2681 | break; |
| 2682 | |
| 2683 | default: break; |
| 2684 | } |
| 2685 | break; |
| 2686 | |
| 2687 | case ae_op_percent: |
| 2688 | if( ( left == te_time && right == te_dur ) ) // time % dur = dur |
| 2689 | { |
| 2690 | emit->append( instr = new Chuck_Instr_Mod_double ); |
| 2691 | instr->set_linepos( rhs->line ); |
| 2692 | } |
| 2693 | else // other types |
| 2694 | { |
| 2695 | switch( left ) |
| 2696 | { |
| 2697 | case te_int: |
| 2698 | emit->append( instr = new Chuck_Instr_Mod_int ); |
| 2699 | instr->set_linepos( rhs->line ); |
| 2700 | break; |
| 2701 | case te_float: |
| 2702 | case te_dur: |
| 2703 | emit->append( instr = new Chuck_Instr_Mod_double ); |
| 2704 | instr->set_linepos( rhs->line ); |
| 2705 | break; |
| 2706 | |
| 2707 | default: break; |
| 2708 | } |
| 2709 | } |
| 2710 | break; |
| 2711 | |
| 2712 | case ae_op_percent_chuck: |
| 2713 | // reverse |
| 2714 | if( ( left == te_dur && right == te_time ) ) // time % dur = dur |
| 2715 | { |
| 2716 | emit->append( instr = new Chuck_Instr_Mod_double_Assign ); |
| 2717 | instr->set_linepos( lhs->line ); |
| 2718 | } |
| 2719 | else // other types |
| 2720 | { |
| 2721 | switch( left ) |
| 2722 | { |
| 2723 | case te_int: |
| 2724 | emit->append( instr = new Chuck_Instr_Mod_int_Assign ); |
| 2725 | instr->set_linepos( lhs->line ); |
| 2726 | break; |
| 2727 | case te_float: |
| 2728 | case te_dur: |
| 2729 | emit->append( instr = new Chuck_Instr_Mod_double_Assign ); |
| 2730 | instr->set_linepos( lhs->line ); |
| 2731 | break; |
| 2732 | |
| 2733 | default: break; |
| 2734 | } |
| 2735 | } |
| 2736 | break; |
| 2737 | |
| 2738 | case ae_op_s_xor: |
| 2739 | switch( left ) |
| 2740 | { |
| 2741 | case te_int: |
| 2742 | emit->append( instr = new Chuck_Instr_Binary_Xor ); |
| 2743 | break; |
| 2744 | default: break; |
| 2745 | } |
| 2746 | break; |
| 2747 | |
| 2748 | case ae_op_s_xor_chuck: |
| 2749 | switch( left ) |
| 2750 | { |
| 2751 | case te_int: |
| 2752 | emit->append( instr = new Chuck_Instr_Binary_Xor_Assign ); |
| 2753 | break; |
| 2754 | default: break; |
| 2755 | } |
| 2756 | break; |
| 2757 | |
| 2758 | // ----------------------------- chuck ------------------------------------- |
| 2759 | case ae_op_chuck: |
| 2760 | { |
| 2761 | a_Exp cl = lhs, cr = rhs; |
| 2762 | |
| 2763 | if( !emit_engine_emit_op_chuck( emit, cl, cr, binary ) ) |
| 2764 | return FALSE; |
| 2765 | |
| 2766 | return TRUE; |
| 2767 | } |
| 2768 | |
| 2769 | case ae_op_unchuck: |
| 2770 | { |
| 2771 | a_Exp cl = lhs, cr = rhs; |
| 2772 | |
| 2773 | // TODO: cross chuck |
| 2774 | assert( cl->next == NULL && cr->next == NULL ); |
| 2775 | while( cr ) |
| 2776 | { |
| 2777 | cl = lhs; |
| 2778 | while( cl ) |
| 2779 | { |
| 2780 | if( !emit_engine_emit_op_unchuck( emit, cl, cr ) ) |
| 2781 | return FALSE; |
| 2782 | cl = cl->next; |
| 2783 | } |
| 2784 | |
| 2785 | cr = cr->next; |
| 2786 | } |
| 2787 | |
| 2788 | return TRUE; |
| 2789 | } |
| 2790 | |
| 2791 | case ae_op_upchuck: |
| 2792 | { |
| 2793 | a_Exp cl = lhs, cr = rhs; |
| 2794 | |
| 2795 | // TODO: cross chuck |
| 2796 | assert( cl->next == NULL && cr->next == NULL ); |
| 2797 | while( cr ) |
| 2798 | { |
| 2799 | cl = lhs; |
| 2800 | while( cl ) |
| 2801 | { |
| 2802 | if( !emit_engine_emit_op_upchuck( emit, cl, cr ) ) |
| 2803 | return FALSE; |
| 2804 | cl = cl->next; |
| 2805 | } |
| 2806 | |
| 2807 | cr = cr->next; |
| 2808 | } |
| 2809 | |
| 2810 | return TRUE; |
| 2811 | } |
| 2812 | |
| 2813 | case ae_op_at_chuck: |
| 2814 | { |
| 2815 | a_Exp cl = lhs, cr = rhs; |
| 2816 | |
| 2817 | // TODO: cross chuck |
| 2818 | assert( cl->next == NULL && cr->next == NULL ); |
| 2819 | while( cr ) |
| 2820 | { |
| 2821 | cl = lhs; |
| 2822 | while( cl ) |
| 2823 | { |
| 2824 | if( !emit_engine_emit_op_at_chuck( emit, cl, cr ) ) |
| 2825 | return FALSE; |
| 2826 | cl = cl->next; |
| 2827 | } |
| 2828 | |
| 2829 | cr = cr->next; |
| 2830 | } |
| 2831 | |
| 2832 | return TRUE; |
| 2833 | } |
| 2834 | |
| 2835 | // -------------------------------- bool ----------------------------------- |
| 2836 | case ae_op_eq: |
| 2837 | // if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) |
| 2838 | // && !isa( t_left, emit->env->ckt_null ) && !isa( t_right, emit->env->ckt_null ) ) // !null |
| 2839 | // { |
| 2840 | // emit->append( instr = new Chuck_Instr_Op_string( op ) ); |
| 2841 | // instr->set_linepos( lhs->line ); |
| 2842 | // } else |
| 2843 | if( ( isa( t_left, emit->env->ckt_object ) && isa( t_right, emit->env->ckt_object ) ) || |
| 2844 | ( isa( t_left, emit->env->ckt_object ) && isnull( emit->env, t_right ) ) || |
| 2845 | ( isnull( emit->env,t_left ) && isa( t_right, emit->env->ckt_object ) ) || |
| 2846 | ( isnull( emit->env,t_left ) && isnull( emit->env, t_right ) ) ) |
| 2847 | { |
| 2848 | // object reference compare |
| 2849 | emit->append( instr = new Chuck_Instr_Eq_int ); |
| 2850 | } |
| 2851 | else |
| 2852 | { |
| 2853 | switch( left ) |
| 2854 | { |
| 2855 | case te_int: |
| 2856 | emit->append( instr = new Chuck_Instr_Eq_int ); |
| 2857 | break; |
| 2858 | case te_float: |
| 2859 | case te_dur: |
| 2860 | case te_time: |
| 2861 | emit->append( instr = new Chuck_Instr_Eq_double ); |
| 2862 | break; |
| 2863 | case te_complex: |
| 2864 | case te_polar: |
| 2865 | emit->append( instr = new Chuck_Instr_Eq_complex ); |
| 2866 | break; |
| 2867 | case te_vec2: |
| 2868 | emit->append( instr = new Chuck_Instr_Eq_vec2 ); |
| 2869 | break; |
| 2870 | case te_vec3: |
| 2871 | emit->append( instr = new Chuck_Instr_Eq_vec3 ); |
| 2872 | break; |
| 2873 | case te_vec4: |
| 2874 | emit->append( instr = new Chuck_Instr_Eq_vec4 ); |
| 2875 | break; |
| 2876 | |
| 2877 | default: break; |
| 2878 | } |
| 2879 | } |
| 2880 | break; |
| 2881 | |
| 2882 | case ae_op_neq: |
| 2883 | // if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) |
| 2884 | // && !isa( t_left, emit->env->ckt_null ) && !isa( t_right, emit->env->ckt_null ) ) // !null |
| 2885 | // // added 1.3.2.0 (spencer) |
| 2886 | // { |
| 2887 | // emit->append( instr = new Chuck_Instr_Op_string( op ) ); |
| 2888 | // instr->set_linepos( lhs->line ); |
| 2889 | // } else |
| 2890 | if( ( isa( t_left, emit->env->ckt_object ) && isa( t_right, emit->env->ckt_object ) ) || |
| 2891 | ( isa( t_left, emit->env->ckt_object ) && isnull( emit->env, t_right ) ) || |
| 2892 | ( isnull( emit->env,t_left ) && isa( t_right, emit->env->ckt_object ) ) || |
| 2893 | ( isnull( emit->env,t_left ) && isnull( emit->env, t_right ) ) ) |
| 2894 | { |
| 2895 | emit->append( instr = new Chuck_Instr_Neq_int ); |
| 2896 | } |
| 2897 | else |
| 2898 | { |
| 2899 | switch( left ) |
| 2900 | { |
| 2901 | case te_int: |
| 2902 | emit->append( instr = new Chuck_Instr_Neq_int ); |
| 2903 | break; |
| 2904 | case te_float: |
| 2905 | case te_dur: |
| 2906 | case te_time: |
| 2907 | emit->append( instr = new Chuck_Instr_Neq_double ); |
| 2908 | break; |
| 2909 | case te_complex: |
| 2910 | case te_polar: |
| 2911 | emit->append( instr = new Chuck_Instr_Neq_complex ); |
| 2912 | break; |
| 2913 | case te_vec2: |
| 2914 | emit->append( instr = new Chuck_Instr_Neq_vec2 ); |
| 2915 | break; |
| 2916 | case te_vec3: |
| 2917 | emit->append( instr = new Chuck_Instr_Neq_vec3 ); |
| 2918 | break; |
| 2919 | case te_vec4: |
| 2920 | emit->append( instr = new Chuck_Instr_Neq_vec4 ); |
| 2921 | break; |
| 2922 | |
| 2923 | default: break; |
| 2924 | } |
| 2925 | } |
| 2926 | break; |
| 2927 | |
| 2928 | case ae_op_lt: |
| 2929 | switch( left ) |
| 2930 | { |
| 2931 | case te_int: |
| 2932 | emit->append( instr = new Chuck_Instr_Lt_int ); |
| 2933 | break; |
| 2934 | case te_float: |
| 2935 | case te_dur: |
| 2936 | case te_time: |
| 2937 | emit->append( instr = new Chuck_Instr_Lt_double ); |
| 2938 | break; |
| 2939 | |
| 2940 | default: |
| 2941 | // if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) ) |
| 2942 | // { |
| 2943 | // emit->append( instr = new Chuck_Instr_Op_string( op ) ); |
| 2944 | // instr->set_linepos( lhs->line ); |
| 2945 | // } |
| 2946 | break; |
| 2947 | } |
| 2948 | break; |
| 2949 | |
| 2950 | case ae_op_le: |
| 2951 | switch( left ) |
| 2952 | { |
| 2953 | case te_int: |
| 2954 | emit->append( instr = new Chuck_Instr_Le_int ); |
| 2955 | break; |
| 2956 | case te_float: |
| 2957 | case te_dur: |
| 2958 | case te_time: |
| 2959 | emit->append( instr = new Chuck_Instr_Le_double ); |
| 2960 | break; |
| 2961 | |
| 2962 | default: |
| 2963 | // if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) ) |
| 2964 | // { |
| 2965 | // emit->append( instr = new Chuck_Instr_Op_string( op ) ); |
| 2966 | // instr->set_linepos( lhs->line ); |
| 2967 | // } else |
| 2968 | if( isa( t_left, emit->env->ckt_io ) ) |
| 2969 | { |
| 2970 | // output |
| 2971 | if( isa( t_right, emit->env->ckt_int ) ) |
| 2972 | { |
| 2973 | emit->append( instr = new Chuck_Instr_IO_out_int ); |
| 2974 | instr->set_linepos( rhs->line ); |
| 2975 | } |
| 2976 | else if( isa( t_right, emit->env->ckt_float ) ) |
| 2977 | { |
| 2978 | emit->append( instr = new Chuck_Instr_IO_out_float ); |
| 2979 | instr->set_linepos( rhs->line ); |
| 2980 | } |
| 2981 | else if( isa( t_right, emit->env->ckt_string ) ) |
| 2982 | { |
| 2983 | emit->append( instr = new Chuck_Instr_IO_out_string ); |
| 2984 | instr->set_linepos( rhs->line ); |
| 2985 | } |
| 2986 | else if( isa( t_right, emit->env->ckt_complex ) ) |
| 2987 | { |
| 2988 | emit->append( instr = new Chuck_Instr_IO_out_complex ); |
| 2989 | instr->set_linepos( rhs->line ); |
| 2990 | } |
| 2991 | else if( isa( t_right, emit->env->ckt_polar ) ) |
| 2992 | { |
| 2993 | emit->append( instr = new Chuck_Instr_IO_out_polar ); |
| 2994 | instr->set_linepos( rhs->line ); |
| 2995 | } |
| 2996 | else if( isa( t_right, emit->env->ckt_vec2 ) ) |
| 2997 | { |
| 2998 | emit->append( instr = new Chuck_Instr_IO_out_vec2 ); |
| 2999 | instr->set_linepos( rhs->line ); |
| 3000 | } |
| 3001 | else if( isa( t_right, emit->env->ckt_vec3 ) ) |
| 3002 | { |
| 3003 | emit->append( instr = new Chuck_Instr_IO_out_vec3 ); |
| 3004 | instr->set_linepos( rhs->line ); |
| 3005 | } |
| 3006 | else if( isa( t_right, emit->env->ckt_vec4 ) ) |
| 3007 | { |
| 3008 | emit->append( instr = new Chuck_Instr_IO_out_vec4 ); |
| 3009 | instr->set_linepos( rhs->line ); |
| 3010 | } |
| 3011 | } |
| 3012 | break; |
| 3013 | } |
| 3014 | break; |
| 3015 | |
| 3016 | case ae_op_gt: |
| 3017 | switch( left ) |
| 3018 | { |
| 3019 | case te_int: |
| 3020 | emit->append( instr = new Chuck_Instr_Gt_int ); |
| 3021 | break; |
| 3022 | case te_float: |
| 3023 | case te_dur: |
| 3024 | case te_time: |
| 3025 | emit->append( instr = new Chuck_Instr_Gt_double ); |
| 3026 | break; |
| 3027 | |
| 3028 | default: |
| 3029 | // if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) ) |
| 3030 | // { |
| 3031 | // emit->append( instr = new Chuck_Instr_Op_string( op ) ); |
| 3032 | // instr->set_linepos( lhs->line ); |
| 3033 | // } |
| 3034 | break; |
| 3035 | } |
| 3036 | break; |
| 3037 | |
| 3038 | case ae_op_ge: |
| 3039 | switch( left ) |
| 3040 | { |
| 3041 | case te_int: |
| 3042 | emit->append( instr = new Chuck_Instr_Ge_int ); |
| 3043 | break; |
| 3044 | case te_float: |
| 3045 | case te_dur: |
| 3046 | case te_time: |
| 3047 | emit->append( instr = new Chuck_Instr_Ge_double ); |
| 3048 | break; |
| 3049 | |
| 3050 | default: |
| 3051 | // if( isa( t_left, emit->env->ckt_string ) && isa( t_right, emit->env->ckt_string ) ) |
| 3052 | // { |
| 3053 | // emit->append( instr = new Chuck_Instr_Op_string( op ) ); |
| 3054 | // instr->set_linepos( lhs->line ); |
| 3055 | // } |
| 3056 | break; |
| 3057 | } |
| 3058 | break; |
| 3059 | |
| 3060 | case ae_op_and: |
| 3061 | switch( left ) |
| 3062 | { |
| 3063 | case te_int: |
| 3064 | case te_object: |
| 3065 | case te_user: |
| 3066 | emit->append( instr = new Chuck_Instr_And ); |
| 3067 | break; |
| 3068 | |
| 3069 | default: break; |
| 3070 | } |
| 3071 | break; |
| 3072 | |
| 3073 | case ae_op_or: |
| 3074 | switch( left ) |
| 3075 | { |
| 3076 | case te_int: |
| 3077 | case te_object: |
| 3078 | case te_user: |
| 3079 | emit->append( instr = new Chuck_Instr_Or ); |
| 3080 | break; |
| 3081 | |
| 3082 | default: break; |
| 3083 | } |
| 3084 | break; |
| 3085 | |
| 3086 | //------------------------------- IO ---------------------------------- |
| 3087 | case ae_op_arrow_left: |
| 3088 | switch( right ) |
| 3089 | { |
| 3090 | case te_int: |
| 3091 | emit->append( instr = new Chuck_Instr_IO_out_int ); |
| 3092 | instr->set_linepos( rhs->line ); |
| 3093 | break; |
| 3094 | case te_float: |
| 3095 | emit->append( instr = new Chuck_Instr_IO_out_float ); |
| 3096 | instr->set_linepos( rhs->line ); |
| 3097 | break; |
| 3098 | default: |
| 3099 | if( isa( t_right, emit->env->ckt_string ) ) |
| 3100 | { |
| 3101 | emit->append( instr = new Chuck_Instr_IO_out_string ); |
| 3102 | instr->set_linepos( rhs->line ); |
| 3103 | } |
| 3104 | break; |
| 3105 | } |
| 3106 | break; |
| 3107 | |
| 3108 | case ae_op_arrow_right: |
| 3109 | switch( right ) |
| 3110 | { |
| 3111 | case te_int: |
| 3112 | emit->append( instr = new Chuck_Instr_IO_in_int ); |
| 3113 | instr->set_linepos( rhs->line ); |
| 3114 | break; |
| 3115 | case te_float: |
| 3116 | emit->append( instr = new Chuck_Instr_IO_in_float ); |
| 3117 | instr->set_linepos( rhs->line ); |
| 3118 | break; |
| 3119 | default: |
| 3120 | if( isa( t_right, emit->env->ckt_string ) ) |
| 3121 | { |
| 3122 | emit->append( instr = new Chuck_Instr_IO_in_string ); |
| 3123 | instr->set_linepos( rhs->line ); |
| 3124 | } |
| 3125 | break; |
| 3126 | } |
| 3127 | break; |
| 3128 | |
| 3129 | //---------------------------- (error) -------------------------------- |
| 3130 | default: break; |
| 3131 | EM_error2( binary->where, |
| 3132 | "(emit): internal error: unhandled op '%s' %s '%s'", |
| 3133 | t_left->c_name(), op2str(op), t_right->c_name() ); |
| 3134 | return FALSE; |
| 3135 | } |
| 3136 | |
| 3137 | // make sure emit |
| 3138 | if( !instr ) |
| 3139 | { |
| 3140 | EM_error2( binary->where, |
| 3141 | "(emit): internal error: unhandled op implementation '%s' %s '%s'", |
| 3142 | t_left->c_name(), op2str( op ), t_right->c_name() ); |
| 3143 | return FALSE; |
| 3144 | } |
| 3145 | |
| 3146 | return TRUE; |
| 3147 | } |
| 3148 | |
| 3149 | |
| 3150 | |
| 3151 | |
| 3152 | //----------------------------------------------------------------------------- |
| 3153 | // name: emit_engine_emit_stmt_remember_object() | 1.5.1.8 |
| 3154 | // desc: as needed (if type is Object), emit a Stmt_Remember_Object instruction |
| 3155 | // assuming that the greater Stmt_Start to Stmt_Cleanup mechanism is |
| 3156 | // handled correctly elsewhere |
| 3157 | //----------------------------------------------------------------------------- |
| 3158 | t_CKBOOL emit_engine_emit_stmt_remember_object( Chuck_Emitter * emit, Chuck_Type * type ) |
| 3159 | { |
| 3160 | // if the function returns an Object |
| 3161 | if( !isobj( emit->env, type ) ) |
| 3162 | return FALSE; |
| 3163 | |
| 3164 | // the return needs to be released (later at the end of the stmt that contains this) |
| 3165 | Chuck_Instr_Stmt_Start * onStack = emit->stmt_stack.size() ? emit->stmt_stack.back() : NULL; |
| 3166 | // check it |
| 3167 | if( onStack ) |
| 3168 | { |
| 3169 | t_CKUINT offset = 0; |
| 3170 | // acquire next offset |
| 3171 | if( !onStack->nextOffset( offset ) ) return FALSE; |
| 3172 | // append instruction |
| 3173 | emit->append( new Chuck_Instr_Stmt_Remember_Object( onStack, offset ) ); |
| 3174 | } |
| 3175 | |
| 3176 | return TRUE; |
| 3177 | } |
| 3178 | |
| 3179 | |
| 3180 | |
| 3181 | |
| 3182 | //----------------------------------------------------------------------------- |
| 3183 | // name: emit_engine_emit_op_overload_binary() | 1.5.1.5 (ge) added |
| 3184 | // desc: emit binary operator overload |
| 3185 | //----------------------------------------------------------------------------- |
| 3186 | t_CKBOOL emit_engine_emit_op_overload_binary( Chuck_Emitter * emit, a_Exp_Binary binary ) |
| 3187 | { |
| 3188 | // TODO: transforms local stack into args; add refs, variable mem to reg, etc. |
| 3189 | // push function pointer |
| 3190 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)binary->ck_overload_func ) ); |
| 3191 | // emit the function call |
| 3192 | if( !emit_engine_emit_exp_func_call( emit, binary->ck_overload_func, binary->self->type, binary->line, binary->where ) ) |
| 3193 | return FALSE; |
| 3194 | |
| 3195 | // return type |
| 3196 | Chuck_Type * rtype = binary->ck_overload_func->type(); |
| 3197 | // emit remember instr | 1.5.1.8 |
| 3198 | if( isobj( emit->env, rtype ) ) emit_engine_emit_stmt_remember_object( emit, rtype ); |
| 3199 | |
| 3200 | // done |
| 3201 | return TRUE; |
| 3202 | } |
| 3203 | |
| 3204 | |
| 3205 | |
| 3206 | |
| 3207 | //----------------------------------------------------------------------------- |
| 3208 | // name: emit_engine_emit_op_overload_unary() | 1.5.1.5 (ge) added |
| 3209 | // desc: emit unary (prefix) operator overload |
| 3210 | //----------------------------------------------------------------------------- |
| 3211 | t_CKBOOL emit_engine_emit_op_overload_unary( Chuck_Emitter * emit, a_Exp_Unary unary ) |
| 3212 | { |
| 3213 | // TODO: transforms local stack into args; add refs, variable mem to reg, etc. |
| 3214 | // push function pointer |
| 3215 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)unary->ck_overload_func ) ); |
| 3216 | // emit the function call |
| 3217 | if( !emit_engine_emit_exp_func_call( emit, unary->ck_overload_func, unary->self->type, unary->line, unary->where ) ) |
| 3218 | return FALSE; |
| 3219 | |
| 3220 | // return type |
| 3221 | Chuck_Type * rtype = unary->ck_overload_func->type(); |
| 3222 | // emit remember instr | 1.5.1.8 |
| 3223 | if( isobj( emit->env, rtype ) ) emit_engine_emit_stmt_remember_object( emit, rtype ); |
| 3224 | |
| 3225 | // done |
| 3226 | return TRUE; |
| 3227 | } |
| 3228 | |
| 3229 | |
| 3230 | |
| 3231 | |
| 3232 | //----------------------------------------------------------------------------- |
| 3233 | // name: emit_engine_emit_op_overload_postfix() | 1.5.1.5 (ge) added |
| 3234 | // desc: emit unary (postfix) operator overload |
| 3235 | //----------------------------------------------------------------------------- |
| 3236 | t_CKBOOL emit_engine_emit_op_overload_postfix( Chuck_Emitter * emit, a_Exp_Postfix postfix ) |
| 3237 | { |
| 3238 | // TODO: transforms local stack into args; add refs, variable mem to reg, etc. |
| 3239 | // push function pointer |
| 3240 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)postfix->ck_overload_func ) ); |
| 3241 | // emit the function call |
| 3242 | if( !emit_engine_emit_exp_func_call( emit, postfix->ck_overload_func, postfix->self->type, postfix->line, postfix->where ) ) |
| 3243 | return FALSE; |
| 3244 | |
| 3245 | // return type |
| 3246 | Chuck_Type * rtype = postfix->ck_overload_func->type(); |
| 3247 | // emit remember instr | 1.5.1.8 |
| 3248 | if( isobj( emit->env, rtype ) ) emit_engine_emit_stmt_remember_object( emit, rtype ); |
| 3249 | |
| 3250 | // done |
| 3251 | return TRUE; |
| 3252 | } |
| 3253 | |
| 3254 | |
| 3255 | |
| 3256 | |
| 3257 | //----------------------------------------------------------------------------- |
| 3258 | // name: emit_engine_emit_op_chuck() |
| 3259 | // desc: emit => |
| 3260 | //----------------------------------------------------------------------------- |
| 3261 | t_CKBOOL emit_engine_emit_op_chuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs, a_Exp_Binary binary ) |
| 3262 | { |
| 3263 | // consistency check | 1.5.4.3 (ge) added |
| 3264 | assert( binary->op == ae_op_chuck ); |
| 3265 | |
| 3266 | // any implicit cast happens before this |
| 3267 | Chuck_Type * left = lhs->cast_to ? lhs->cast_to : lhs->type; |
| 3268 | Chuck_Type * right = rhs->cast_to ? rhs->cast_to : rhs->type; |
| 3269 | |
| 3270 | // the pointer |
| 3271 | Chuck_Instr * instr = NULL; |
| 3272 | |
| 3273 | // ugen => ugen |
| 3274 | if( isa( left, emit->env->ckt_ugen ) && isa( right, emit->env->ckt_ugen ) ) |
| 3275 | { |
| 3276 | // link, flag as NOT upchuck |
| 3277 | emit->append( instr = new Chuck_Instr_UGen_Link( FALSE ) ); |
| 3278 | instr->set_linepos( lhs->line ); |
| 3279 | // done |
| 3280 | return TRUE; |
| 3281 | } |
| 3282 | |
| 3283 | // ugen[] => ugen[] (or permutation) |
| 3284 | if( ( isa( left, emit->env->ckt_ugen ) || ( isa( left, emit->env->ckt_array ) && isa( left->array_type, emit->env->ckt_ugen ) ) ) && |
| 3285 | ( isa( right, emit->env->ckt_ugen ) || ( isa( right, emit->env->ckt_array ) && isa( right->array_type, emit->env->ckt_ugen ) ) ) ) |
| 3286 | { |
| 3287 | // link, flag as NOT upchuck |
| 3288 | emit->append( instr = new Chuck_Instr_UGen_Array_Link( isa( left, emit->env->ckt_array ), isa( right, emit->env->ckt_array ), FALSE ) ); |
| 3289 | instr->set_linepos( lhs->line ); |
| 3290 | // done |
| 3291 | return TRUE; |
| 3292 | } |
| 3293 | |
| 3294 | // time advance |
| 3295 | if( isa( left, emit->env->ckt_dur ) && isa( right, emit->env->ckt_time ) && rhs->s_meta == ae_meta_var ) |
| 3296 | { |
| 3297 | // add the two |
| 3298 | emit->append( new Chuck_Instr_Add_double ); |
| 3299 | |
| 3300 | // see if rhs is 'now' |
| 3301 | if( rhs->s_type == ae_exp_primary && !strcmp( "now", S_name(rhs->primary.var) ) ) |
| 3302 | { |
| 3303 | // advance time |
| 3304 | emit->append( instr = new Chuck_Instr_Time_Advance ); |
| 3305 | instr->set_linepos( lhs->line ); |
| 3306 | } |
| 3307 | |
| 3308 | return TRUE; |
| 3309 | } |
| 3310 | |
| 3311 | // time advance |
| 3312 | if( isa( left, emit->env->ckt_event ) && isa( right, emit->env->ckt_time ) && rhs->s_meta == ae_meta_var && |
| 3313 | rhs->s_type == ae_exp_primary && !strcmp( "now", S_name(rhs->primary.var) ) ) |
| 3314 | { |
| 3315 | // pop now |
| 3316 | emit->append( new Chuck_Instr_Reg_Pop_Float ); |
| 3317 | emit->append( instr = new Chuck_Instr_Event_Wait ); |
| 3318 | instr->set_linepos( lhs->line ); |
| 3319 | |
| 3320 | return TRUE; |
| 3321 | } |
| 3322 | |
| 3323 | // input |
| 3324 | if( isa( left, emit->env->ckt_io ) ) |
| 3325 | { |
| 3326 | if( isa( right, emit->env->ckt_int ) ) |
| 3327 | { |
| 3328 | assert( rhs->s_meta == ae_meta_var ); |
| 3329 | emit->append( instr = new Chuck_Instr_IO_in_int ); |
| 3330 | instr->set_linepos( rhs->line ); |
| 3331 | return TRUE; |
| 3332 | } |
| 3333 | else if( isa( right, emit->env->ckt_float ) ) |
| 3334 | { |
| 3335 | assert( rhs->s_meta == ae_meta_var ); |
| 3336 | emit->append( instr = new Chuck_Instr_IO_in_float ); |
| 3337 | instr->set_linepos( rhs->line ); |
| 3338 | return TRUE; |
| 3339 | } |
| 3340 | else if( isa( right, emit->env->ckt_string ) ) |
| 3341 | { |
| 3342 | assert( rhs->s_meta == ae_meta_var ); |
| 3343 | emit->append( instr = new Chuck_Instr_IO_in_string ); |
| 3344 | instr->set_linepos( rhs->line ); |
| 3345 | return TRUE; |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | // func call |
| 3350 | // make sure not 'null' which also looks like any object | 1.5.1.7 |
| 3351 | // if( isa( right, emit->env->ckt_function ) && !isnull(emit->env,right) ) |
| 3352 | if( type_engine_binary_is_func_call( emit->env, binary->op, lhs, rhs ) ) |
| 3353 | { |
| 3354 | assert( binary->ck_func != NULL ); |
| 3355 | |
| 3356 | // emit |
| 3357 | return emit_engine_emit_exp_func_call( emit, binary->ck_func, binary->self->type, binary->line, binary->where ); |
| 3358 | } |
| 3359 | |
| 3360 | // assignment or something else |
| 3361 | if( isa( left, right ) ) |
| 3362 | { |
| 3363 | // basic types? |
| 3364 | if( type_engine_check_primitive( emit->env, left ) || isa( left, emit->env->ckt_string ) ) |
| 3365 | { |
| 3366 | // use at assign |
| 3367 | return emit_engine_emit_op_at_chuck( emit, lhs, rhs ); |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | // TODO: deal with const |
| 3372 | |
| 3373 | // no match |
| 3374 | EM_error2( lhs->where, |
| 3375 | "(emit): internal error: unhandled '=>' on types '%s' and '%s'...", |
| 3376 | left->c_name(), right->c_name() ); |
| 3377 | |
| 3378 | return FALSE; |
| 3379 | } |
| 3380 | |
| 3381 | |
| 3382 | |
| 3383 | |
| 3384 | //----------------------------------------------------------------------------- |
| 3385 | // name: emit_engine_emit_op_unchuck() |
| 3386 | // desc: ... |
| 3387 | //----------------------------------------------------------------------------- |
| 3388 | t_CKBOOL emit_engine_emit_op_unchuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs ) |
| 3389 | { |
| 3390 | // any implicit cast happens before this |
| 3391 | Chuck_Type * left = lhs->cast_to ? lhs->cast_to : lhs->type; |
| 3392 | Chuck_Type * right = rhs->cast_to ? rhs->cast_to : rhs->type; |
| 3393 | |
| 3394 | // if ugen |
| 3395 | if( isa( left, emit->env->ckt_ugen ) && isa( right, emit->env->ckt_ugen ) ) |
| 3396 | { |
| 3397 | // no connect |
| 3398 | emit->append( new Chuck_Instr_UGen_UnLink ); |
| 3399 | } |
| 3400 | else |
| 3401 | { |
| 3402 | EM_error2( lhs->where, |
| 3403 | "(emit): internal error: unhandled '=<' on types '%s' and '%s'", |
| 3404 | left->c_name(), right->c_name() ); |
| 3405 | return FALSE; |
| 3406 | } |
| 3407 | |
| 3408 | return TRUE; |
| 3409 | } |
| 3410 | |
| 3411 | |
| 3412 | |
| 3413 | |
| 3414 | //----------------------------------------------------------------------------- |
| 3415 | // name: emit_engine_emit_op_upchuck() |
| 3416 | // desc: ... |
| 3417 | //----------------------------------------------------------------------------- |
| 3418 | t_CKBOOL emit_engine_emit_op_upchuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs ) |
| 3419 | { |
| 3420 | // any implicit cast happens before this |
| 3421 | Chuck_Type * left = lhs->cast_to ? lhs->cast_to : lhs->type; |
| 3422 | Chuck_Type * right = rhs->cast_to ? rhs->cast_to : rhs->type; |
| 3423 | |
| 3424 | // if ugen |
| 3425 | if( isa( left, emit->env->ckt_uana ) && isa( right, emit->env->ckt_uana ) ) |
| 3426 | { |
| 3427 | // connect; flag it as unchuck |
| 3428 | Chuck_Instr * instr = NULL; |
| 3429 | emit->append( instr = new Chuck_Instr_UGen_Link( TRUE ) ); |
| 3430 | instr->set_linepos( lhs->line ); |
| 3431 | } |
| 3432 | // uana[] =^ uana[] (or permutation) |
| 3433 | else if( ( isa( left, emit->env->ckt_uana ) || ( isa( left, emit->env->ckt_array ) && isa( left->array_type, emit->env->ckt_uana ) ) ) && |
| 3434 | ( isa( right, emit->env->ckt_uana ) || ( isa( right, emit->env->ckt_array ) && isa( right->array_type, emit->env->ckt_uana ) ) ) ) |
| 3435 | { |
| 3436 | // link, flag as upchuck |
| 3437 | Chuck_Instr * instr = NULL; |
| 3438 | emit->append( instr = new Chuck_Instr_UGen_Array_Link( isa( left, emit->env->ckt_array ), isa( right, emit->env->ckt_array ), TRUE ) ); |
| 3439 | instr->set_linepos( lhs->line ); |
| 3440 | // done |
| 3441 | return TRUE; |
| 3442 | } |
| 3443 | else |
| 3444 | { |
| 3445 | EM_error2( lhs->where, |
| 3446 | "(emit): internal error: unhandled '=^' on types '%s' and '%s'", |
| 3447 | left->c_name(), right->c_name() ); |
| 3448 | return FALSE; |
| 3449 | } |
| 3450 | |
| 3451 | return TRUE; |
| 3452 | } |
| 3453 | |
| 3454 | |
| 3455 | |
| 3456 | |
| 3457 | //----------------------------------------------------------------------------- |
| 3458 | // name: emit_engine_emit_op_at_chuck() |
| 3459 | // desc: ... |
| 3460 | //----------------------------------------------------------------------------- |
| 3461 | t_CKBOOL emit_engine_emit_op_at_chuck( Chuck_Emitter * emit, a_Exp lhs, a_Exp rhs ) |
| 3462 | { |
| 3463 | // any implicit cast happens before this |
| 3464 | Chuck_Type * left = lhs->cast_to ? lhs->cast_to : lhs->type; |
| 3465 | Chuck_Type * right = rhs->cast_to ? rhs->cast_to : rhs->type; |
| 3466 | |
| 3467 | // assignment or something else |
| 3468 | if( isa( left, right ) ) |
| 3469 | { |
| 3470 | // basic types? |
| 3471 | // 1.5.4.4 (ge) updated to isa( right, emit->env->ckt_string ) instead of left |
| 3472 | // in case left is null; right can't be null since null should be const |
| 3473 | if( type_engine_check_primitive( emit->env, left ) || isa( right, emit->env->ckt_string ) ) |
| 3474 | { |
| 3475 | // assigment? |
| 3476 | if( rhs->s_meta != ae_meta_var ) |
| 3477 | { |
| 3478 | EM_error2( lhs->where, |
| 3479 | "(emit): internal error: assignment to non-variable..." ); |
| 3480 | return FALSE; |
| 3481 | } |
| 3482 | |
| 3483 | // see if rhs is 'now' - time => now |
| 3484 | if( rhs->s_type == ae_exp_primary && !strcmp( "now", S_name(rhs->primary.var) ) ) |
| 3485 | { |
| 3486 | // pop the now addr |
| 3487 | emit->append( new Chuck_Instr_Reg_Pop_Float ); |
| 3488 | // advance time |
| 3489 | Chuck_Instr * instr = NULL; |
| 3490 | emit->append( instr = new Chuck_Instr_Time_Advance ); |
| 3491 | instr->set_linepos( lhs->line ); |
| 3492 | } |
| 3493 | // string | 1.5.4.4 (ge) updated to check right instead of left |
| 3494 | // in case left is null; right can't be null since null should be const |
| 3495 | else if( isa( right, emit->env->ckt_string ) ) |
| 3496 | { |
| 3497 | // assign string |
| 3498 | emit->append( new Chuck_Instr_Assign_String ); |
| 3499 | } |
| 3500 | else |
| 3501 | { |
| 3502 | // assign primitive |
| 3503 | // added 1.3.1.0: iskindofint -- since on some 64-bit systems, sz_INT == sz_FLOAT |
| 3504 | if( right->size == sz_INT && iskindofint(emit->env, right) ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 3505 | emit->append( new Chuck_Instr_Assign_Primitive ); |
| 3506 | else if( right->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 3507 | emit->append( new Chuck_Instr_Assign_Primitive2 ); |
| 3508 | else if( right->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 3509 | emit->append( new Chuck_Instr_Assign_Primitive4 ); |
| 3510 | else if( right->size == sz_VEC3 ) // ge: added 1.3.5.3 |
| 3511 | emit->append( new Chuck_Instr_Assign_PrimitiveVec3 ); |
| 3512 | else if( right->size == sz_VEC4 ) // ge: added 1.3.5.3 |
| 3513 | emit->append( new Chuck_Instr_Assign_PrimitiveVec4 ); |
| 3514 | else |
| 3515 | { |
| 3516 | EM_error2( rhs->where, |
| 3517 | "(emit): internal error: assignment to type of size %i...", |
| 3518 | right->size ); |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | return TRUE; |
| 3523 | } |
| 3524 | else // objects |
| 3525 | { |
| 3526 | // assign object |
| 3527 | emit->append( new Chuck_Instr_Assign_Object ); |
| 3528 | |
| 3529 | return TRUE; |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | // TODO: deal with const |
| 3534 | // 1.5.4.4 (ge) if RHS is a var, now const is checked in |
| 3535 | |
| 3536 | // no match |
| 3537 | EM_error2( lhs->where, |
| 3538 | "(emit): internal error: unhandled '@=>' on types '%s' and '%s'...", |
| 3539 | left->c_name(), right->c_name() ); |
| 3540 | |
| 3541 | return FALSE; |
| 3542 | } |
| 3543 | |
| 3544 | |
| 3545 | |
| 3546 | |
| 3547 | //----------------------------------------------------------------------------- |
| 3548 | // name: emit_engine_emit_exp_unary() |
| 3549 | // desc: emit unary (prefix) expression |
| 3550 | //----------------------------------------------------------------------------- |
| 3551 | t_CKBOOL emit_engine_emit_exp_unary( Chuck_Emitter * emit, a_Exp_Unary unary ) |
| 3552 | { |
| 3553 | // check operator overload | 1.5.1.5 (ge) |
| 3554 | t_CKBOOL op_overload = (unary->ck_overload_func != NULL); |
| 3555 | t_CKBOOL doRef = FALSE; |
| 3556 | if( op_overload && isobj( emit->env, unary->exp->type ) ) |
| 3557 | { |
| 3558 | // treat as argument |
| 3559 | doRef = TRUE; |
| 3560 | } |
| 3561 | |
| 3562 | // exp |
| 3563 | if( unary->op != ae_op_spork && !emit_engine_emit_exp( emit, unary->exp, doRef ) ) |
| 3564 | return FALSE; |
| 3565 | |
| 3566 | // get type |
| 3567 | Chuck_Type * t = unary->self->type; |
| 3568 | assert( t != NULL ); |
| 3569 | |
| 3570 | // check overloading | 1.5.1.5 (ge) added |
| 3571 | if( unary->ck_overload_func ) |
| 3572 | { |
| 3573 | // emit overloading | FYI spork can't be overloaded for now |
| 3574 | return emit_engine_emit_op_overload_unary( emit, unary ); |
| 3575 | } |
| 3576 | |
| 3577 | // emit the operator |
| 3578 | switch( unary->op ) |
| 3579 | { |
| 3580 | case ae_op_plusplus: |
| 3581 | // make sure assignment is legal |
| 3582 | if( unary->self->s_meta != ae_meta_var ) // TODO: const |
| 3583 | { |
| 3584 | EM_error2( unary->self->where, |
| 3585 | "(emit): target for '++' not mutable..." ); |
| 3586 | return FALSE; |
| 3587 | } |
| 3588 | |
| 3589 | // increment |
| 3590 | if( equals( unary->exp->type, emit->env->ckt_int ) ) |
| 3591 | { |
| 3592 | emit->append( new Chuck_Instr_PreInc_int ); |
| 3593 | } |
| 3594 | else |
| 3595 | { |
| 3596 | // internal error |
| 3597 | EM_error2( unary->where, |
| 3598 | "(emit): internal error: unhandled type '%s' for pre '++'' operator", |
| 3599 | unary->exp->type->c_name() ); |
| 3600 | return FALSE; |
| 3601 | } |
| 3602 | break; |
| 3603 | |
| 3604 | case ae_op_minusminus: |
| 3605 | // make sure assignment is legal |
| 3606 | if( unary->self->s_meta != ae_meta_var ) // TODO: const |
| 3607 | { |
| 3608 | EM_error2( unary->self->where, |
| 3609 | "(emit): target for '--' not mutable..." ); |
| 3610 | return FALSE; |
| 3611 | } |
| 3612 | |
| 3613 | // decrement |
| 3614 | if( equals( unary->exp->type, emit->env->ckt_int ) ) |
| 3615 | { |
| 3616 | emit->append( new Chuck_Instr_PreDec_int ); |
| 3617 | } |
| 3618 | else |
| 3619 | { |
| 3620 | // internal error |
| 3621 | EM_error2( unary->where, |
| 3622 | "(emit): internal error: unhandled type '%s' for pre '--' operator", |
| 3623 | unary->exp->type->c_name() ); |
| 3624 | return FALSE; |
| 3625 | } |
| 3626 | break; |
| 3627 | |
| 3628 | case ae_op_tilda: |
| 3629 | // complement |
| 3630 | if( equals( unary->exp->type, emit->env->ckt_int ) ) |
| 3631 | { |
| 3632 | emit->append( new Chuck_Instr_Complement_int ); |
| 3633 | } |
| 3634 | else |
| 3635 | { |
| 3636 | EM_error2( unary->where, |
| 3637 | "(emit): internal error: unhandled type '%s' for '~' operator", |
| 3638 | unary->exp->type->c_name() ); |
| 3639 | return FALSE; |
| 3640 | } |
| 3641 | break; |
| 3642 | |
| 3643 | case ae_op_exclamation: |
| 3644 | // ! |
| 3645 | if( equals( unary->exp->type, emit->env->ckt_int ) |
| 3646 | || |
| 3647 | isa( unary->exp->type, emit->env->ckt_object ) |
| 3648 | ) |
| 3649 | { |
| 3650 | emit->append( new Chuck_Instr_Not_int ); |
| 3651 | } |
| 3652 | else |
| 3653 | { |
| 3654 | EM_error2( unary->where, |
| 3655 | "(emit): internal error: unhandled type '%s' for '!' operator", |
| 3656 | unary->exp->type->c_name() ); |
| 3657 | return FALSE; |
| 3658 | } |
| 3659 | break; |
| 3660 | |
| 3661 | case ae_op_minus: |
| 3662 | // negate |
| 3663 | if( equals( unary->exp->type, emit->env->ckt_int ) ) |
| 3664 | emit->append( new Chuck_Instr_Negate_int ); |
| 3665 | else if( equals( unary->exp->type, emit->env->ckt_float ) ) |
| 3666 | emit->append( new Chuck_Instr_Negate_double ); |
| 3667 | else if( equals( unary->exp->type, emit->env->ckt_vec2 ) ) |
| 3668 | emit->append( new Chuck_Instr_Negate_vec2 ); |
| 3669 | else if( equals( unary->exp->type, emit->env->ckt_vec3 ) ) |
| 3670 | emit->append( new Chuck_Instr_Negate_vec3 ); |
| 3671 | else if( equals( unary->exp->type, emit->env->ckt_vec4 ) ) |
| 3672 | emit->append( new Chuck_Instr_Negate_vec4 ); |
| 3673 | else |
| 3674 | { |
| 3675 | EM_error2( unary->where, |
| 3676 | "(emit): internal error: unhandled type '%s' for unary '-' operator", |
| 3677 | unary->exp->type->c_name() ); |
| 3678 | return FALSE; |
| 3679 | } |
| 3680 | break; |
| 3681 | |
| 3682 | case ae_op_spork: |
| 3683 | // spork ~ func() |
| 3684 | if( unary->exp && unary->exp->s_type == ae_exp_func_call ) |
| 3685 | { |
| 3686 | if( !emit_engine_emit_spork( emit, &unary->exp->func_call ) ) |
| 3687 | return FALSE; |
| 3688 | } |
| 3689 | // spork ~ { ... } |
| 3690 | // else if( unary->code ) |
| 3691 | // { |
| 3692 | // if( !emit_engine_emit_spork( emit, unary->code ) ) |
| 3693 | // return FALSE; |
| 3694 | // } |
| 3695 | else |
| 3696 | { |
| 3697 | EM_error2( unary->where, |
| 3698 | "(emit): internal error: sporking non-function call..." ); |
| 3699 | return FALSE; |
| 3700 | } |
| 3701 | break; |
| 3702 | |
| 3703 | case ae_op_new: |
| 3704 | // if this is an object |
| 3705 | if( isobj( emit->env, t ) ) |
| 3706 | { |
| 3707 | // only check dependency violations if we are at a context-top-level |
| 3708 | // or class-top-level scope, i.e., not in a function definition |
| 3709 | if( !emit->env->func ) |
| 3710 | { |
| 3711 | // dependency tracking: ensure object instantiation (e.g., Foo foo;) is not invoked |
| 3712 | // before dependencies are met | 1.5.0.8 (ge) added |
| 3713 | // NOTE if the type (t) originates from another file, this should |
| 3714 | // behave correctly and return NULL | 1.5.1.1 (ge) fixed |
| 3715 | const Chuck_Value_Dependency * unfulfilled = t->depends.locate( unary->where ); |
| 3716 | // at least one unfulfilled |
| 3717 | if( unfulfilled ) |
| 3718 | { |
| 3719 | EM_error2( unary->type->where, |
| 3720 | "'%s' instantiation at this point skips initialization of a needed variable:", |
| 3721 | t->c_name() ); |
| 3722 | EM_error2( unfulfilled->where, |
| 3723 | "...(note: this skipped variable initialization is needed by class '%s')", |
| 3724 | t->c_name() ); |
| 3725 | EM_error2( unfulfilled->use_where, |
| 3726 | "...(note: this is where the variable is used within class '%s')", |
| 3727 | t->c_name() ); |
| 3728 | EM_error2( 0, |
| 3729 | "...(hint: try instantiating '%s' after the variable initialization)", t->c_name() ); |
| 3730 | return FALSE; |
| 3731 | } |
| 3732 | } |
| 3733 | |
| 3734 | // should always be false; can't 'new int[]'... |
| 3735 | t_CKBOOL is_array_ref = FALSE; |
| 3736 | // instantiate object, including array |
| 3737 | if( !emit_engine_instantiate_object( emit, t, &unary->ctor, unary->array, unary->type->ref, is_array_ref ) ) |
| 3738 | return FALSE; |
| 3739 | |
| 3740 | // the new needs to be addref, and released (later at the end of the stmt that contains this) | 1.5.1.8 |
| 3741 | Chuck_Instr_Stmt_Start * onStack = emit->stmt_stack.size() ? emit->stmt_stack.back() : NULL; |
| 3742 | // check it |
| 3743 | assert( onStack != NULL ); |
| 3744 | // offset variable |
| 3745 | t_CKUINT offset = 0; |
| 3746 | // acquire next offset |
| 3747 | if( !onStack->nextOffset( offset ) ) return FALSE; |
| 3748 | // append instruction, addRef=TRUE |
| 3749 | emit->append( new Chuck_Instr_Stmt_Remember_Object( onStack, offset, TRUE ) ); |
| 3750 | } |
| 3751 | break; |
| 3752 | |
| 3753 | default: |
| 3754 | EM_error2( unary->where, |
| 3755 | "(emit): internal error: unhandled unary op '%s", |
| 3756 | op2str( unary->op ) ); |
| 3757 | return FALSE; |
| 3758 | } |
| 3759 | |
| 3760 | return TRUE; |
| 3761 | } |
| 3762 | |
| 3763 | |
| 3764 | |
| 3765 | |
| 3766 | //----------------------------------------------------------------------------- |
| 3767 | // name: emit_engine_emit_exp_primary() |
| 3768 | // desc: ... |
| 3769 | //----------------------------------------------------------------------------- |
| 3770 | t_CKBOOL emit_engine_emit_exp_primary( Chuck_Emitter * emit, a_Exp_Primary exp ) |
| 3771 | { |
| 3772 | t_CKUINT temp; |
| 3773 | t_CKDUR dur; |
| 3774 | Chuck_String * str = NULL; |
| 3775 | |
| 3776 | // find out exp |
| 3777 | switch( exp->s_type ) |
| 3778 | { |
| 3779 | case ae_primary_var: |
| 3780 | if( exp->var == insert_symbol( "now" ) ) |
| 3781 | { |
| 3782 | emit->append( new Chuck_Instr_Reg_Push_Now ); |
| 3783 | } |
| 3784 | else if( exp->var == insert_symbol( "this" ) ) |
| 3785 | { |
| 3786 | // TODO: verify this is in the right scope |
| 3787 | emit->append( new Chuck_Instr_Reg_Push_This ); |
| 3788 | } |
| 3789 | else if( exp->var == insert_symbol( "super" ) ) |
| 3790 | { |
| 3791 | // super is still the same pointer, just different semantics on virtual table lookup |
| 3792 | emit->append( new Chuck_Instr_Reg_Push_This ); |
| 3793 | } |
| 3794 | else if( exp->var == insert_symbol( "me" ) ) |
| 3795 | { |
| 3796 | emit->append( new Chuck_Instr_Reg_Push_Me ); |
| 3797 | } |
| 3798 | else if( exp->var == insert_symbol( "true" ) ) |
| 3799 | { |
| 3800 | emit->append( new Chuck_Instr_Reg_Push_Imm( 1 ) ); |
| 3801 | } |
| 3802 | else if( exp->var == insert_symbol( "false" ) ) |
| 3803 | { |
| 3804 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 3805 | } |
| 3806 | else if( exp->var == insert_symbol("maybe") ) |
| 3807 | { |
| 3808 | emit->append( new Chuck_Instr_Reg_Push_Maybe() ); |
| 3809 | } |
| 3810 | else if( exp->var == insert_symbol("null") || |
| 3811 | exp->var == insert_symbol("NULL") ) |
| 3812 | { |
| 3813 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 3814 | } |
| 3815 | else if( exp->var == insert_symbol( "pi" ) ) |
| 3816 | { |
| 3817 | double ckPi = 3.14159265358979323846; |
| 3818 | emit->append( new Chuck_Instr_Reg_Push_Imm2( ckPi ) ); |
| 3819 | } |
| 3820 | else if( exp->var == insert_symbol( "dac" ) ) |
| 3821 | { |
| 3822 | // 1.4.1.0 (jack): see chuck_compile.h for an explanation of |
| 3823 | // replacement dacs |
| 3824 | // should replace dac with global ugen? |
| 3825 | if( emit->should_replace_dac ) |
| 3826 | { |
| 3827 | // push the global UGen on to the stack |
| 3828 | Chuck_Instr_Reg_Push_Global * instr = |
| 3829 | new Chuck_Instr_Reg_Push_Global( |
| 3830 | emit->dac_replacement, te_globalUGen ); |
| 3831 | instr->set_linepos( exp->line ); |
| 3832 | emit->append( instr ); |
| 3833 | } |
| 3834 | else |
| 3835 | { |
| 3836 | // proceed as normal -- push the dac onto the stack~ |
| 3837 | emit->append( new Chuck_Instr_DAC ); |
| 3838 | } |
| 3839 | } |
| 3840 | else if( exp->var == insert_symbol( "adc" ) ) |
| 3841 | { |
| 3842 | emit->append( new Chuck_Instr_ADC ); |
| 3843 | } |
| 3844 | else if( exp->var == insert_symbol( "bunghole" ) ) |
| 3845 | { |
| 3846 | emit->append( new Chuck_Instr_Bunghole ); |
| 3847 | } |
| 3848 | else if( exp->var == insert_symbol( "blackhole" ) ) |
| 3849 | { |
| 3850 | emit->append( new Chuck_Instr_Bunghole ); |
| 3851 | } |
| 3852 | else if( exp->var == insert_symbol( "chout" ) ) |
| 3853 | { |
| 3854 | emit->append( new Chuck_Instr_Chout ); |
| 3855 | } |
| 3856 | else if( exp->var == insert_symbol( "cherr" ) ) |
| 3857 | { |
| 3858 | emit->append( new Chuck_Instr_Cherr ); |
| 3859 | } |
| 3860 | else if( emit->find_dur( S_name(exp->var), &dur ) ) |
| 3861 | { |
| 3862 | emit->append( new Chuck_Instr_Reg_Push_Imm2( dur ) ); |
| 3863 | } |
| 3864 | else |
| 3865 | { |
| 3866 | // emit the symbol |
| 3867 | return emit_engine_emit_symbol( |
| 3868 | emit, exp->var, exp->value, exp->self->emit_var, exp->line, exp->where, exp ); |
| 3869 | } |
| 3870 | break; |
| 3871 | |
| 3872 | case ae_primary_num: |
| 3873 | memcpy( &temp, &exp->num, sizeof(temp) ); |
| 3874 | emit->append( new Chuck_Instr_Reg_Push_Imm( temp ) ); |
| 3875 | break; |
| 3876 | |
| 3877 | case ae_primary_float: |
| 3878 | emit->append( new Chuck_Instr_Reg_Push_Imm2( exp->fnum ) ); |
| 3879 | break; |
| 3880 | |
| 3881 | case ae_primary_str: |
| 3882 | // TODO: fix this |
| 3883 | str = new Chuck_String(); |
| 3884 | if( !str || !initialize_object( str, emit->env->ckt_string, NULL, emit->env->vm() ) ) |
| 3885 | { |
| 3886 | // error (TODO: why is this a CK_SAFE_RELEASE and not CK_SAFE_DELETE?) |
| 3887 | CK_SAFE_RELEASE( str ); |
| 3888 | // error out |
| 3889 | CK_FPRINTF_STDERR( |
| 3890 | "[chuck](emitter): OutOfMemory: while allocating string literal '%s'\n", exp->str ); |
| 3891 | return FALSE; |
| 3892 | } |
| 3893 | str->set( exp->str ); |
| 3894 | temp = (t_CKUINT)str; |
| 3895 | emit->append( new Chuck_Instr_Reg_Push_Imm( temp ) ); |
| 3896 | // add reference for string literal (added 1.3.0.2) |
| 3897 | str->add_ref(); |
| 3898 | break; |
| 3899 | |
| 3900 | case ae_primary_char: |
| 3901 | emit->append( new Chuck_Instr_Reg_Push_Imm( str2char(exp->chr, exp->where ) ) ); |
| 3902 | break; |
| 3903 | |
| 3904 | case ae_primary_array: |
| 3905 | if( !emit_engine_emit_array_lit( emit, exp->array ) ) |
| 3906 | return FALSE; |
| 3907 | break; |
| 3908 | |
| 3909 | case ae_primary_complex: |
| 3910 | if( !emit_engine_emit_complex_lit( emit, exp->complex ) ) |
| 3911 | return FALSE; |
| 3912 | break; |
| 3913 | |
| 3914 | case ae_primary_polar: |
| 3915 | if( !emit_engine_emit_polar_lit( emit, exp->polar ) ) |
| 3916 | return FALSE; |
| 3917 | break; |
| 3918 | |
| 3919 | case ae_primary_vec: |
| 3920 | if( !emit_engine_emit_vec_lit( emit, exp->vec ) ) |
| 3921 | return FALSE; |
| 3922 | break; |
| 3923 | |
| 3924 | case ae_primary_exp: |
| 3925 | if( !emit_engine_emit_exp( emit, exp->exp ) ) |
| 3926 | return FALSE; |
| 3927 | break; |
| 3928 | |
| 3929 | case ae_primary_hack: |
| 3930 | { |
| 3931 | a_Exp e = exp->exp; |
| 3932 | // emit the expression |
| 3933 | if( !emit_engine_emit_exp( emit, e ) ) |
| 3934 | return FALSE; |
| 3935 | std::vector<Chuck_Type *> types; |
| 3936 | |
| 3937 | while( e != NULL ) |
| 3938 | { |
| 3939 | types.push_back( e->type ); |
| 3940 | e = e->next; |
| 3941 | } |
| 3942 | |
| 3943 | // emit hack |
| 3944 | emit->append( new Chuck_Instr_Gack( types ) ); |
| 3945 | |
| 3946 | break; |
| 3947 | } |
| 3948 | |
| 3949 | // ge (april 2012): nil is void, so nothing to emit; TODO: check it (added 1.3.0.0) |
| 3950 | case ae_primary_nil: |
| 3951 | break; |
| 3952 | } |
| 3953 | |
| 3954 | return TRUE; |
| 3955 | } |
| 3956 | |
| 3957 | |
| 3958 | |
| 3959 | |
| 3960 | //----------------------------------------------------------------------------- |
| 3961 | // name: emit_engine_emit_array_lit() |
| 3962 | // desc: ... |
| 3963 | //----------------------------------------------------------------------------- |
| 3964 | t_CKBOOL emit_engine_emit_array_lit( Chuck_Emitter * emit, a_Array_Sub array ) |
| 3965 | { |
| 3966 | // go through and emit the expressions |
| 3967 | if( !emit_engine_emit_exp( emit, array->exp_list ) ) |
| 3968 | return FALSE; |
| 3969 | |
| 3970 | // count the number |
| 3971 | a_Exp e = array->exp_list; |
| 3972 | t_CKUINT count = 0; |
| 3973 | // loop over |
| 3974 | while( e ) { count++; e = e->next; } |
| 3975 | |
| 3976 | // the type |
| 3977 | e = array->exp_list; |
| 3978 | Chuck_Type * type = e->cast_to ? e->cast_to : e->type; |
| 3979 | |
| 3980 | // construct array dynamically |
| 3981 | Chuck_Instr * instr = NULL; |
| 3982 | emit->append( instr = new Chuck_Instr_Array_Init_Literal( emit->env, type, count ) ); |
| 3983 | instr->set_linepos( array->line ); |
| 3984 | |
| 3985 | return TRUE; |
| 3986 | } |
| 3987 | |
| 3988 | |
| 3989 | |
| 3990 | |
| 3991 | //----------------------------------------------------------------------------- |
| 3992 | // name: emit_engine_emit_complex_lit() |
| 3993 | // desc: ... |
| 3994 | //----------------------------------------------------------------------------- |
| 3995 | t_CKBOOL emit_engine_emit_complex_lit( Chuck_Emitter * emit, a_Complex val ) |
| 3996 | { |
| 3997 | // go through and emit the real and imaginary |
| 3998 | if( !emit_engine_emit_exp( emit, val->re ) ) |
| 3999 | return FALSE; |
| 4000 | |
| 4001 | // imaginary (not needed since linked after real |
| 4002 | // if( !emit_engine_emit_exp( emit, val->im ) ) |
| 4003 | // return FALSE; |
| 4004 | |
| 4005 | return TRUE; |
| 4006 | } |
| 4007 | |
| 4008 | |
| 4009 | |
| 4010 | |
| 4011 | //----------------------------------------------------------------------------- |
| 4012 | // name: emit_engine_emit_polar_lit() |
| 4013 | // desc: ... |
| 4014 | //----------------------------------------------------------------------------- |
| 4015 | t_CKBOOL emit_engine_emit_polar_lit( Chuck_Emitter * emit, a_Polar val ) |
| 4016 | { |
| 4017 | // go through and emit the mod and phase |
| 4018 | if( !emit_engine_emit_exp( emit, val->mod ) ) |
| 4019 | return FALSE; |
| 4020 | |
| 4021 | // phase (not needed since linked after mod) |
| 4022 | // if( !emit_engine_emit_exp( emit, val->phase ) ) |
| 4023 | // return FALSE; |
| 4024 | |
| 4025 | return TRUE; |
| 4026 | } |
| 4027 | |
| 4028 | |
| 4029 | |
| 4030 | |
| 4031 | //----------------------------------------------------------------------------- |
| 4032 | // name: emit_engine_emit_vec_lit() |
| 4033 | // desc: ... |
| 4034 | //----------------------------------------------------------------------------- |
| 4035 | t_CKBOOL emit_engine_emit_vec_lit( Chuck_Emitter * emit, a_Vec val ) |
| 4036 | { |
| 4037 | // go through and emit the mod and phase |
| 4038 | if( !emit_engine_emit_exp( emit, val->args ) ) |
| 4039 | return FALSE; |
| 4040 | |
| 4041 | // zero pad if needed, using smallest type: vec2 | 1.5.1.7 |
| 4042 | t_CKINT n = sz_VEC2/sz_FLOAT - val->numdims; |
| 4043 | // any remaining |
| 4044 | while( n > 0 ) |
| 4045 | { |
| 4046 | // emit a zero float |
| 4047 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0 ) ); |
| 4048 | // decrement |
| 4049 | n--; |
| 4050 | } |
| 4051 | |
| 4052 | return TRUE; |
| 4053 | } |
| 4054 | |
| 4055 | |
| 4056 | |
| 4057 | |
| 4058 | //----------------------------------------------------------------------------- |
| 4059 | // name: emit_engine_emit_exp_cast() |
| 4060 | // desc: ... |
| 4061 | //----------------------------------------------------------------------------- |
| 4062 | t_CKBOOL emit_engine_emit_exp_cast( Chuck_Emitter * emit, a_Exp_Cast cast ) |
| 4063 | { |
| 4064 | Chuck_Type * to = cast->self->type; |
| 4065 | Chuck_Type * from = cast->exp->type; |
| 4066 | |
| 4067 | // emit the exp |
| 4068 | if( !emit_engine_emit_exp( emit, cast->exp ) ) |
| 4069 | return FALSE; |
| 4070 | |
| 4071 | // the actual work to be done |
| 4072 | return emit_engine_emit_cast( emit, to, from, cast->where ); |
| 4073 | } |
| 4074 | |
| 4075 | |
| 4076 | |
| 4077 | |
| 4078 | //----------------------------------------------------------------------------- |
| 4079 | // name: emit_engine_emit_cast() |
| 4080 | // desc: emit type cast instructions |
| 4081 | //----------------------------------------------------------------------------- |
| 4082 | t_CKBOOL emit_engine_emit_cast( Chuck_Emitter * emit, |
| 4083 | Chuck_Type * to, Chuck_Type * from, |
| 4084 | uint32_t where ) |
| 4085 | { |
| 4086 | // if type is already the same |
| 4087 | if( equals( to, from ) ) |
| 4088 | return TRUE; |
| 4089 | |
| 4090 | // int to float |
| 4091 | if( equals( to, emit->env->ckt_int ) && equals( from, emit->env->ckt_float ) ) |
| 4092 | emit->append( new Chuck_Instr_Cast_double2int ); |
| 4093 | // float to int |
| 4094 | else if( equals( to, emit->env->ckt_float ) && equals( from, emit->env->ckt_int ) ) |
| 4095 | emit->append( new Chuck_Instr_Cast_int2double ); |
| 4096 | else if( equals( to, emit->env->ckt_complex ) && equals( from, emit->env->ckt_int ) ) |
| 4097 | emit->append( new Chuck_Instr_Cast_int2complex ); |
| 4098 | else if( equals( to, emit->env->ckt_polar ) && equals( from, emit->env->ckt_int ) ) |
| 4099 | emit->append( new Chuck_Instr_Cast_int2polar ); |
| 4100 | else if( equals( to, emit->env->ckt_complex ) && equals( from, emit->env->ckt_float ) ) |
| 4101 | emit->append( new Chuck_Instr_Cast_double2complex ); |
| 4102 | else if( equals( to, emit->env->ckt_polar ) && equals( from, emit->env->ckt_float ) ) |
| 4103 | emit->append( new Chuck_Instr_Cast_double2polar ); |
| 4104 | else if( equals( to, emit->env->ckt_polar ) && equals( from, emit->env->ckt_complex ) ) |
| 4105 | emit->append( new Chuck_Instr_Cast_complex2polar ); |
| 4106 | else if( equals( to, emit->env->ckt_complex ) && equals( from, emit->env->ckt_polar ) ) |
| 4107 | emit->append( new Chuck_Instr_Cast_polar2complex ); |
| 4108 | else if( equals( to, emit->env->ckt_vec2 ) && equals( from, emit->env->ckt_vec3 ) ) // 1.5.1.7 |
| 4109 | emit->append( new Chuck_Instr_Cast_vec3tovec2 ); |
| 4110 | else if( equals( to, emit->env->ckt_vec2 ) && equals( from, emit->env->ckt_vec4 ) ) // 1.5.1.7 |
| 4111 | emit->append( new Chuck_Instr_Cast_vec4tovec2 ); |
| 4112 | else if( equals( to, emit->env->ckt_vec3 ) && equals( from, emit->env->ckt_vec2 ) ) // 1.5.1.7 |
| 4113 | emit->append( new Chuck_Instr_Cast_vec2tovec3 ); |
| 4114 | else if( equals( to, emit->env->ckt_vec4 ) && equals( from, emit->env->ckt_vec2 ) ) // 1.5.1.7 |
| 4115 | emit->append( new Chuck_Instr_Cast_vec2tovec4 ); |
| 4116 | else if( equals( to, emit->env->ckt_vec3 ) && equals( from, emit->env->ckt_vec4 ) ) |
| 4117 | emit->append( new Chuck_Instr_Cast_vec4tovec3 ); |
| 4118 | else if( equals( to, emit->env->ckt_vec4 ) && equals( from, emit->env->ckt_vec3 ) ) |
| 4119 | emit->append( new Chuck_Instr_Cast_vec3tovec4 ); |
| 4120 | else if( equals( to, emit->env->ckt_string ) && isa( from, emit->env->ckt_object ) && !isa( from, emit->env->ckt_string ) ) |
| 4121 | emit->append( new Chuck_Instr_Cast_object2string ); |
| 4122 | // verify runtime dynamic cast | 1.5.2.0 (ge) added |
| 4123 | else if( isobj(emit->env,to) && isa(to, from) ) |
| 4124 | { |
| 4125 | // append instruction |
| 4126 | Chuck_Instr_Cast_Runtime_Verify * instr = NULL; |
| 4127 | emit->append( instr = new Chuck_Instr_Cast_Runtime_Verify(from,to) ); |
| 4128 | // create format string %%s for output in case of exception |
| 4129 | instr->set_codeformat4exception( EM_error2str(where, TRUE, "cannot cast dynamic type '%%s' to '%%s'..." ) ); |
| 4130 | } |
| 4131 | // up cast - do nothing |
| 4132 | else if( !isa( to, from ) && !isa( from, to ) ) |
| 4133 | { |
| 4134 | EM_error2( 0, "(emit): internal error: cannot cast type '%s' to '%s'", |
| 4135 | from->c_name(), to->c_name() ); |
| 4136 | return FALSE; |
| 4137 | } |
| 4138 | |
| 4139 | return TRUE; |
| 4140 | } |
| 4141 | |
| 4142 | |
| 4143 | |
| 4144 | |
| 4145 | //----------------------------------------------------------------------------- |
| 4146 | // name: emit_engine_emit_exp_postfix() |
| 4147 | // desc: ... |
| 4148 | //----------------------------------------------------------------------------- |
| 4149 | t_CKBOOL emit_engine_emit_exp_postfix( Chuck_Emitter * emit, a_Exp_Postfix postfix ) |
| 4150 | { |
| 4151 | // check operator overload | 1.5.1.5 (ge) |
| 4152 | t_CKBOOL op_overload = (postfix->ck_overload_func != NULL); |
| 4153 | t_CKBOOL doRef = FALSE; |
| 4154 | if( op_overload && isobj( emit->env, postfix->exp->type ) ) |
| 4155 | { |
| 4156 | // treat as argument |
| 4157 | doRef = TRUE; |
| 4158 | } |
| 4159 | |
| 4160 | // emit the exp |
| 4161 | if( !emit_engine_emit_exp( emit, postfix->exp, doRef ) ) |
| 4162 | return FALSE; |
| 4163 | |
| 4164 | // check overloading | 1.5.1.5 (ge) added |
| 4165 | if( postfix->ck_overload_func ) |
| 4166 | { |
| 4167 | // emit overloading |
| 4168 | return emit_engine_emit_op_overload_postfix( emit, postfix ); |
| 4169 | } |
| 4170 | |
| 4171 | // emit |
| 4172 | switch( postfix->op ) |
| 4173 | { |
| 4174 | case ae_op_plusplus: |
| 4175 | if( equals( postfix->exp->type, emit->env->ckt_int ) ) |
| 4176 | emit->append( new Chuck_Instr_PostInc_int ); |
| 4177 | else |
| 4178 | { |
| 4179 | EM_error2( postfix->where, |
| 4180 | "(emit): internal error: unhandled type '%s' for post '++' operator", |
| 4181 | postfix->exp->type->c_name() ); |
| 4182 | return FALSE; |
| 4183 | } |
| 4184 | break; |
| 4185 | |
| 4186 | case ae_op_minusminus: |
| 4187 | if( equals( postfix->exp->type, emit->env->ckt_int ) ) |
| 4188 | emit->append( new Chuck_Instr_PostDec_int ); |
| 4189 | else |
| 4190 | { |
| 4191 | EM_error2( postfix->where, |
| 4192 | "(emit): internal error: unhandled type '%s' for post '--' operator", |
| 4193 | postfix->exp->type->c_name() ); |
| 4194 | return FALSE; |
| 4195 | } |
| 4196 | break; |
| 4197 | |
| 4198 | default: |
| 4199 | EM_error2( postfix->where, |
| 4200 | "(emit): internal error: unhandled postfix operator '%s'", |
| 4201 | op2str( postfix->op ) ); |
| 4202 | return FALSE; |
| 4203 | } |
| 4204 | |
| 4205 | return TRUE; |
| 4206 | } |
| 4207 | |
| 4208 | |
| 4209 | |
| 4210 | |
| 4211 | //----------------------------------------------------------------------------- |
| 4212 | // name: emit_engine_emit_exp_dur() |
| 4213 | // desc: ... |
| 4214 | //----------------------------------------------------------------------------- |
| 4215 | t_CKBOOL emit_engine_emit_exp_dur( Chuck_Emitter * emit, a_Exp_Dur dur ) |
| 4216 | { |
| 4217 | // emit base |
| 4218 | if( !emit_engine_emit_exp( emit, dur->base ) ) |
| 4219 | return FALSE; |
| 4220 | |
| 4221 | // cast |
| 4222 | if( equals( dur->base->type, emit->env->ckt_int ) ) |
| 4223 | emit->append( new Chuck_Instr_Cast_int2double ); |
| 4224 | |
| 4225 | // emit unit |
| 4226 | if( !emit_engine_emit_exp( emit, dur->unit ) ) |
| 4227 | return FALSE; |
| 4228 | |
| 4229 | // multiply |
| 4230 | emit->append( new Chuck_Instr_Times_double ); |
| 4231 | |
| 4232 | return TRUE; |
| 4233 | } |
| 4234 | |
| 4235 | |
| 4236 | |
| 4237 | |
| 4238 | //----------------------------------------------------------------------------- |
| 4239 | // name: emit_engine_emit_exp_array() |
| 4240 | // desc: ... |
| 4241 | //----------------------------------------------------------------------------- |
| 4242 | t_CKBOOL emit_engine_emit_exp_array( Chuck_Emitter * emit, a_Exp_Array array ) |
| 4243 | { |
| 4244 | Chuck_Type * type = NULL, * base_type = NULL; |
| 4245 | t_CKUINT depth = 0; |
| 4246 | a_Array_Sub sub = NULL; |
| 4247 | a_Exp exp = NULL; |
| 4248 | t_CKBOOL is_var = FALSE; |
| 4249 | t_CKBOOL is_str = FALSE; |
| 4250 | |
| 4251 | // get the type |
| 4252 | type = array->self->type; |
| 4253 | // get the base type |
| 4254 | base_type = array->base->type; |
| 4255 | // emit as addr? |
| 4256 | is_var = array->self->emit_var; |
| 4257 | // get the dimension |
| 4258 | depth = base_type->array_depth - type->array_depth; |
| 4259 | // make sure |
| 4260 | if( depth == 0 ) |
| 4261 | { |
| 4262 | EM_error2( array->where, |
| 4263 | "(emit): internal error: array with 0 depth..." ); |
| 4264 | return FALSE; |
| 4265 | } |
| 4266 | // get the sub |
| 4267 | sub = array->indices; |
| 4268 | if( !sub ) |
| 4269 | { |
| 4270 | EM_error2( array->where, |
| 4271 | "(emit): internal error: NULL array sub..." ); |
| 4272 | return FALSE; |
| 4273 | } |
| 4274 | // get the exp list |
| 4275 | exp = sub->exp_list; |
| 4276 | if( !exp ) |
| 4277 | { |
| 4278 | EM_error2( array->where, |
| 4279 | "(emit): internal error: NULL array exp..." ); |
| 4280 | return FALSE; |
| 4281 | } |
| 4282 | |
| 4283 | // emit the base (it should be a pointer to array) |
| 4284 | if( !emit_engine_emit_exp( emit, array->base ) ) |
| 4285 | return FALSE; |
| 4286 | |
| 4287 | // emit the exp list |
| 4288 | if( !emit_engine_emit_exp( emit, exp ) ) |
| 4289 | return FALSE; |
| 4290 | |
| 4291 | // find out first element |
| 4292 | if( isa( exp->type, emit->env->ckt_string ) ) |
| 4293 | is_str = TRUE; |
| 4294 | |
| 4295 | // make sure |
| 4296 | // ISSUE: 64-bit (fixed 1.3.1.0) |
| 4297 | if( type->size != sz_INT && type->size != sz_FLOAT && type->size != sz_VEC2 && |
| 4298 | type->size != sz_VEC3 && type->size != sz_VEC4 ) |
| 4299 | { |
| 4300 | EM_error2( array->where, |
| 4301 | "(emit): internal error: array with datasize of %i...", type->size ); |
| 4302 | return FALSE; |
| 4303 | } |
| 4304 | |
| 4305 | // check the depth |
| 4306 | if( depth == 1 ) |
| 4307 | { |
| 4308 | // the pointer |
| 4309 | Chuck_Instr * instr = NULL; |
| 4310 | // emit the array access (1.3.1.0: use getkindof instead of type->size) |
| 4311 | if( is_str ) |
| 4312 | emit->append( instr = new Chuck_Instr_Array_Map_Access( getkindof(emit->env, type), is_var ) ); |
| 4313 | else |
| 4314 | emit->append( instr = new Chuck_Instr_Array_Access( getkindof(emit->env, type), is_var ) ); |
| 4315 | instr->set_linepos( array->line ); |
| 4316 | } |
| 4317 | else |
| 4318 | { |
| 4319 | // the pointer |
| 4320 | Chuck_Instr_Array_Access_Multi * aam = NULL; |
| 4321 | // emit the multi array access (1.3.1.0: use getkindof instead of type->size) |
| 4322 | emit->append( aam = new Chuck_Instr_Array_Access_Multi( depth, getkindof(emit->env, type), is_var ) ); |
| 4323 | aam->set_linepos( array->line ); |
| 4324 | // add type info (1.3.1.0) -- to support mixed string & int indexing (thanks Robin Haberkorn) |
| 4325 | a_Exp e = exp; |
| 4326 | while( e ) |
| 4327 | { |
| 4328 | // check if string |
| 4329 | aam->indexIsAssociative().push_back( isa( exp->type, emit->env->ckt_string ) ); |
| 4330 | // next |
| 4331 | e = e->next; |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | // TODO: variable? |
| 4336 | |
| 4337 | return TRUE; |
| 4338 | } |
| 4339 | |
| 4340 | |
| 4341 | |
| 4342 | |
| 4343 | //----------------------------------------------------------------------------- |
| 4344 | // name: struct Chuck_FuncCall_Options | 1.5.4.2 (ge) added |
| 4345 | // desc: a struct containing func call options, usually for specific cases |
| 4346 | //----------------------------------------------------------------------------- |
| 4347 | struct Chuck_FuncCall_Options |
| 4348 | { |
| 4349 | // set this to true ONLY for specific cases of... |
| 4350 | // 1) a special primitive (e.g., vec2/3/4)... |
| 4351 | // 2) calls one of its "member" functions (e.g., magnitude()) |
| 4352 | // 3) not from a variable but from a literal value (e.g., @(1,2,3).magnitude()) |
| 4353 | // in these cases, a special Chuck_Instr_Reg_Transmute_Value_To_Pointer |
| 4354 | // must have been emitted prior to the member function call |
| 4355 | // NOTE: in effect, this option tells the member function call to |
| 4356 | // clean up the trasmuted THIS pointer before returning |
| 4357 | // NOTE: part of #special-primitive-member-func-from-literal |
| 4358 | t_CKBOOL transmutingSpecialPrimitiveForMemberFunc; |
| 4359 | |
| 4360 | // constructor |
| 4361 | Chuck_FuncCall_Options( t_CKBOOL transmuting = FALSE ) |
| 4362 | { |
| 4363 | // defaults |
| 4364 | transmutingSpecialPrimitiveForMemberFunc = transmuting; |
| 4365 | } |
| 4366 | }; |
| 4367 | |
| 4368 | |
| 4369 | |
| 4370 | |
| 4371 | //----------------------------------------------------------------------------- |
| 4372 | // name: emit_engine_emit_func_args() |
| 4373 | // desc: ... |
| 4374 | //----------------------------------------------------------------------------- |
| 4375 | t_CKBOOL emit_engine_emit_func_args( Chuck_Emitter * emit, |
| 4376 | a_Exp_Func_Call func_call ) |
| 4377 | { |
| 4378 | // emit the args (TRUE for doAddRef added 1.3.0.0) |
| 4379 | if( !emit_engine_emit_exp( emit, func_call->args, TRUE ) ) |
| 4380 | { |
| 4381 | EM_error2( func_call->where, |
| 4382 | "(emit): internal error in emitting function call arguments..." ); |
| 4383 | return FALSE; |
| 4384 | } |
| 4385 | |
| 4386 | return TRUE; |
| 4387 | } |
| 4388 | |
| 4389 | |
| 4390 | |
| 4391 | |
| 4392 | //----------------------------------------------------------------------------- |
| 4393 | // name: emit_engine_emit_exp_func_call() |
| 4394 | // desc: emit function call from a_Exp_Func_Call |
| 4395 | //----------------------------------------------------------------------------- |
| 4396 | t_CKBOOL emit_engine_emit_exp_func_call( Chuck_Emitter * emit, |
| 4397 | a_Exp_Func_Call func_call, |
| 4398 | t_CKBOOL spork ) |
| 4399 | { |
| 4400 | // note: spork situations are now taken care in exp_spork... |
| 4401 | // please look at that one before modifying this one! |
| 4402 | |
| 4403 | // make sure there are args, and not sporking |
| 4404 | if( func_call->args && !spork ) |
| 4405 | { |
| 4406 | if( !emit_engine_emit_func_args( emit, func_call ) ) |
| 4407 | return FALSE; |
| 4408 | } |
| 4409 | |
| 4410 | // need to know here so the func can properly emit |
| 4411 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 4412 | func_call->func->emit_as_funccall = TRUE; |
| 4413 | |
| 4414 | // emit func |
| 4415 | if( !emit_engine_emit_exp( emit, func_call->func ) ) |
| 4416 | { |
| 4417 | // 1.5.4.3 (ge) no longer necessarily an internal error #2024-static-init |
| 4418 | // EM_error2( func_call->where, |
| 4419 | // "(emit): internal error in evaluating function call..." ); |
| 4420 | return FALSE; |
| 4421 | } |
| 4422 | |
| 4423 | // get the function as exp | 1.5.4.4 (ge) added to support calling this() #2024-ctor-this |
| 4424 | a_Exp exp_func = func_call->func; |
| 4425 | // check if this() -- for calling another constructor from a constructor | 1.5.4.4 (ge) added |
| 4426 | if( exp_func->s_type == ae_exp_primary && exp_func->primary.s_type == ae_primary_var && string(S_name(exp_func->primary.var)) == "this" ) |
| 4427 | { |
| 4428 | // NOTE should have already checked that we are within a class if `this` was used |
| 4429 | assert( emit->env->class_def != NULL ); |
| 4430 | // emit the contructor func | (don't need to dup last since we are not resolving from a dot_member |
| 4431 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)exp_func->primary.func_alias ) ); |
| 4432 | } |
| 4433 | |
| 4434 | // line and pos |
| 4435 | t_CKUINT line = func_call->line; |
| 4436 | t_CKUINT where = func_call->where; |
| 4437 | // additional func call options | 1.5.4.2 (ge) added |
| 4438 | Chuck_FuncCall_Options options; |
| 4439 | |
| 4440 | // check if func is a dot_member |
| 4441 | if( func_call->func->s_type == ae_exp_dot_member ) |
| 4442 | { |
| 4443 | // if possible, get more accurate code position |
| 4444 | line = func_call->func->dot_member.line; |
| 4445 | where = func_call->func->dot_member.where; |
| 4446 | // set option | 1.5.4.2 (ge) added as part of #special-primitive-member-func-from-literal |
| 4447 | // this should only be true for special non-primitive functions *from literal value* e.g., @(1,2,3).magnitude(); |
| 4448 | options.transmutingSpecialPrimitiveForMemberFunc = func_call->func->dot_member.isSpecialPrimitiveFunc; |
| 4449 | } |
| 4450 | |
| 4451 | // the rest |
| 4452 | return emit_engine_emit_exp_func_call( emit, func_call->ck_func, func_call->ret_type, |
| 4453 | line, where, spork, &options ); |
| 4454 | } |
| 4455 | |
| 4456 | |
| 4457 | |
| 4458 | |
| 4459 | //----------------------------------------------------------------------------- |
| 4460 | // name: emit_engine_emit_exp_func_call() |
| 4461 | // desc: emit function call from necessary information |
| 4462 | //----------------------------------------------------------------------------- |
| 4463 | t_CKBOOL emit_engine_emit_exp_func_call( Chuck_Emitter * emit, |
| 4464 | Chuck_Func * func, |
| 4465 | Chuck_Type * type, |
| 4466 | t_CKUINT line, |
| 4467 | t_CKUINT where, |
| 4468 | t_CKBOOL spork, |
| 4469 | Chuck_FuncCall_Options * options ) |
| 4470 | { |
| 4471 | // is a member? |
| 4472 | t_CKBOOL is_member = func->is_member; |
| 4473 | // is a static? (within class) |
| 4474 | t_CKBOOL is_static = func->is_static; |
| 4475 | // whether to enable the #special-primitive-member-func-from-literal option for member func |
| 4476 | t_CKBOOL transmuting = options ? options->transmutingSpecialPrimitiveForMemberFunc : FALSE; |
| 4477 | |
| 4478 | // only check dependency violations if we are at a context-top-level |
| 4479 | // or class-top-level scope, i.e., not in a function definition |
| 4480 | // also, once sporked, it will be up the programmer to ensure intention |
| 4481 | // ------- |
| 4482 | // 1.5.4.0 (ge) sporked function calls now subject to same dependency verification |
| 4483 | // this may be more stringent (due to potential timing behavior) but |
| 4484 | // should help prevent confusing crashes; removing check for spork |
| 4485 | if( !emit->env->func /* && !spork */ ) |
| 4486 | { |
| 4487 | // check if we should check for dependency | 1.5.4.4 (ge) added #2024-static-init |
| 4488 | // can skip check if 1) we are in a class def 2) not in a stmt marked for static init 3) the function we are calling is static |
| 4489 | // actually, this doesn't work for cases where static functions actually depend (implicitly) on instance members / pre-ctor |
| 4490 | // for example through the use of new; see test/06-Errors/error-depend-class-extend.ck |
| 4491 | // t_CKBOOL skip = ( emit->env->class_def && !emit->env->in_static_stmt() && func->is_static ); |
| 4492 | |
| 4493 | // dependency tracking: check if we invoke func before all its deps are initialized | 1.5.0.8 (ge) added |
| 4494 | // NOTE if func originates from another file, this should behave correctly and return NULL | 1.5.1.1 (ge) fixed |
| 4495 | // NOTE passing in emit->env->class_def, to differentiate dependencies across class definitions | 1.5.2.0 (ge) fixed |
| 4496 | const Chuck_Value_Dependency * unfulfilled = func->depends.locate( where, emit->env->class_def ); |
| 4497 | // at least one unfulfilled dependency |
| 4498 | if( unfulfilled ) |
| 4499 | { |
| 4500 | EM_error2( where, |
| 4501 | "%s '%s()' at this point %sskips initialization of a needed variable:", |
| 4502 | spork ? "sporking" : "calling", func->base_name.c_str(), spork ? "potentially " : "" ); |
| 4503 | EM_error2( unfulfilled->where, |
| 4504 | "...(note: this skipped variable initialization is needed by '%s')", |
| 4505 | func->signature().c_str() ); |
| 4506 | EM_error2( unfulfilled->use_where, |
| 4507 | "...(note: this is where the variable is used within '%s' or its subsidiaries)", |
| 4508 | func->signature().c_str() ); |
| 4509 | EM_error2( 0, |
| 4510 | "...(hint: try calling '%s()' after the variable initialization)", func->base_name.c_str() ); |
| 4511 | return FALSE; |
| 4512 | } |
| 4513 | } |
| 4514 | |
| 4515 | // translate to code |
| 4516 | emit->append( new Chuck_Instr_Func_To_Code ); |
| 4517 | // emit->append( new Chuck_Instr_Reg_Push_Code( func->code ) ); |
| 4518 | // push the local stack depth - local variables |
| 4519 | emit->append( new Chuck_Instr_Reg_Push_Imm( emit->code->frame->curr_offset ) ); |
| 4520 | |
| 4521 | // call the function |
| 4522 | t_CKUINT size = type->size; |
| 4523 | t_CKUINT kind = getkindof( emit->env, type ); // added 1.3.1.0 |
| 4524 | |
| 4525 | // the pointer |
| 4526 | Chuck_Instr * instr = NULL; |
| 4527 | if( func->def()->s_type == ae_func_builtin ) |
| 4528 | { |
| 4529 | // ISSUE: 64-bit (fixed 1.3.1.0) |
| 4530 | if( size == 0 || size == sz_INT || size == sz_FLOAT || size == sz_VEC2 || |
| 4531 | size == sz_VEC3 || sz_VEC4 ) |
| 4532 | { |
| 4533 | // is member (1.3.1.0: changed to use kind instead of size) |
| 4534 | if( is_member ) |
| 4535 | emit->append( instr = new Chuck_Instr_Func_Call_Member( kind, func, CK_FUNC_CALL_THIS_IN_BACK, transmuting ) ); |
| 4536 | else if( is_static ) |
| 4537 | emit->append( instr = new Chuck_Instr_Func_Call_Static( kind, func ) ); |
| 4538 | else // 1.5.1.5 (ge & andrew) new planes of existence --> this is in global-scope (not global variable) |
| 4539 | emit->append( instr = new Chuck_Instr_Func_Call_Global( kind, func ) ); |
| 4540 | } |
| 4541 | else |
| 4542 | { |
| 4543 | EM_error2( where, |
| 4544 | "(emit): internal error: %i func call not handled", |
| 4545 | size ); |
| 4546 | return FALSE; |
| 4547 | } |
| 4548 | } |
| 4549 | else |
| 4550 | { |
| 4551 | emit->append( instr = new Chuck_Instr_Func_Call ); |
| 4552 | } |
| 4553 | // set line position |
| 4554 | instr->set_linepos(line); |
| 4555 | |
| 4556 | return TRUE; |
| 4557 | } |
| 4558 | |
| 4559 | |
| 4560 | |
| 4561 | |
| 4562 | //----------------------------------------------------------------------------- |
| 4563 | // name: emit_engine_emit_dot_member_special() |
| 4564 | // desc: emit special dot member: complex, polar, vec2, vec3, vec4 |
| 4565 | //----------------------------------------------------------------------------- |
| 4566 | t_CKBOOL emit_engine_emit_exp_dot_member_special( Chuck_Emitter * emit, |
| 4567 | a_Exp_Dot_Member member ) |
| 4568 | { |
| 4569 | // whether to emit addr or value |
| 4570 | t_CKBOOL emit_addr = member->self->emit_var; |
| 4571 | |
| 4572 | // special case: complex and polar |
| 4573 | if( member->t_base->xid == te_complex ) |
| 4574 | { |
| 4575 | // mark to emit addr |
| 4576 | if( member->base->s_meta == ae_meta_var ) |
| 4577 | member->base->emit_var = TRUE; |
| 4578 | else if( emit_addr ) |
| 4579 | { |
| 4580 | EM_error2( member->base->where, |
| 4581 | "(emit): cannot assign value to literal 'complex' value..." ); |
| 4582 | return FALSE; |
| 4583 | } |
| 4584 | |
| 4585 | // emit the base |
| 4586 | if( !emit_engine_emit_exp( emit, member->base ) ) |
| 4587 | return FALSE; |
| 4588 | |
| 4589 | string str = S_name(member->xid); |
| 4590 | // check |
| 4591 | if( str == "re" ) |
| 4592 | emit->append( new Chuck_Instr_Dot_Cmp_First( member->base->s_meta == ae_meta_var, emit_addr, kindof_VEC2 ) ); |
| 4593 | else if( str == "im" ) |
| 4594 | emit->append( new Chuck_Instr_Dot_Cmp_Second( member->base->s_meta == ae_meta_var, emit_addr, kindof_VEC2 ) ); |
| 4595 | else |
| 4596 | goto check_func; |
| 4597 | |
| 4598 | // done |
| 4599 | return TRUE; |
| 4600 | } |
| 4601 | else if( member->t_base->xid == te_polar ) |
| 4602 | { |
| 4603 | // mark to emit addr |
| 4604 | if( member->base->s_meta == ae_meta_var ) |
| 4605 | member->base->emit_var = TRUE; |
| 4606 | else if( emit_addr ) |
| 4607 | { |
| 4608 | EM_error2( member->base->where, |
| 4609 | "(emit): cannot assign value to literal 'polar' value..." ); |
| 4610 | return FALSE; |
| 4611 | } |
| 4612 | |
| 4613 | // emit the base |
| 4614 | if( !emit_engine_emit_exp( emit, member->base ) ) |
| 4615 | return FALSE; |
| 4616 | |
| 4617 | string str = S_name(member->xid); |
| 4618 | // check |
| 4619 | if( str == "mag" ) |
| 4620 | emit->append( new Chuck_Instr_Dot_Cmp_First( member->base->s_meta == ae_meta_var, emit_addr, kindof_VEC2 ) ); |
| 4621 | else if( str == "phase" ) |
| 4622 | emit->append( new Chuck_Instr_Dot_Cmp_Second( member->base->s_meta == ae_meta_var, emit_addr, kindof_VEC2 ) ); |
| 4623 | else |
| 4624 | goto check_func; |
| 4625 | |
| 4626 | // done |
| 4627 | return TRUE; |
| 4628 | } |
| 4629 | else if( member->t_base->xid == te_vec2 ) |
| 4630 | { |
| 4631 | // remember the kind |
| 4632 | te_KindOf kind = kindof_VEC2; |
| 4633 | // mark to emit addr |
| 4634 | if( member->base->s_meta == ae_meta_var ) |
| 4635 | member->base->emit_var = TRUE; |
| 4636 | else if( emit_addr ) |
| 4637 | { |
| 4638 | EM_error2( member->base->where, |
| 4639 | "(emit): cannot assign value to literal '%s' value...", |
| 4640 | member->t_base->c_name() ); |
| 4641 | return FALSE; |
| 4642 | } |
| 4643 | |
| 4644 | // emit the base |
| 4645 | if( !emit_engine_emit_exp( emit, member->base ) ) |
| 4646 | return FALSE; |
| 4647 | |
| 4648 | string str = S_name(member->xid); |
| 4649 | // check for .xy .us |
| 4650 | if( str == "x" || str == "u" || str == "s" ) |
| 4651 | emit->append( new Chuck_Instr_Dot_Cmp_First( member->base->s_meta == ae_meta_var, emit_addr, kind ) ); |
| 4652 | else if( str == "y" || str == "v" || str == "t" ) |
| 4653 | emit->append( new Chuck_Instr_Dot_Cmp_Second( member->base->s_meta == ae_meta_var, emit_addr, kind ) ); |
| 4654 | else |
| 4655 | goto check_func; |
| 4656 | |
| 4657 | // done |
| 4658 | return TRUE; |
| 4659 | } |
| 4660 | else if( member->t_base->xid == te_vec3 || member->t_base->xid == te_vec4 ) |
| 4661 | { |
| 4662 | // remember the kind |
| 4663 | te_KindOf kind = member->t_base->xid == te_vec3 ? kindof_VEC3 : kindof_VEC4; |
| 4664 | // mark to emit addr |
| 4665 | if( member->base->s_meta == ae_meta_var ) |
| 4666 | member->base->emit_var = TRUE; |
| 4667 | else if( emit_addr ) |
| 4668 | { |
| 4669 | EM_error2( member->base->where, |
| 4670 | "(emit): cannot assign value to literal '%s' value...", |
| 4671 | member->t_base->c_name() ); |
| 4672 | return FALSE; |
| 4673 | } |
| 4674 | |
| 4675 | // emit the base |
| 4676 | if( !emit_engine_emit_exp( emit, member->base ) ) |
| 4677 | return FALSE; |
| 4678 | |
| 4679 | string str = S_name(member->xid); |
| 4680 | // check for .xyz[w] .rgb[a] .value/goal/slew |
| 4681 | if( str == "x" || str == "r" || str == "value" ) |
| 4682 | emit->append( new Chuck_Instr_Dot_Cmp_First( member->base->s_meta == ae_meta_var, emit_addr, kind ) ); |
| 4683 | else if( str == "y" || str == "g" || str == "goal" ) |
| 4684 | emit->append( new Chuck_Instr_Dot_Cmp_Second( member->base->s_meta == ae_meta_var, emit_addr, kind ) ); |
| 4685 | else if( str == "z" || str == "b" || str == "slew" ) |
| 4686 | emit->append( new Chuck_Instr_Dot_Cmp_Third( member->base->s_meta == ae_meta_var, emit_addr, kind ) ); |
| 4687 | else if( member->t_base->xid == te_vec4 && ( str == "w" || str == "a" ) ) |
| 4688 | emit->append( new Chuck_Instr_Dot_Cmp_Fourth( member->base->s_meta == ae_meta_var, emit_addr, kind ) ); |
| 4689 | else |
| 4690 | goto check_func; |
| 4691 | |
| 4692 | // done |
| 4693 | return TRUE; |
| 4694 | } |
| 4695 | |
| 4696 | // should not get here |
| 4697 | EM_error2( member->base->where, |
| 4698 | "(emit): internal error in lit_special()..." ); |
| 4699 | |
| 4700 | return FALSE; |
| 4701 | |
| 4702 | //----------------------------------------------------------------------------- |
| 4703 | // goto: check for special member functions, 1.3.5.3 |
| 4704 | //----------------------------------------------------------------------------- |
| 4705 | check_func: |
| 4706 | |
| 4707 | // the type of the base |
| 4708 | Chuck_Type * t_base = member->t_base; |
| 4709 | // is the base a class/namespace or a variable |
| 4710 | t_CKBOOL base_type_static = type_engine_is_base_type_static( emit->env, member->t_base ); |
| 4711 | // a function |
| 4712 | Chuck_Func * func = NULL; |
| 4713 | // a non-function value |
| 4714 | Chuck_Value * value = NULL; |
| 4715 | // the offset |
| 4716 | t_CKUINT offset = 0; |
| 4717 | |
| 4718 | // check error |
| 4719 | if( base_type_static ) |
| 4720 | { |
| 4721 | // should not get here |
| 4722 | EM_error2( member->base->where, |
| 4723 | "(emit): internal error in lit_special(): base static" ); |
| 4724 | // done |
| 4725 | return FALSE; |
| 4726 | } |
| 4727 | |
| 4728 | if( !isfunc( emit->env, member->self->type ) ) |
| 4729 | { |
| 4730 | // should not get here |
| 4731 | EM_error2( member->base->where, |
| 4732 | "(emit): internal error in lit_special(): not member func" ); |
| 4733 | // done |
| 4734 | return FALSE; |
| 4735 | } |
| 4736 | |
| 4737 | // get the func |
| 4738 | value = type_engine_find_value( t_base, member->xid ); |
| 4739 | func = value->func_ref; |
| 4740 | if( !func ) |
| 4741 | { |
| 4742 | // should not get here |
| 4743 | EM_error2( member->base->where, |
| 4744 | "(emit): internal error in lit_special(): expected function not found in value" ); |
| 4745 | // done |
| 4746 | return FALSE; |
| 4747 | } |
| 4748 | |
| 4749 | // NOTE: base already emitted earier in this function (and as var) |
| 4750 | |
| 4751 | // check base; 1.3.5.3 |
| 4752 | if( member->base->s_meta == ae_meta_value ) // is literal |
| 4753 | { |
| 4754 | // verify |
| 4755 | if( !member->isSpecialPrimitiveFunc ) |
| 4756 | { |
| 4757 | // should not get here |
| 4758 | EM_error2( member->base->where, |
| 4759 | "(emit): internal error in lit_special(): unexpected specialPrimitiveFunc == FALSE" ); |
| 4760 | // done |
| 4761 | return FALSE; |
| 4762 | } |
| 4763 | |
| 4764 | // 1.5.4.2 (ge) #special-primitive-member-func-from-literal |
| 4765 | // transmute the value as pointer (as faux-'this' pointer) |
| 4766 | emit->append( new Chuck_Instr_Reg_Transmute_Value_To_Pointer( t_base->size ) ); |
| 4767 | } |
| 4768 | |
| 4769 | // check if we are part of a function call vs. function as value |
| 4770 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 4771 | if( member->self->emit_as_funccall ) |
| 4772 | { |
| 4773 | // dup the base pointer ('this' pointer as argument -- special case primitive) |
| 4774 | // as of 1.5.4.2 this is emitted for both literals and variables |
| 4775 | // #special-primitive-member-func-from-literal |
| 4776 | emit->append( new Chuck_Instr_Reg_Dup_Last ); |
| 4777 | } |
| 4778 | |
| 4779 | // find the offset for virtual table |
| 4780 | offset = func->vt_index; |
| 4781 | // emit the function |
| 4782 | emit->append( new Chuck_Instr_Dot_Primitive_Func( (t_CKUINT)func ) ); |
| 4783 | |
| 4784 | return TRUE; |
| 4785 | } |
| 4786 | |
| 4787 | |
| 4788 | |
| 4789 | |
| 4790 | //----------------------------------------------------------------------------- |
| 4791 | // name: emit_engine_emit_dot_member() |
| 4792 | // desc: ... |
| 4793 | //----------------------------------------------------------------------------- |
| 4794 | t_CKBOOL emit_engine_emit_exp_dot_member( Chuck_Emitter * emit, |
| 4795 | a_Exp_Dot_Member member ) |
| 4796 | { |
| 4797 | // the type of the base |
| 4798 | Chuck_Type * t_base = NULL; |
| 4799 | // whether to emit addr or value |
| 4800 | t_CKBOOL emit_addr = member->self->emit_var; |
| 4801 | // is the base a class/namespace or a variable | 1.5.0.0 (ge) modified to func call |
| 4802 | t_CKBOOL base_type_static = type_engine_is_base_type_static( emit->env, member->t_base ); |
| 4803 | // check if the base exp is static-compatible, e.g., usable to initialize static var | 1.5.4.3 (ge) added as part of #2024-static-init |
| 4804 | t_CKBOOL base_exp_static = type_engine_is_base_exp_static( emit->env, member ); |
| 4805 | // a function |
| 4806 | Chuck_Func * func = NULL; |
| 4807 | // a non-function value |
| 4808 | Chuck_Value * value = NULL; |
| 4809 | // the offset |
| 4810 | t_CKUINT offset = 0; |
| 4811 | |
| 4812 | // the pointer |
| 4813 | Chuck_Instr * instr = NULL; |
| 4814 | |
| 4815 | // type of the base |
| 4816 | t_CKUINT tbase_xid = member->t_base->xid; |
| 4817 | // switch on it |
| 4818 | switch( tbase_xid ) |
| 4819 | { |
| 4820 | case te_complex: |
| 4821 | case te_polar: |
| 4822 | case te_vec2: |
| 4823 | case te_vec3: |
| 4824 | case te_vec4: |
| 4825 | // emit special |
| 4826 | return emit_engine_emit_exp_dot_member_special( emit, member ); |
| 4827 | // done |
| 4828 | break; |
| 4829 | } |
| 4830 | |
| 4831 | // actual type - if base is class name its type is actually 'class' |
| 4832 | // to get the actual type use actual_type |
| 4833 | t_base = base_type_static ? member->t_base->actual_type : member->t_base; |
| 4834 | |
| 4835 | // make sure that the base type is object |
| 4836 | assert( t_base->nspc != NULL ); |
| 4837 | |
| 4838 | // get the value to test | 1.5.4.3 (ge) added as part of #2024-static-init |
| 4839 | // NOTE: this type of check usually resides pre-emisssion, in the type-checker |
| 4840 | // but in this case, dot_member() emission also implicitly covers symbol X |
| 4841 | // if X is a member of a class (type-checker does not do this implicit conversion at the moment) |
| 4842 | Chuck_Value * v = type_engine_find_value( t_base, member->xid ); |
| 4843 | if( v && (!base_type_static && !base_exp_static) |
| 4844 | && emit->env->class_def && !emit->env->func |
| 4845 | && emit->env->in_static_stmt() ) |
| 4846 | { |
| 4847 | if( v->func_ref ) |
| 4848 | { |
| 4849 | if( v->is_instance_member ) |
| 4850 | { |
| 4851 | EM_error2( member->where, |
| 4852 | "cannot call non-static function '%s' to initialize a static variable", v->func_ref->signature(FALSE,FALSE).c_str() ); |
| 4853 | return FALSE; |
| 4854 | } |
| 4855 | } |
| 4856 | else if( !v->is_static ) |
| 4857 | { |
| 4858 | EM_error2( member->where, |
| 4859 | "cannot access non-static variable '%s.%s' to initialize a static variable", v->owner_class->name().c_str(), v->name.c_str() ); |
| 4860 | return FALSE; |
| 4861 | } |
| 4862 | } |
| 4863 | |
| 4864 | // if base is static? |
| 4865 | if( !base_type_static ) |
| 4866 | { |
| 4867 | // if is a func |
| 4868 | if( isfunc( emit->env, member->self->type ) ) |
| 4869 | { |
| 4870 | // get the func |
| 4871 | value = type_engine_find_value( t_base, member->xid ); |
| 4872 | func = value->func_ref; |
| 4873 | // make sure it's there |
| 4874 | assert( func != NULL ); |
| 4875 | |
| 4876 | // is the func static? |
| 4877 | if( func->is_member ) |
| 4878 | { |
| 4879 | // emit the base |
| 4880 | if( !emit_engine_emit_exp( emit, member->base ) ) { return FALSE; } |
| 4881 | // check if we are part of a function call vs. function as value |
| 4882 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 4883 | if( member->self->emit_as_funccall ) |
| 4884 | { |
| 4885 | // dup the base pointer ('this' pointer as argument) |
| 4886 | emit->append( new Chuck_Instr_Reg_Dup_Last ); |
| 4887 | } |
| 4888 | // find the offset for virtual table |
| 4889 | offset = func->vt_index; |
| 4890 | // emit the function |
| 4891 | // check if the base was `super` | (niccolo, modified by nick and ge) 1.5.5.6 |
| 4892 | if( member->base->s_type == ae_exp_primary && |
| 4893 | member->base->primary.s_type == ae_primary_var && |
| 4894 | member->base->primary.var == insert_symbol( "super" ) ) |
| 4895 | { |
| 4896 | // FYI is the base is super, then the pointer on the stack is the SAME... |
| 4897 | // but the vtable lookup logic must be different, hence this instruction |
| 4898 | emit->append( instr = new Chuck_Instr_Dot_Member_Func_Super( offset, emit->env->class_def->parent_type ) ); |
| 4899 | } |
| 4900 | else |
| 4901 | { |
| 4902 | // function lookup with "this" semantics |
| 4903 | emit->append( instr = new Chuck_Instr_Dot_Member_Func( offset ) ); |
| 4904 | } |
| 4905 | // set line position |
| 4906 | instr->set_linepos( member->line ); |
| 4907 | } |
| 4908 | else |
| 4909 | { |
| 4910 | // emit the type |
| 4911 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)t_base ) ); |
| 4912 | // check if we are part of a function call vs. function as value |
| 4913 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 4914 | if( member->self->emit_as_funccall ) |
| 4915 | { |
| 4916 | // dup the base pointer ('this' pointer as argument) |
| 4917 | emit->append( new Chuck_Instr_Reg_Dup_Last ); |
| 4918 | } |
| 4919 | // emit the static function |
| 4920 | emit->append( new Chuck_Instr_Dot_Static_Func( func ) ); |
| 4921 | } |
| 4922 | } |
| 4923 | else |
| 4924 | { |
| 4925 | // get the value |
| 4926 | // value = t_base->info->lookup_value( member->xid, FALSE ); |
| 4927 | value = type_engine_find_value( t_base, member->xid ); |
| 4928 | // make sure it's there |
| 4929 | assert( value != NULL ); |
| 4930 | |
| 4931 | // find the offset for data |
| 4932 | offset = value->offset; |
| 4933 | |
| 4934 | // is the value static? |
| 4935 | if( value->is_instance_member ) |
| 4936 | { |
| 4937 | // emit the base (TODO: return on error?) |
| 4938 | emit_engine_emit_exp( emit, member->base ); |
| 4939 | // lookup the member (1.3.1.0: changed to use getkindof instead of type size) |
| 4940 | emit->append( instr = new Chuck_Instr_Dot_Member_Data( |
| 4941 | offset, getkindof(emit->env, member->self->type), emit_addr ) ); |
| 4942 | instr->set_linepos( member->line ); |
| 4943 | } |
| 4944 | else |
| 4945 | { |
| 4946 | // if builtin or not |
| 4947 | if( value->addr ) |
| 4948 | { |
| 4949 | // emit builtin (1.3.1.0: changed to use getkindof instead of type size) |
| 4950 | emit->append( new Chuck_Instr_Dot_Static_Import_Data( |
| 4951 | value->addr, getkindof(emit->env, member->self->type), emit_addr ) ); |
| 4952 | } |
| 4953 | else |
| 4954 | { |
| 4955 | // emit the type |
| 4956 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)t_base ) ); |
| 4957 | // emit the static value (1.3.1.0: changed to use getkindof in addition to size) |
| 4958 | emit->append( new Chuck_Instr_Dot_Static_Data( |
| 4959 | offset, member->self->type->size, getkindof(emit->env, member->self->type), emit_addr ) ); |
| 4960 | } |
| 4961 | } |
| 4962 | } |
| 4963 | } |
| 4964 | else // static |
| 4965 | { |
| 4966 | // emit the type |
| 4967 | // commented out so built-in static member variables don't have an |
| 4968 | // extra thing on the stack - spencer |
| 4969 | // emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)t_base ) ); |
| 4970 | |
| 4971 | // if is a func |
| 4972 | if( isfunc( emit->env, member->self->type ) ) |
| 4973 | { |
| 4974 | // emit the type - spencer |
| 4975 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)t_base ) ); |
| 4976 | // if part of a func call |
| 4977 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 4978 | if( member->self->emit_as_funccall ) |
| 4979 | { |
| 4980 | // dupe the pointer, as Chuck_Instr_Dot_Static_Func will consume it |
| 4981 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 4982 | emit->append( new Chuck_Instr_Reg_Dup_Last ); |
| 4983 | } |
| 4984 | // get the func | 1.4.1.0 (ge) added looking in parent |
| 4985 | value = type_engine_find_value( t_base, member->xid ); |
| 4986 | // get the function reference |
| 4987 | func = value->func_ref; |
| 4988 | // make sure it's there |
| 4989 | assert( func != NULL ); |
| 4990 | // emit the function |
| 4991 | emit->append( new Chuck_Instr_Dot_Static_Func( func ) ); |
| 4992 | } |
| 4993 | else |
| 4994 | { |
| 4995 | // get the func | 1.4.1.0 (ge) added looking in parent |
| 4996 | value = type_engine_find_value( t_base, member->xid ); |
| 4997 | // make sure it's there |
| 4998 | assert( value != NULL ); |
| 4999 | |
| 5000 | // if builtin |
| 5001 | if( value->addr ) |
| 5002 | { |
| 5003 | // emit (1.3.1.0: changed to use getkindof instead of type size) |
| 5004 | emit->append( new Chuck_Instr_Dot_Static_Import_Data( |
| 5005 | value->addr, getkindof(emit->env, member->self->type), emit_addr ) ); |
| 5006 | } |
| 5007 | else |
| 5008 | { |
| 5009 | // emit the type - spencer |
| 5010 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)t_base ) ); |
| 5011 | // find the offset for data |
| 5012 | offset = value->offset; |
| 5013 | // emit the member (1.3.1.0: changed to use getkindof in addition to size) |
| 5014 | emit->append( new Chuck_Instr_Dot_Static_Data( |
| 5015 | offset, member->self->type->size, getkindof(emit->env, member->self->type), emit_addr ) ); |
| 5016 | } |
| 5017 | } |
| 5018 | } |
| 5019 | |
| 5020 | return TRUE; |
| 5021 | } |
| 5022 | |
| 5023 | |
| 5024 | |
| 5025 | |
| 5026 | //----------------------------------------------------------------------------- |
| 5027 | // name: emit_engine_emit_exp_if() |
| 5028 | // desc: ... |
| 5029 | //----------------------------------------------------------------------------- |
| 5030 | t_CKBOOL emit_engine_emit_exp_if( Chuck_Emitter * emit, a_Exp_If exp_if ) |
| 5031 | { |
| 5032 | t_CKBOOL ret = TRUE; |
| 5033 | Chuck_Instr_Branch_Op * op = NULL, * op2 = NULL; |
| 5034 | |
| 5035 | // push the stack, allowing for new local variables |
| 5036 | emit->push_scope(); |
| 5037 | |
| 5038 | // emit the condition |
| 5039 | ret = emit_engine_emit_exp( emit, exp_if->cond ); |
| 5040 | if( !ret ) |
| 5041 | return FALSE; |
| 5042 | |
| 5043 | // type of the condition |
| 5044 | switch( exp_if->cond->type->xid ) |
| 5045 | { |
| 5046 | case te_int: |
| 5047 | // push 0 |
| 5048 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 5049 | op = new Chuck_Instr_Branch_Eq_int( 0 ); |
| 5050 | break; |
| 5051 | case te_float: |
| 5052 | case te_dur: |
| 5053 | case te_time: |
| 5054 | // push 0 |
| 5055 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 5056 | op = new Chuck_Instr_Branch_Eq_double( 0 ); |
| 5057 | break; |
| 5058 | |
| 5059 | default: |
| 5060 | EM_error2( exp_if->cond->where, |
| 5061 | "(emit): internal error: unhandled type '%s' in if condition", |
| 5062 | exp_if->cond->type->base_name.c_str() ); |
| 5063 | return FALSE; |
| 5064 | } |
| 5065 | |
| 5066 | if( !ret ) return FALSE; |
| 5067 | |
| 5068 | // append the op |
| 5069 | emit->append( op ); |
| 5070 | |
| 5071 | // emit the body |
| 5072 | ret = emit_engine_emit_exp( emit, exp_if->if_exp ); |
| 5073 | if( !ret ) |
| 5074 | return FALSE; |
| 5075 | |
| 5076 | // emit the skip to the end |
| 5077 | emit->append( op2 = new Chuck_Instr_Goto(0) ); |
| 5078 | |
| 5079 | // set the op's target |
| 5080 | op->set( emit->next_index() ); |
| 5081 | |
| 5082 | // emit the body |
| 5083 | ret = emit_engine_emit_exp( emit, exp_if->else_exp ); |
| 5084 | if( !ret ) |
| 5085 | return FALSE; |
| 5086 | |
| 5087 | // pop stack |
| 5088 | emit->pop_scope(); |
| 5089 | |
| 5090 | // set the op2's target |
| 5091 | op2->set( emit->next_index() ); |
| 5092 | |
| 5093 | return ret; |
| 5094 | } |
| 5095 | |
| 5096 | |
| 5097 | |
| 5098 | |
| 5099 | //----------------------------------------------------------------------------- |
| 5100 | // name: emit_engine_pre_constructor() |
| 5101 | // desc: emit instructions for type pre-constructor, for Object instantiation |
| 5102 | //----------------------------------------------------------------------------- |
| 5103 | t_CKBOOL emit_engine_pre_constructor( Chuck_Emitter * emit, Chuck_Type * type, a_Ctor_Call ctor_info ) |
| 5104 | { |
| 5105 | // parent first pre constructor |
| 5106 | if( type->parent_type != NULL ) |
| 5107 | { |
| 5108 | // first emit parent pre and base constructor |
| 5109 | emit_engine_pre_constructor( emit, type->parent_type, NULL ); |
| 5110 | } |
| 5111 | |
| 5112 | // pre constructor |
| 5113 | if( type->has_pre_ctor ) |
| 5114 | { |
| 5115 | // make sure |
| 5116 | assert( type->nspc->pre_ctor != NULL ); |
| 5117 | // append instruction |
| 5118 | emit->append( new Chuck_Instr_Pre_Constructor( type->nspc->pre_ctor, |
| 5119 | emit->code->frame->curr_offset ) ); |
| 5120 | } |
| 5121 | |
| 5122 | // constructors |
| 5123 | if( ctor_info && ctor_info->func ) // if named |
| 5124 | { |
| 5125 | // verify |
| 5126 | if( !ctor_info->func->code ) |
| 5127 | { |
| 5128 | EM_error3( "(internal error) missing VM code for constructor '%s'", |
| 5129 | ctor_info->func->signature(FALSE,FALSE).c_str() ); |
| 5130 | return FALSE; |
| 5131 | } |
| 5132 | |
| 5133 | // dup the base pointer ('this' pointer as argument) |
| 5134 | emit->append( new Chuck_Instr_Reg_Dup_Last ); |
| 5135 | // emit arguments |
| 5136 | emit_engine_emit_exp( emit, ctor_info->args, TRUE ); |
| 5137 | // push code |
| 5138 | emit->append( new Chuck_Instr_Reg_Push_Code( ctor_info->func->code ) ); |
| 5139 | // push local stack depth corresponding to local variables |
| 5140 | emit->append( new Chuck_Instr_Reg_Push_Imm( emit->code->frame->curr_offset ) ); |
| 5141 | |
| 5142 | // code format (native or in-language) |
| 5143 | if( ctor_info->func->code->native_func ) |
| 5144 | { |
| 5145 | // append instruction |
| 5146 | emit->append( new Chuck_Instr_Func_Call_Member( kindof_VOID, ctor_info->func, |
| 5147 | CK_FUNC_CALL_THIS_IN_FRONT ) ); |
| 5148 | } |
| 5149 | else |
| 5150 | { |
| 5151 | // push offset |
| 5152 | emit->append( new Chuck_Instr_Func_Call( CK_FUNC_CALL_THIS_IN_FRONT ) ); |
| 5153 | } |
| 5154 | } |
| 5155 | else if( type->ctor_default ) // emit base constructor, if there is one |
| 5156 | { |
| 5157 | // verify |
| 5158 | if( !type->ctor_default->code ) |
| 5159 | { |
| 5160 | EM_error3( "(internal error) missing VM code for base constructor '%s'", |
| 5161 | type->ctor_default->signature(FALSE,FALSE).c_str() ); |
| 5162 | return FALSE; |
| 5163 | } |
| 5164 | |
| 5165 | // emit |
| 5166 | emit->append( new Chuck_Instr_Pre_Constructor( type->ctor_default->code, |
| 5167 | emit->code->frame->curr_offset ) ); |
| 5168 | } |
| 5169 | |
| 5170 | return TRUE; |
| 5171 | } |
| 5172 | |
| 5173 | |
| 5174 | |
| 5175 | |
| 5176 | //----------------------------------------------------------------------------- |
| 5177 | // name: emit_engine_pre_constructor_array() |
| 5178 | // desc: emit instruction for pre-constructing an array of Objects |
| 5179 | //----------------------------------------------------------------------------- |
| 5180 | t_CKBOOL emit_engine_pre_constructor_array( Chuck_Emitter * emit, Chuck_Type * type, a_Ctor_Call ctor_info ) |
| 5181 | { |
| 5182 | // alloc should have put all objects to made in linear list, on stack |
| 5183 | Chuck_Instr_Pre_Ctor_Array_Top * top = NULL; |
| 5184 | Chuck_Instr_Pre_Ctor_Array_Bottom * bottom = NULL; |
| 5185 | // get start index |
| 5186 | t_CKUINT start_index = emit->next_index(); |
| 5187 | // append first part of pre ctor |
| 5188 | emit->append( top = new Chuck_Instr_Pre_Ctor_Array_Top( type ) ); |
| 5189 | // call pre constructor |
| 5190 | emit_engine_pre_constructor( emit, type, ctor_info ); |
| 5191 | // append second part of the pre ctor |
| 5192 | emit->append( bottom = new Chuck_Instr_Pre_Ctor_Array_Bottom ); |
| 5193 | // set the goto of the first one |
| 5194 | top->set( emit->next_index() ); |
| 5195 | // set the goto for the second one |
| 5196 | bottom->set( start_index ); |
| 5197 | // clean up code |
| 5198 | emit->append( new Chuck_Instr_Pre_Ctor_Array_Post ); |
| 5199 | |
| 5200 | return TRUE; |
| 5201 | } |
| 5202 | |
| 5203 | |
| 5204 | |
| 5205 | //----------------------------------------------------------------------------- |
| 5206 | // name: emit_engine_instantiate_object() |
| 5207 | // desc: emit instructions for instantiating object |
| 5208 | //----------------------------------------------------------------------------- |
| 5209 | t_CKBOOL emit_engine_instantiate_object( Chuck_Emitter * emit, Chuck_Type * type, |
| 5210 | a_Ctor_Call ctor_info, a_Array_Sub array, t_CKBOOL is_ref, |
| 5211 | t_CKBOOL is_array_ref ) |
| 5212 | { |
| 5213 | // if array |
| 5214 | if( type->array_depth ) |
| 5215 | { |
| 5216 | // check if the array was an empty decl, e.g., int foo[] |
| 5217 | if( !is_array_ref ) |
| 5218 | { |
| 5219 | // emit indices |
| 5220 | emit_engine_emit_exp( emit, array->exp_list ); |
| 5221 | // emit array allocation |
| 5222 | Chuck_Instr * instr = NULL; |
| 5223 | emit->append( instr = new Chuck_Instr_Array_Alloc( emit->env, |
| 5224 | type->array_depth, type->array_type, |
| 5225 | emit->code->frame->curr_offset, |
| 5226 | is_ref, type ) ); |
| 5227 | instr->set_linepos( array->line ); |
| 5228 | |
| 5229 | // handle constructor; possible to have an instanced array of references |
| 5230 | // Object @ foo[10], for example |
| 5231 | if( isobj( emit->env, type->array_type ) && !is_ref ) |
| 5232 | { |
| 5233 | // call pre constructor for objects in array | TODO verify multi-dim array |
| 5234 | emit_engine_pre_constructor_array( emit, type->array_type, ctor_info ); |
| 5235 | } |
| 5236 | } |
| 5237 | } |
| 5238 | else if( !is_ref ) // not array |
| 5239 | { |
| 5240 | // emit object instantiation code, include pre constructor |
| 5241 | emit->append( new Chuck_Instr_Instantiate_Object_Start( type ) ); |
| 5242 | |
| 5243 | // call pre constructor |
| 5244 | emit_engine_pre_constructor( emit, type, ctor_info ); |
| 5245 | |
| 5246 | // complete object instantiation | 1.5.4.3 (ge) added |
| 5247 | // see Chuck_Instr_Instantiate_Object_Complete::execute() for explanation |
| 5248 | emit->append( new Chuck_Instr_Instantiate_Object_Complete( type ) ); |
| 5249 | } |
| 5250 | |
| 5251 | return TRUE; |
| 5252 | } |
| 5253 | |
| 5254 | |
| 5255 | |
| 5256 | |
| 5257 | //----------------------------------------------------------------------------- |
| 5258 | // name: emit_engine_emit_exp_decl() |
| 5259 | // desc: emit instruction for variable declaration expression |
| 5260 | //----------------------------------------------------------------------------- |
| 5261 | t_CKBOOL emit_engine_emit_exp_decl( Chuck_Emitter * emit, a_Exp_Decl decl, |
| 5262 | t_CKBOOL first_exp ) |
| 5263 | { |
| 5264 | a_Var_Decl_List list = decl->var_decl_list; |
| 5265 | a_Var_Decl var_decl = NULL; |
| 5266 | Chuck_Value * value = NULL; |
| 5267 | Chuck_Type * type = NULL; |
| 5268 | Chuck_Local * local = NULL; |
| 5269 | t_CKBOOL is_obj = FALSE; |
| 5270 | t_CKBOOL is_ref = FALSE; |
| 5271 | t_CKBOOL is_init = FALSE; |
| 5272 | t_CKBOOL is_array = FALSE; |
| 5273 | t_CKBOOL needs_global_ctor = FALSE; |
| 5274 | |
| 5275 | // chuck type (for global emission use) |
| 5276 | t_CKTYPE tglobals = NULL; |
| 5277 | // handling global decls | 1.4.0.1 (jack) added |
| 5278 | te_GlobalType globalType = te_globalTypeNone; |
| 5279 | // declared as global? |
| 5280 | if( decl->is_global ) |
| 5281 | { |
| 5282 | // moved into global section | 1.5.1.3 |
| 5283 | // NOTE inner classes definition (experimental) is not found this way (but works otherwise) |
| 5284 | tglobals = type_engine_find_type( emit->env, decl->type->xid ); |
| 5285 | // check return | 1.5.1.3 (ge) |
| 5286 | if( !tglobals ) |
| 5287 | { |
| 5288 | // fail if type unsupported |
| 5289 | EM_error2( decl->type->where, "unsupported type for global keyword: '%s'", decl->ck_type->base_name.c_str() ); |
| 5290 | EM_error2( 0, "(supported types: int, float, string, Event, UGen, Object)" ); |
| 5291 | return FALSE; |
| 5292 | } |
| 5293 | |
| 5294 | if( isa( tglobals, emit->env->ckt_int ) ) |
| 5295 | { |
| 5296 | globalType = te_globalInt; |
| 5297 | } |
| 5298 | else if( isa( tglobals, emit->env->ckt_float ) ) |
| 5299 | { |
| 5300 | globalType = te_globalFloat; |
| 5301 | } |
| 5302 | else if( isa( tglobals, emit->env->ckt_string ) ) |
| 5303 | { |
| 5304 | globalType = te_globalString; |
| 5305 | } |
| 5306 | else if( isa( tglobals, emit->env->ckt_event ) ) |
| 5307 | { |
| 5308 | // kind-of-event (te_Type for this would be te_user, which is not helpful) |
| 5309 | globalType = te_globalEvent; |
| 5310 | // need to call ctors |
| 5311 | needs_global_ctor = TRUE; |
| 5312 | } |
| 5313 | else if( isa( tglobals, emit->env->ckt_ugen ) ) |
| 5314 | { |
| 5315 | // kind-of-ugen (te_Type might not be te_ugen, so we store globalUGen in our own field) |
| 5316 | globalType = te_globalUGen; |
| 5317 | // need to call ctors |
| 5318 | needs_global_ctor = TRUE; |
| 5319 | } |
| 5320 | else if( isa( tglobals, emit->env->ckt_object ) ) |
| 5321 | { |
| 5322 | // kind-of-object (te_Type might not be te_object, so we store globalObject in our own field) |
| 5323 | globalType = te_globalObject; |
| 5324 | // need to call ctors |
| 5325 | needs_global_ctor = TRUE; |
| 5326 | } |
| 5327 | else |
| 5328 | { |
| 5329 | // fail if type unsupported |
| 5330 | EM_error2( decl->type->where, (std::string("unsupported type for global keyword: ") + tglobals->base_name).c_str() ); |
| 5331 | EM_error2( 0, "(supported types: int, float, string, Event, UGen, Object)" ); |
| 5332 | return FALSE; |
| 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | // loop through vars |
| 5337 | while( list ) |
| 5338 | { |
| 5339 | // the var |
| 5340 | var_decl = list->var_decl; |
| 5341 | // get the value determined in type checker |
| 5342 | value = var_decl->value; |
| 5343 | // get the type of the value |
| 5344 | type = value->type; |
| 5345 | // is the variable a reference |
| 5346 | is_obj = isobj( emit->env, type ); |
| 5347 | // do alloc or not |
| 5348 | is_ref = var_decl->force_ref || decl->type->ref; // 1.5.0.8 (ge) added force_ref for foreach |
| 5349 | // not init |
| 5350 | is_init = FALSE; |
| 5351 | // is array |
| 5352 | is_array = FALSE; |
| 5353 | |
| 5354 | // if this is an object, do instantiation |
| 5355 | if( is_obj ) |
| 5356 | { |
| 5357 | // if this is an array, ... |
| 5358 | if( var_decl->array ) |
| 5359 | { |
| 5360 | // mark as true |
| 5361 | is_array = TRUE; |
| 5362 | // ... then check to see if empty [] |
| 5363 | t_CKBOOL is_array_ref = ( var_decl->array->exp_list == NULL ); |
| 5364 | // ...and only instantiate if NOT empty |
| 5365 | // REFACTOR-2017 TODO: do we want to |
| 5366 | // avoid doing this if the array is global? |
| 5367 | if( !is_array_ref ) |
| 5368 | { |
| 5369 | // only check dependency violations if we are at a context-top-level |
| 5370 | // or class-top-level scope, i.e., not in a function definition |
| 5371 | if( !emit->env->func ) |
| 5372 | { |
| 5373 | // look at the array/actual type |
| 5374 | Chuck_Type * actual_type = type->actual_type; |
| 5375 | // dependency tracking: ensure object instantiation (e.g., Foo foo;) is not invoked |
| 5376 | // before dependencies are met | 1.5.0.8 (ge) added |
| 5377 | // NOTE if actual_type originates from another file, this should behave correctly and return NULL | 1.5.1.1 (ge) fixed |
| 5378 | const Chuck_Value_Dependency * unfulfilled = actual_type->depends.locate( decl->type->where ); |
| 5379 | // at least one unfulfilled dependency |
| 5380 | if( unfulfilled ) |
| 5381 | { |
| 5382 | EM_error2( decl->type->where, |
| 5383 | "'%s' instantiation at this point skips initialization of a needed variable:", |
| 5384 | actual_type->c_name() ); |
| 5385 | EM_error2( unfulfilled->where, |
| 5386 | "...(note: this skipped variable initialization is needed by class '%s')", |
| 5387 | actual_type->c_name() ); |
| 5388 | EM_error2( unfulfilled->use_where, |
| 5389 | "...(note: this is where the variable is used within class '%s')", |
| 5390 | actual_type->c_name() ); |
| 5391 | EM_error2( 0, |
| 5392 | "...(hint: try instantiating '%s' after the variable initialization)", actual_type->c_name() ); |
| 5393 | return FALSE; |
| 5394 | } |
| 5395 | } |
| 5396 | |
| 5397 | // set |
| 5398 | is_init = TRUE; |
| 5399 | // instantiate object, including array |
| 5400 | if( !emit_engine_instantiate_object( emit, type, &var_decl->ctor, var_decl->array, is_ref, is_array_ref ) ) |
| 5401 | return FALSE; |
| 5402 | } |
| 5403 | } |
| 5404 | else if( !is_ref ) |
| 5405 | { |
| 5406 | // only check dependency violations if we are at a context-top-level |
| 5407 | // or class-top-level scope, i.e., not in a function definition |
| 5408 | if( !emit->env->func ) |
| 5409 | { |
| 5410 | // dependency tracking: ensure object instantiation (e.g., Foo foo;) is not invoked |
| 5411 | // before dependencies are met | 1.5.0.8 (ge) added |
| 5412 | // NOTE if type originates from another file, this should behave correctly and return NULL | 1.5.1.1 (ge) fixed |
| 5413 | const Chuck_Value_Dependency * unfulfilled = type->depends.locate( var_decl->where ); |
| 5414 | // at least one unfulfilled |
| 5415 | if( unfulfilled ) |
| 5416 | { |
| 5417 | EM_error2( var_decl->where, |
| 5418 | "'%s' instantiation at this point skips initialization of needed variable:", |
| 5419 | type->c_name() ); |
| 5420 | EM_error2( unfulfilled->where, |
| 5421 | "...(note: this skipped variable initialization is needed by class '%s')", |
| 5422 | type->c_name() ); |
| 5423 | EM_error2( unfulfilled->use_where, |
| 5424 | "...(note: this is where the variable is used within class '%s')", |
| 5425 | type->c_name() ); |
| 5426 | EM_error2( 0, |
| 5427 | "...(hint: try instantiating '%s' after the variable initialization)", type->c_name() ); |
| 5428 | return FALSE; |
| 5429 | } |
| 5430 | } |
| 5431 | |
| 5432 | // REFACTOR-2017: don't emit instructions to instantiate |
| 5433 | // non-array global variables -- they are init/instantiated |
| 5434 | // during emit (see below in this function) |
| 5435 | if( !decl->is_global ) |
| 5436 | { |
| 5437 | // set |
| 5438 | is_init = TRUE; |
| 5439 | // instantiate object (not array) |
| 5440 | if( !emit_engine_instantiate_object( emit, type, &var_decl->ctor, var_decl->array, is_ref, FALSE ) ) |
| 5441 | return FALSE; |
| 5442 | } |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | // if not init and first exp |
| 5447 | /* if( !is_init && first_exp ) |
| 5448 | { |
| 5449 | // push 0 |
| 5450 | if( type->size == sz_INT ) // ISSUE: 64-bit |
| 5451 | emit->append( new Chuck_Instr_Reg_Push_Imm( 0 ) ); |
| 5452 | else if( type->size == sz_FLOAT ) // ISSUE: 64-bit |
| 5453 | emit->append( new Chuck_Instr_Reg_Push_Imm2( 0.0 ) ); |
| 5454 | else if( type->size == sz_VEC2 ) // ISSUE: 64-bit |
| 5455 | emit->append( new Chuck_Instr_Reg_Push_Imm4( 0.0, 0.0 ) ); |
| 5456 | else |
| 5457 | { |
| 5458 | EM_error2( decl->where, |
| 5459 | "(emit): unhandle decl size of '%i'...", |
| 5460 | type->size ); |
| 5461 | return FALSE; |
| 5462 | } |
| 5463 | }*/ |
| 5464 | |
| 5465 | // done with object instantiation |
| 5466 | |
| 5467 | // put in the value |
| 5468 | |
| 5469 | // member |
| 5470 | if( value->is_instance_member ) |
| 5471 | { |
| 5472 | // zero out location in object, and leave addr on operand stack |
| 5473 | // added 1.3.1.0: iskindofint -- on some 64-bit systems, sz_int == sz_FLOAT |
| 5474 | if( type->size == sz_INT && iskindofint(emit->env, type) ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5475 | emit->append( new Chuck_Instr_Alloc_Member_Word( value->offset ) ); |
| 5476 | else if( type->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5477 | emit->append( new Chuck_Instr_Alloc_Member_Word2( value->offset ) ); |
| 5478 | else if( type->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5479 | emit->append( new Chuck_Instr_Alloc_Member_Word4( value->offset ) ); // 1.5.1.7 (ge) NOTE also handles vec2 |
| 5480 | else if( type->size == sz_VEC3 ) // ge: added 1.3.5.3 |
| 5481 | emit->append( new Chuck_Instr_Alloc_Member_Vec3( value->offset ) ); |
| 5482 | else if( type->size == sz_VEC4 ) // ge: added 1.3.5.3 |
| 5483 | emit->append( new Chuck_Instr_Alloc_Member_Vec4( value->offset) ); |
| 5484 | else |
| 5485 | { |
| 5486 | EM_error2( decl->where, |
| 5487 | "(emit): internal error: unhandled decl size of '%i'...", |
| 5488 | type->size ); |
| 5489 | return FALSE; |
| 5490 | } |
| 5491 | } |
| 5492 | else // not member |
| 5493 | { |
| 5494 | // not in class |
| 5495 | if( !emit->env->class_def || !decl->is_static ) |
| 5496 | { |
| 5497 | // allocate a place on the local stack |
| 5498 | // (added 1.3.0.0 -- is_obj for tracking objects ref count on stack) |
| 5499 | local = emit->alloc_local( type->size, value->name, |
| 5500 | is_ref, is_obj, decl->is_global ); |
| 5501 | if( !local ) |
| 5502 | { |
| 5503 | EM_error2( decl->where, |
| 5504 | "(emit): internal error: cannot allocate local '%s'...", |
| 5505 | value->name.c_str() ); |
| 5506 | return FALSE; |
| 5507 | } |
| 5508 | |
| 5509 | // put in the value |
| 5510 | value->offset = local->offset; |
| 5511 | |
| 5512 | // REFACTOR-2017: global declaration |
| 5513 | if( decl->is_global ) |
| 5514 | { |
| 5515 | Chuck_Instr_Alloc_Word_Global * instr = new Chuck_Instr_Alloc_Word_Global(); |
| 5516 | instr->m_name = value->name; |
| 5517 | instr->m_type = globalType; |
| 5518 | instr->set_linepos( decl->line ); |
| 5519 | instr->m_is_array = is_array; |
| 5520 | |
| 5521 | // extra fields for objects that need their ctors called |
| 5522 | if( needs_global_ctor ) |
| 5523 | { |
| 5524 | instr->m_chuck_type = type; |
| 5525 | instr->m_stack_offset = local->offset; |
| 5526 | instr->m_should_execute_ctors = TRUE; |
| 5527 | } |
| 5528 | |
| 5529 | // if it's an event, we need to initialize it and check if the exact type matches |
| 5530 | if( globalType == te_globalEvent ) |
| 5531 | { |
| 5532 | // init and construct it now! |
| 5533 | if( !emit->env->vm()->globals_manager()->init_global_event( value->name, tglobals ) ) |
| 5534 | { |
| 5535 | // if the type doesn't exactly match (different kinds of Event), then fail. |
| 5536 | EM_error2( decl->where, |
| 5537 | "global Event '%s' has different type '%s' than already existing global Event of the same name", |
| 5538 | value->name.c_str(), tglobals->base_name.c_str() ); |
| 5539 | return FALSE; |
| 5540 | } |
| 5541 | } |
| 5542 | // if it's a ugen, we need to initialize it and check if the exact type matches |
| 5543 | else if( globalType == te_globalUGen ) |
| 5544 | { |
| 5545 | // init and construct it now! |
| 5546 | if( !emit->env->vm()->globals_manager()->init_global_ugen( value->name, tglobals ) ) |
| 5547 | { |
| 5548 | // if the type doesn't exactly match (different kinds of UGen), then fail. |
| 5549 | EM_error2( decl->where, |
| 5550 | "global UGen '%s' has different type '%s' than already existing global UGen of the same name", |
| 5551 | value->name.c_str(), tglobals->base_name.c_str() ); |
| 5552 | return FALSE; |
| 5553 | } |
| 5554 | } |
| 5555 | // if it's a ugen, we need to initialize it and check if the exact type matches |
| 5556 | else if( globalType == te_globalObject ) |
| 5557 | { |
| 5558 | // init and construct it now! |
| 5559 | if( !emit->env->vm()->globals_manager()->init_global_object( value->name, tglobals ) ) |
| 5560 | { |
| 5561 | // if the type doesn't exactly match (different types), then fail. |
| 5562 | EM_error2( decl->where, |
| 5563 | "global Object '%s' has different type '%s' than already existing global Object of the same name", |
| 5564 | value->name.c_str(), tglobals->base_name.c_str() ); |
| 5565 | return FALSE; |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | // add instruction |
| 5570 | emit->append( instr ); |
| 5571 | } |
| 5572 | // zero out location in memory, and leave addr on operand stack |
| 5573 | // TODO: this is wrong for static |
| 5574 | // BAD: |
| 5575 | // FIX: |
| 5576 | // added 1.3.1.0: iskindofint -- since on some 64-bit systems, sz_INT == sz_FLOAT |
| 5577 | else if( type->size == sz_INT && iskindofint(emit->env, type) ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5578 | { |
| 5579 | // (added 1.3.0.0 -- is_obj) |
| 5580 | Chuck_Instr_Alloc_Word * instr = new Chuck_Instr_Alloc_Word( local->offset, is_obj ); |
| 5581 | instr->set_linepos( decl->line ); |
| 5582 | emit->append( instr ); |
| 5583 | } |
| 5584 | else if( type->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5585 | { |
| 5586 | Chuck_Instr_Alloc_Word2 * instr = new Chuck_Instr_Alloc_Word2( local->offset ); |
| 5587 | instr->set_linepos( decl->line ); |
| 5588 | emit->append( instr ); |
| 5589 | } |
| 5590 | else if( type->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5591 | { |
| 5592 | emit->append( new Chuck_Instr_Alloc_Word4( local->offset ) ); |
| 5593 | } |
| 5594 | else if( type->size == sz_VEC3 ) // ge: added 1.3.5.3 |
| 5595 | { |
| 5596 | emit->append( new Chuck_Instr_Alloc_Vec3(local->offset) ); |
| 5597 | } |
| 5598 | else if( type->size == sz_VEC4 ) // ge: added 1.3.5.3 |
| 5599 | { |
| 5600 | emit->append( new Chuck_Instr_Alloc_Vec4(local->offset) ); |
| 5601 | } |
| 5602 | else |
| 5603 | { |
| 5604 | EM_error2( decl->where, |
| 5605 | "(emit): internal error: unhandled decl size of '%i'...", |
| 5606 | type->size ); |
| 5607 | return FALSE; |
| 5608 | } |
| 5609 | } |
| 5610 | else // static |
| 5611 | { |
| 5612 | // emit the type |
| 5613 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)emit->env->class_def ) ); |
| 5614 | // emit the static value (1.3.1.0: changed to use getkindof in addition to size) |
| 5615 | emit->append( new Chuck_Instr_Dot_Static_Data( |
| 5616 | value->offset, value->type->size, getkindof(emit->env, value->type), TRUE ) ); |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | // set is_init |
| 5621 | is_init = FALSE; |
| 5622 | |
| 5623 | // if object, assign |
| 5624 | if( is_obj ) |
| 5625 | { |
| 5626 | // if array |
| 5627 | if( var_decl->array ) |
| 5628 | { |
| 5629 | // if not [] |
| 5630 | if( var_decl->array->exp_list ) |
| 5631 | { |
| 5632 | // set |
| 5633 | is_init = TRUE; |
| 5634 | // assign |
| 5635 | emit->append( new Chuck_Instr_Assign_Object ); |
| 5636 | } |
| 5637 | } |
| 5638 | else if( !is_ref ) |
| 5639 | { |
| 5640 | // REFACTOR-2017: don't add an Assign_Object instruction for |
| 5641 | // global objects -- they should be instantiated during emit, |
| 5642 | // not during runtime and therefore don't need an assign instr |
| 5643 | if( !decl->is_global ) |
| 5644 | { |
| 5645 | // set |
| 5646 | is_init = TRUE; |
| 5647 | // assign the object |
| 5648 | emit->append( new Chuck_Instr_Assign_Object ); |
| 5649 | } |
| 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | // if not init and first exp |
| 5654 | /* if( !is_init && first_exp ) |
| 5655 | { |
| 5656 | // if obj |
| 5657 | if( is_obj ) |
| 5658 | emit->append( new Chuck_Instr_Assign_Object ); |
| 5659 | // size 4 primitive |
| 5660 | // added 1.3.1.0: iskindofint -- since on some 64-bit systems, sz_INT == sz_FLOAT |
| 5661 | else if( type->size == sz_INT && iskindofint(type) ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5662 | emit->append( new Chuck_Instr_Assign_Primitive ); |
| 5663 | // size 8 primitive |
| 5664 | else if( type->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5665 | emit->append( new Chuck_Instr_Assign_Primitive2 ); |
| 5666 | // size 16 primitive |
| 5667 | else if( type->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 5668 | emit->append( new Chuck_Instr_Assign_Primitive4 ); |
| 5669 | else |
| 5670 | assert( FALSE ); |
| 5671 | }*/ |
| 5672 | |
| 5673 | list = list->next; |
| 5674 | } |
| 5675 | |
| 5676 | return TRUE; |
| 5677 | } |
| 5678 | |
| 5679 | |
| 5680 | |
| 5681 | |
| 5682 | //----------------------------------------------------------------------------- |
| 5683 | // name: emit_engine_emit_code_segment() |
| 5684 | // desc: ... |
| 5685 | //----------------------------------------------------------------------------- |
| 5686 | t_CKBOOL emit_engine_emit_code_segment( Chuck_Emitter * emit, |
| 5687 | a_Stmt_Code stmt, t_CKBOOL push ) |
| 5688 | { |
| 5689 | a_Stmt_List list = stmt->stmt_list; |
| 5690 | |
| 5691 | // loop through |
| 5692 | while( list ) |
| 5693 | { |
| 5694 | // emit the statement |
| 5695 | if( !emit_engine_emit_stmt( emit, list->stmt ) ) |
| 5696 | return FALSE; |
| 5697 | |
| 5698 | // next |
| 5699 | list = list->next; |
| 5700 | } |
| 5701 | |
| 5702 | // TODO: push |
| 5703 | |
| 5704 | return TRUE; |
| 5705 | } |
| 5706 | |
| 5707 | |
| 5708 | |
| 5709 | |
| 5710 | //----------------------------------------------------------------------------- |
| 5711 | // name: emit_engine_emit_func_def() |
| 5712 | // desc: emit VM instructions for a function definition |
| 5713 | //----------------------------------------------------------------------------- |
| 5714 | t_CKBOOL emit_engine_emit_func_def( Chuck_Emitter * emit, a_Func_Def func_def ) |
| 5715 | { |
| 5716 | // get the func |
| 5717 | Chuck_Func * func = func_def->ck_func; |
| 5718 | // get the value |
| 5719 | Chuck_Value * value = func->value_ref; |
| 5720 | // get the type |
| 5721 | Chuck_Type * type = value->type; |
| 5722 | // local |
| 5723 | Chuck_Local * local = NULL; |
| 5724 | |
| 5725 | // make sure it's the same one |
| 5726 | //Chuck_Func * func2 = emit->env->context->nspc.lookup_func( func_def->name, FALSE ); |
| 5727 | //if( func != func2 ) |
| 5728 | //{ |
| 5729 | // EM_error2( func_def->where, |
| 5730 | // "(emit): ambiguous function resolution for %s...", |
| 5731 | // S_name(func_def->name) ); |
| 5732 | // return FALSE; |
| 5733 | //} |
| 5734 | |
| 5735 | // make sure code is good to go | 1.5.2.0 |
| 5736 | // NOTE now func->code is allocated earlier in scan2_func_def(), this allows |
| 5737 | // the code reference to be available for emission potentially before its |
| 5738 | // contents are emitted |
| 5739 | if( func->code == NULL ) |
| 5740 | { |
| 5741 | EM_error2( func_def->where, |
| 5742 | "(emit): internal error: function '%s' missing code shuttle...", |
| 5743 | func->signature(FALSE,FALSE).c_str() ); |
| 5744 | return FALSE; |
| 5745 | } |
| 5746 | |
| 5747 | // make sure the code is empty |
| 5748 | if( func->code->num_instr ) // updated logic 1.5.2.0 was: if( func->code != NULL ) |
| 5749 | { |
| 5750 | EM_error2( func_def->where, |
| 5751 | "(emit): internal error: function '%s' already emitted...", |
| 5752 | func->signature(FALSE,FALSE).c_str() ); |
| 5753 | return FALSE; |
| 5754 | } |
| 5755 | |
| 5756 | // make sure we are not in a function already |
| 5757 | if( emit->env->func != NULL ) |
| 5758 | { |
| 5759 | EM_error2( func_def->where, |
| 5760 | "(emit): internal error: nested function definition..." ); |
| 5761 | return FALSE; |
| 5762 | } |
| 5763 | |
| 5764 | // if not part of a class |
| 5765 | if( !emit->env->class_def ) |
| 5766 | { |
| 5767 | // put function on stack |
| 5768 | // ge: added FALSE to the 'is_obj' argument, 2012 april (added 1.3.0.0) |
| 5769 | local = emit->alloc_local( value->type->size, value->name, TRUE, FALSE, FALSE ); |
| 5770 | // remember the offset |
| 5771 | value->offset = local->offset; |
| 5772 | // write to mem stack |
| 5773 | emit->append( new Chuck_Instr_Mem_Set_Imm( value->offset, (t_CKUINT)func ) ); |
| 5774 | } |
| 5775 | |
| 5776 | // set the func |
| 5777 | emit->env->func = func; |
| 5778 | // push the current code |
| 5779 | emit->code_stack.push_back( emit->code ); |
| 5780 | // make a new one |
| 5781 | emit->code = new Chuck_Code; |
| 5782 | // name the code | 1.5.1.5 use signature() |
| 5783 | emit->code->name += func->signature(FALSE,FALSE); |
| 5784 | // name the code (older code) |
| 5785 | // emit->code->name = emit->env->class_def ? emit->env->class_def->base_name + "." : ""; |
| 5786 | // emit->code->name += func->name + "(...)"; |
| 5787 | |
| 5788 | // set whether need this |
| 5789 | emit->code->need_this = func->is_member; |
| 5790 | // if static inside class | 1.4.1.0 (ge) added |
| 5791 | emit->code->is_static = func->is_static; |
| 5792 | // keep track of full path (added 1.3.0.0) |
| 5793 | emit->code->filename = emit->context->full_path; |
| 5794 | |
| 5795 | // go through the args |
| 5796 | a_Arg_List a = func_def->arg_list; |
| 5797 | t_CKBOOL is_obj = FALSE; // (added 1.3.0.0) |
| 5798 | t_CKBOOL is_ref = FALSE; |
| 5799 | |
| 5800 | // if member (non-static) function |
| 5801 | if( func->is_member ) |
| 5802 | { |
| 5803 | // get the size |
| 5804 | emit->code->stack_depth += sizeof(t_CKUINT); |
| 5805 | // add this |
| 5806 | // ge: added FALSE to the 'is_obj' argument, 2012 april (added 1.3.0.0) |
| 5807 | if( !emit->alloc_local( sizeof(t_CKUINT), "this", TRUE, FALSE, FALSE ) ) |
| 5808 | { |
| 5809 | EM_error2( a->where, |
| 5810 | "(emit): internal error: cannot allocate local 'this'..." ); |
| 5811 | return FALSE; |
| 5812 | } |
| 5813 | } |
| 5814 | // if static function inside class def | 1.4.1.0 (ge) added |
| 5815 | else if( func->is_static ) |
| 5816 | { |
| 5817 | // get the size (for the TYPE argument) |
| 5818 | emit->code->stack_depth += sizeof(t_CKUINT); |
| 5819 | // create local |
| 5820 | if( !emit->alloc_local( sizeof(t_CKUINT), "@type", TRUE, FALSE, FALSE ) ) |
| 5821 | { |
| 5822 | EM_error2( a->where, |
| 5823 | "(emit): internal error: cannot allocate local '@type'..." ); |
| 5824 | return FALSE; |
| 5825 | } |
| 5826 | } |
| 5827 | |
| 5828 | // ge: 2012 april: added push scope (added 1.3.0.0) |
| 5829 | emit->push_scope(); |
| 5830 | |
| 5831 | // loop through args |
| 5832 | while( a ) |
| 5833 | { |
| 5834 | // get the value |
| 5835 | value = a->var_decl->value; |
| 5836 | // get the type |
| 5837 | type = value->type; |
| 5838 | // is object? (added ge: 2012 april | added 1.3.0.0) |
| 5839 | is_obj = isobj( emit->env, type ); |
| 5840 | // get ref |
| 5841 | is_ref = a->type_decl->ref; |
| 5842 | // get the size |
| 5843 | emit->code->stack_depth += type->size; |
| 5844 | // allocate a place on the local stack |
| 5845 | // ge: added 'is_obj' 2012 april |
| 5846 | local = emit->alloc_local( type->size, value->name, is_ref, is_obj, FALSE ); |
| 5847 | if( !local ) |
| 5848 | { |
| 5849 | EM_error2( a->where, |
| 5850 | "(emit): internal error: cannot allocate local '%s'...", |
| 5851 | value->name.c_str() ); |
| 5852 | return FALSE; |
| 5853 | } |
| 5854 | // remember the offset |
| 5855 | value->offset = local->offset; |
| 5856 | |
| 5857 | // advance |
| 5858 | a = a->next; |
| 5859 | } |
| 5860 | |
| 5861 | // TODO: make sure the calculated stack depth is the same as func_def->stack depth |
| 5862 | // taking into account member function |
| 5863 | |
| 5864 | // add references for objects in the arguments (added 1.3.0.0) |
| 5865 | // NOTE: this isn't in use since currently the caller is reference counting |
| 5866 | // the arguments -- this is to better support sporking, which is more |
| 5867 | // asynchronous. the code below is left in as reference for callee ref counting |
| 5868 | // emit->addref_on_scope(); |
| 5869 | |
| 5870 | // emit the code |
| 5871 | if( !emit_engine_emit_stmt( emit, func_def->code, FALSE ) ) |
| 5872 | return FALSE; |
| 5873 | |
| 5874 | // added by spencer June 2014 (1.3.5.0) | ensure return |
| 5875 | if( func_def->ret_type && func_def->ret_type != emit->env->ckt_void ) |
| 5876 | { |
| 5877 | // 1.5.0.0 (ge) | account for differences in return type size |
| 5878 | // push 0 for the width of the return type in question |
| 5879 | emit->append( new Chuck_Instr_Reg_Push_Zero( func_def->ret_type->size ) ); |
| 5880 | // previously this pushed 0 as int, regardless of return type size |
| 5881 | // emit->append( new Chuck_Instr_Reg_Push_Imm(0) ); |
| 5882 | |
| 5883 | // 1.5.0.0 (ge) | commented out this goto... |
| 5884 | // (okay as long as the next instruction is Chuck_Instr_Func_Return...) |
| 5885 | // Chuck_Instr_Goto * instr = new Chuck_Instr_Goto( 0 ); |
| 5886 | // emit->append( instr ); |
| 5887 | // emit->code->stack_return.push_back( instr ); |
| 5888 | } |
| 5889 | |
| 5890 | // ge: pop scope (2012 april | added 1.3.0.0) |
| 5891 | // TODO: ge 2012 april: pop scope? clean up function arguments? are argument properly ref counted? |
| 5892 | emit->pop_scope(); |
| 5893 | |
| 5894 | // set the index for next instruction for return statements |
| 5895 | for( t_CKUINT i = 0; i < emit->code->stack_return.size(); i++ ) |
| 5896 | emit->code->stack_return[i]->set( emit->next_index() ); |
| 5897 | // clear the return stack |
| 5898 | emit->code->stack_return.clear(); |
| 5899 | // emit return statement |
| 5900 | emit->append( new Chuck_Instr_Func_Return ); |
| 5901 | |
| 5902 | // vm code | 1.5.2.0 (ge) updated to pass in existing func->code |
| 5903 | if( !emit_to_code( emit->code, func->code, emit->dump ) ) |
| 5904 | return FALSE; |
| 5905 | |
| 5906 | // unset the func |
| 5907 | emit->env->func = NULL; |
| 5908 | // delete the code |
| 5909 | CK_SAFE_DELETE( emit->code ); |
| 5910 | // pop the code |
| 5911 | assert( emit->code_stack.size() ); |
| 5912 | emit->code = emit->code_stack.back(); |
| 5913 | emit->code_stack.pop_back(); |
| 5914 | |
| 5915 | return TRUE; |
| 5916 | } |
| 5917 | |
| 5918 | |
| 5919 | |
| 5920 | |
| 5921 | //----------------------------------------------------------------------------- |
| 5922 | // name: emit_engine_emit_class_def() |
| 5923 | // desc: emit bytecode for a class definition |
| 5924 | //----------------------------------------------------------------------------- |
| 5925 | t_CKBOOL emit_engine_emit_class_def( Chuck_Emitter * emit, a_Class_Def class_def ) |
| 5926 | { |
| 5927 | // get the type |
| 5928 | Chuck_Type * type = class_def->type; |
| 5929 | // the return type |
| 5930 | t_CKBOOL ret = TRUE; |
| 5931 | // the class body |
| 5932 | a_Class_Body body = class_def->body; |
| 5933 | |
| 5934 | // make sure the code is empty |
| 5935 | if( type->nspc->pre_ctor != NULL && type->nspc->pre_ctor->instr != NULL ) |
| 5936 | { |
| 5937 | EM_error2( class_def->where, |
| 5938 | "(emit): class '%s' already emitted...", |
| 5939 | type->base_name.c_str() ); |
| 5940 | return FALSE; |
| 5941 | } |
| 5942 | |
| 5943 | // make sure we are not in a class already |
| 5944 | //if( emit->env->class_def != NULL ) |
| 5945 | //{ |
| 5946 | // EM_error2( class_def->where, |
| 5947 | // "(emit): internal error: nested class definition..." ); |
| 5948 | // return FALSE; |
| 5949 | //} |
| 5950 | |
| 5951 | // set the class |
| 5952 | emit->env->class_stack.push_back( emit->env->class_def ); |
| 5953 | emit->env->class_def = type; |
| 5954 | // push the current code |
| 5955 | emit->code_stack.push_back( emit->code ); |
| 5956 | // make a new one |
| 5957 | emit->code = new Chuck_Code; |
| 5958 | // name the code |
| 5959 | emit->code->name = string("class ") + type->base_name; |
| 5960 | // whether code needs this |
| 5961 | emit->code->need_this = TRUE; |
| 5962 | // keep track of full path (added 1.3.0.0) |
| 5963 | emit->code->filename = emit->context->full_path; |
| 5964 | |
| 5965 | // get the size |
| 5966 | emit->code->stack_depth += sizeof(t_CKUINT); |
| 5967 | // add this |
| 5968 | // ge: added TRUE to the 'is_obj' argument, 2012 april (added 1.3.0.0) |
| 5969 | // TODO: verify this is right, and not over-ref counted / cleaned up? |
| 5970 | if( !emit->alloc_local( sizeof(t_CKUINT), "this", TRUE, TRUE, FALSE ) ) |
| 5971 | { |
| 5972 | EM_error2( class_def->where, |
| 5973 | "(emit): internal error: cannot allocate local 'this'..." ); |
| 5974 | return FALSE; |
| 5975 | } |
| 5976 | |
| 5977 | // static code initializer | 1.5.4.3 (ge) added as part of #2024-static-init |
| 5978 | if( type->nspc->static_data_size > 0 ) |
| 5979 | { |
| 5980 | // create code container for emitting |
| 5981 | type->static_code_emit = new Chuck_Code; |
| 5982 | // name the code; copy the class code name |
| 5983 | type->static_code_emit->name = emit->code->name + " (static initializer)"; |
| 5984 | // need this? |
| 5985 | type->static_code_emit->need_this = FALSE; |
| 5986 | // keep track of filename; copy the class code filename |
| 5987 | type->static_code_emit->filename = emit->code->filename; |
| 5988 | } |
| 5989 | |
| 5990 | // emit the body |
| 5991 | while( body && ret ) |
| 5992 | { |
| 5993 | // check the section |
| 5994 | switch( body->section->s_type ) |
| 5995 | { |
| 5996 | case ae_section_stmt: |
| 5997 | ret = emit_engine_emit_stmt_list( emit, body->section->stmt_list ); |
| 5998 | break; |
| 5999 | |
| 6000 | case ae_section_func: |
| 6001 | ret = emit_engine_emit_func_def( emit, body->section->func_def ); |
| 6002 | break; |
| 6003 | |
| 6004 | case ae_section_class: |
| 6005 | ret = emit_engine_emit_class_def( emit, body->section->class_def ); |
| 6006 | //EM_error2( body->section->class_def->where, |
| 6007 | // "nested class definitions are not yet supported..." ); |
| 6008 | //ret = FALSE; |
| 6009 | break; |
| 6010 | } |
| 6011 | |
| 6012 | // move to the next section |
| 6013 | body = body->next; |
| 6014 | } |
| 6015 | |
| 6016 | // if ok |
| 6017 | if( ret ) |
| 6018 | { |
| 6019 | // emit return statement |
| 6020 | emit->append( new Chuck_Instr_Func_Return ); |
| 6021 | |
| 6022 | // ---------------------- |
| 6023 | // *** FYI: previously, as type->info->pre_ctor in both LHS and RHS... |
| 6024 | // type->info->pre_ctor = emit_to_code( emit->code, type->info->pre_ctor, emit->dump ); |
| 6025 | // *** ... could result in extra ref count |
| 6026 | // type->info->pre_ctor->add_ref(); |
| 6027 | // ---------------------- |
| 6028 | // use CK_SAFE_REF_ASSIGN to add_ref RHS then releae LHS | 1.5.1.5 |
| 6029 | // maintain refcount integrity whether type->info->pre_ctor==NULL or not |
| 6030 | // ---------------------- |
| 6031 | CK_SAFE_REF_ASSIGN( type->nspc->pre_ctor, |
| 6032 | emit_to_code( emit->code, type->nspc->pre_ctor, emit->dump ) ); |
| 6033 | |
| 6034 | // ---------------------- |
| 6035 | // static data and code |
| 6036 | // check if we have static data? |
| 6037 | if( type->nspc->static_data_size > 0 ) |
| 6038 | { |
| 6039 | // allocate static |
| 6040 | type->nspc->static_data = new t_CKBYTE[type->nspc->static_data_size]; |
| 6041 | // verify |
| 6042 | if( !type->nspc->static_data ) |
| 6043 | { |
| 6044 | // we have a problem |
| 6045 | CK_FPRINTF_STDERR( |
| 6046 | "[chuck](emitter): OutOfMemory: while allocating static data '%s'\n", type->c_name() ); |
| 6047 | // flag |
| 6048 | ret = FALSE; |
| 6049 | } |
| 6050 | else |
| 6051 | { |
| 6052 | // zero it out |
| 6053 | memset( type->nspc->static_data, 0, type->nspc->static_data_size ); |
| 6054 | } |
| 6055 | |
| 6056 | // static initializer code | 1.5.4.3 (ge) added as part of #2024-static-init |
| 6057 | //----------------------------------------- |
| 6058 | // STATIC VARAIBLE INITIALIZATION SEMANTICS |
| 6059 | //----------------------------------------- |
| 6060 | // static variables must be DECLARED: |
| 6061 | // a) within a class definition |
| 6062 | // b) outside of any class function defintion |
| 6063 | // c) at the outer-most class scope; can not be in nested { } |
| 6064 | //----------------------------------------- |
| 6065 | // COMPILE-TIME checks: |
| 6066 | // 1) a statement containing a static variable declaration |
| 6067 | // CANNOT access member data/functions |
| 6068 | // 2) otherwise, a statement can access both static and member data |
| 6069 | // 3) a statement containing a static variable declaration |
| 6070 | // CANNOT access local (i.e., outside of class def) vars or funcs |
| 6071 | // even in non-public classes |
| 6072 | //----------------------------------------- |
| 6073 | // RUNTIME checks: |
| 6074 | // 4) static initialization statements are run in immediate mode |
| 6075 | // meaning no time advances; this is enforced at runtime; any |
| 6076 | // time advances, including waiting on events, will result in |
| 6077 | // a runtime exception |
| 6078 | //----------------------------------------- |
| 6079 | // see if there is at least one static init instruction |
| 6080 | if( type->static_code_emit->code.size() ) |
| 6081 | { |
| 6082 | // append EOC for the static initializer |
| 6083 | type->static_code_emit->code.push_back( new Chuck_Instr_EOC ); |
| 6084 | // static itor code => vm code |
| 6085 | Chuck_VM_Code * static_code = emit_to_code( type->static_code_emit, NULL, emit->dump ); |
| 6086 | |
| 6087 | // make sure NULL |
| 6088 | assert( type->nspc->static_invoker == NULL ); |
| 6089 | // instantiate an invoker |
| 6090 | type->nspc->static_invoker = new Chuck_VM_SInitInvoker; |
| 6091 | // setup |
| 6092 | type->nspc->static_invoker->setup( type, static_code, emit->env->vm() ); |
| 6093 | // run the static initializer in immediate mode |
| 6094 | type->nspc->static_invoker->invoke( emit->env->vm()->shreduler()->get_current_shred() ); |
| 6095 | // clean up |
| 6096 | CK_SAFE_DELETE( type->nspc->static_invoker ); |
| 6097 | |
| 6098 | // TODO: either defer the initializer to when we have a parent shred OR disallow context-level vars and func calls for static |
| 6099 | // TODO: disallow return in class def body (member and static) |
| 6100 | } |
| 6101 | // clean up the code emit structure |
| 6102 | CK_SAFE_DELETE( type->static_code_emit ); |
| 6103 | } |
| 6104 | } |
| 6105 | |
| 6106 | // check again |
| 6107 | if( !ret ) |
| 6108 | { |
| 6109 | // release | 1.5.1.5 (ge) changed from DELETE to RELEASE |
| 6110 | CK_SAFE_RELEASE( type->nspc->pre_ctor ); |
| 6111 | } |
| 6112 | |
| 6113 | // unset the class |
| 6114 | emit->env->class_def = emit->env->class_stack.back(); |
| 6115 | emit->env->class_stack.pop_back(); |
| 6116 | // delete the code |
| 6117 | CK_SAFE_DELETE( emit->code ); |
| 6118 | // pop the code |
| 6119 | assert( emit->code_stack.size() ); |
| 6120 | emit->code = emit->code_stack.back(); |
| 6121 | emit->code_stack.pop_back(); |
| 6122 | |
| 6123 | return ret; |
| 6124 | } |
| 6125 | |
| 6126 | |
| 6127 | |
| 6128 | |
| 6129 | //----------------------------------------------------------------------------- |
| 6130 | // name: emit_engine_emit_spork() |
| 6131 | // desc: ... |
| 6132 | //----------------------------------------------------------------------------- |
| 6133 | t_CKBOOL emit_engine_emit_spork( Chuck_Emitter * emit, a_Exp_Func_Call exp ) |
| 6134 | { |
| 6135 | // spork |
| 6136 | Chuck_Instr_Mem_Push_Imm * op = NULL; |
| 6137 | |
| 6138 | // if member function, push this |
| 6139 | // TODO: this is a hack - what if exp is not func_call? |
| 6140 | // if( exp->ck_func->is_member ) |
| 6141 | // emit->append( new Chuck_Instr_Reg_Push_This ); |
| 6142 | |
| 6143 | // evaluate args on sporker shred |
| 6144 | if( !emit_engine_emit_func_args( emit, exp ) ) |
| 6145 | return FALSE; |
| 6146 | |
| 6147 | // need to know here so the func can properly emit |
| 6148 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 6149 | exp->func->emit_as_funccall = TRUE; |
| 6150 | |
| 6151 | // emit func pointer on sporker shred |
| 6152 | if( !emit_engine_emit_exp( emit, exp->func ) ) |
| 6153 | { |
| 6154 | // 1.5.4.3 (ge) no longer necessarily an internal error #2024-static-init |
| 6155 | // EM_error2( exp->where, |
| 6156 | // "(emit): internal error in evaluating function call..." ); |
| 6157 | return FALSE; |
| 6158 | } |
| 6159 | |
| 6160 | // push the current code |
| 6161 | emit->code_stack.push_back( emit->code ); |
| 6162 | // make a new one (spork~exp shred) |
| 6163 | emit->code = new Chuck_Code; |
| 6164 | // handle need this |
| 6165 | emit->code->need_this = exp->ck_func->is_member; |
| 6166 | // handle is static (inside class def) | 1.4.1.0 (ge) added |
| 6167 | emit->code->is_static = exp->ck_func->is_static; |
| 6168 | // name it: e.g. spork~foo [line 5] |
| 6169 | std::ostringstream name; |
| 6170 | name << "spork~" |
| 6171 | << exp->ck_func->name.substr( 0, exp->ck_func->name.find( "@" )) |
| 6172 | << " [line " << exp->line << "]"; |
| 6173 | emit->code->name = name.str(); |
| 6174 | // keep track of full path (added 1.3.0.0) |
| 6175 | emit->code->filename = emit->context->full_path; |
| 6176 | // push op |
| 6177 | op = new Chuck_Instr_Mem_Push_Imm( 0 ); |
| 6178 | // emit the stack depth - we don't know this yet |
| 6179 | emit->append( op ); |
| 6180 | |
| 6181 | // call the func on sporkee shred |
| 6182 | if( !emit_engine_emit_exp_func_call( emit, |
| 6183 | exp->ck_func, |
| 6184 | exp->ret_type, |
| 6185 | exp->line, |
| 6186 | exp->where, |
| 6187 | TRUE ) ) return FALSE; |
| 6188 | |
| 6189 | // 1.5.0.0 (ge) | added to pop/release the returned value, which could be an object |
| 6190 | if( iskindofint(emit->env, exp->ret_type) ) |
| 6191 | { |
| 6192 | // is an object? |
| 6193 | if( isobj( emit->env, exp->ret_type) ) |
| 6194 | { emit->append( new Chuck_Instr_Release_Object3_Pop_Int ); } |
| 6195 | else // not an object |
| 6196 | { emit->append( new Chuck_Instr_Reg_Pop_Int ); } |
| 6197 | } |
| 6198 | |
| 6199 | // emit the function call, with special flag |
| 6200 | // if( !emit_engine_emit_exp_func_call( emit, exp, TRUE ) ) |
| 6201 | // return FALSE; |
| 6202 | |
| 6203 | // done |
| 6204 | emit->append( new Chuck_Instr_EOC ); |
| 6205 | // set the stack depth now that we know |
| 6206 | op->set( emit->code->stack_depth ); |
| 6207 | |
| 6208 | // emit it |
| 6209 | Chuck_VM_Code * code = emit_to_code( emit->code, NULL, emit->dump ); |
| 6210 | // remember it |
| 6211 | exp->ck_vm_code = code; |
| 6212 | // add reference |
| 6213 | exp->ck_vm_code->add_ref(); |
| 6214 | // code->name = string("spork~exp"); |
| 6215 | |
| 6216 | // restore the code to sporker shred |
| 6217 | assert( emit->code_stack.size() > 0 ); |
| 6218 | emit->code = emit->code_stack.back(); |
| 6219 | // pop |
| 6220 | emit->code_stack.pop_back(); |
| 6221 | |
| 6222 | a_Exp e = exp->args; |
| 6223 | t_CKUINT size = 0; |
| 6224 | while( e ) { size += e->cast_to ? e->cast_to->size : e->type->size; e = e->next; } |
| 6225 | |
| 6226 | // handle member function |
| 6227 | // TODO: this is a hack - what if exp is not func_call? |
| 6228 | // if( emit->code->need_this ) |
| 6229 | // size += sz_INT; // (changed 1.3.1.0: 4 to sz_INT) |
| 6230 | |
| 6231 | // emit instruction that will put the code on the stack |
| 6232 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)code ) ); |
| 6233 | // emit spork instruction - this will copy, func, args, this |
| 6234 | emit->append( new Chuck_Instr_Spork( size ) ); |
| 6235 | |
| 6236 | return TRUE; |
| 6237 | } |
| 6238 | |
| 6239 | |
| 6240 | |
| 6241 | |
| 6242 | //----------------------------------------------------------------------------- |
| 6243 | // name: emit_engine_emit_symbol() |
| 6244 | // desc: emit a symbol into code |
| 6245 | //----------------------------------------------------------------------------- |
| 6246 | t_CKBOOL emit_engine_emit_symbol( Chuck_Emitter * emit, S_Symbol symbol, |
| 6247 | Chuck_Value * v, t_CKBOOL emit_var, |
| 6248 | t_CKUINT line, t_CKUINT where, a_Exp_Primary exp ) |
| 6249 | { |
| 6250 | // look up the value |
| 6251 | // Chuck_Value * v = emit->env->curr->lookup_value( symbol, TRUE ); |
| 6252 | // it should be there |
| 6253 | if( !v ) |
| 6254 | { |
| 6255 | // internal error |
| 6256 | EM_error2( where, |
| 6257 | "(emit): internal error: undefined symbol '%s'...", |
| 6258 | S_name(symbol) ); |
| 6259 | return FALSE; |
| 6260 | } |
| 6261 | |
| 6262 | // if global, find what type |
| 6263 | // (due to user classes, this info is only available during emit) |
| 6264 | te_GlobalType global_type = te_globalTypeNone; |
| 6265 | if( v->is_global ) |
| 6266 | { |
| 6267 | if( isa( v->type, emit->env->ckt_int ) ) |
| 6268 | { |
| 6269 | global_type = te_globalInt; |
| 6270 | } |
| 6271 | else if( isa( v->type, emit->env->ckt_float ) ) |
| 6272 | { |
| 6273 | global_type = te_globalFloat; |
| 6274 | } |
| 6275 | else if( isa( v->type, emit->env->ckt_string ) ) |
| 6276 | { |
| 6277 | global_type = te_globalString; |
| 6278 | } |
| 6279 | else if( isa( v->type, emit->env->ckt_event ) ) |
| 6280 | { |
| 6281 | global_type = te_globalEvent; |
| 6282 | } |
| 6283 | else if( isa( v->type, emit->env->ckt_ugen ) ) |
| 6284 | { |
| 6285 | global_type = te_globalUGen; |
| 6286 | } |
| 6287 | else if( isa( v->type, emit->env->ckt_array ) ) |
| 6288 | { |
| 6289 | global_type = te_globalArraySymbol; |
| 6290 | } |
| 6291 | else if( isa( v->type, emit->env->ckt_object ) ) |
| 6292 | { |
| 6293 | global_type = te_globalObject; |
| 6294 | } |
| 6295 | else |
| 6296 | { |
| 6297 | // internal error |
| 6298 | EM_error2( where, |
| 6299 | "(emit): internal error: unknown global type '%s'...", |
| 6300 | v->type->base_name.c_str() ); |
| 6301 | return FALSE; |
| 6302 | } |
| 6303 | } |
| 6304 | |
| 6305 | // if part of class - this only works because x.y is handled separately |
| 6306 | if( v->owner_class && (v->is_instance_member || v->is_static) ) |
| 6307 | { |
| 6308 | // make sure talking about the same class |
| 6309 | // this doesn't work since the owner class could be a super class |
| 6310 | // assert( v->owner_class == emit->env->class_def ); |
| 6311 | // |
| 6312 | // try this (thanks Robin Davies) |
| 6313 | assert( isa( emit->env->class_def, v->owner_class ) /* || is_global( v->owner_class ) */ ); |
| 6314 | |
| 6315 | // emit as this.v |
| 6316 | // BUG: passing "this" below might be bad if the exp is freed and string with it |
| 6317 | // ACTUALLY: this might be ok, since this would result in a S_symbol which allocates |
| 6318 | // new memory for the xid that would be "this" | 1.5.1.3 |
| 6319 | a_Exp base = new_exp_from_id( (char *)"this", (uint32_t)line, (uint32_t)where ); |
| 6320 | a_Exp dot = new_exp_from_member_dot( base, (char *)v->name.c_str(), (uint32_t)line, (uint32_t)where, (uint32_t)where ); // TODO: 1.5.0.5 check this last memberPos |
| 6321 | |
| 6322 | // add information |
| 6323 | base->type = v->owner_class; |
| 6324 | CK_SAFE_ADD_REF( base->type ); // 1.5.1.3 |
| 6325 | dot->type = v->type; |
| 6326 | CK_SAFE_ADD_REF( dot->type ); // 1.5.1.3 |
| 6327 | dot->dot_member.t_base = v->owner_class; |
| 6328 | CK_SAFE_ADD_REF( dot->dot_member.t_base ); // 1.5.1.3 |
| 6329 | dot->emit_var = emit_var; |
| 6330 | |
| 6331 | // propagate whether this should be emitted as a func call (vs. a function value only) |
| 6332 | // 1.5.4.3 (ge) added as part of #2024-func-call-update |
| 6333 | dot->emit_as_funccall = exp->self->emit_as_funccall; |
| 6334 | |
| 6335 | // emit it |
| 6336 | if( !emit_engine_emit_exp_dot_member( emit, &dot->dot_member ) ) |
| 6337 | { |
| 6338 | // 1.5.4.3 (ge) no longer necessarily an internal error #2024-static-init |
| 6339 | // internal error |
| 6340 | // EM_error2( where, |
| 6341 | // "(emit): internal error: symbol transformation failed..." ); |
| 6342 | return FALSE; |
| 6343 | } |
| 6344 | |
| 6345 | // delete locally-allocated case and dot | 1.5.1.3 |
| 6346 | // FYI: dot will implicitly delete base, so ONLY delete_exp( dot ) |
| 6347 | // TODO: is this safe? hopefully nothing within exp_dot_member() keeps a reference to either base or dot |
| 6348 | delete_exp( dot ); |
| 6349 | // zero out for good measure |
| 6350 | base = NULL; dot = NULL; |
| 6351 | |
| 6352 | // done |
| 6353 | return TRUE; |
| 6354 | } |
| 6355 | |
| 6356 | // var or value |
| 6357 | if( emit_var ) |
| 6358 | { |
| 6359 | // check for const | 1.5.4.4 (ge) added |
| 6360 | if( v->is_const ) |
| 6361 | { |
| 6362 | EM_error2( exp->where, |
| 6363 | "cannot modify constant variable '%s'", S_name( exp->var ) ); |
| 6364 | return FALSE; |
| 6365 | } |
| 6366 | |
| 6367 | // emit as addr |
| 6368 | if( v->is_global ) |
| 6369 | { |
| 6370 | emit->append( new Chuck_Instr_Reg_Push_Global_Addr( v->name, global_type ) ); |
| 6371 | } |
| 6372 | else |
| 6373 | { |
| 6374 | emit->append( new Chuck_Instr_Reg_Push_Mem_Addr( v->offset, v->is_context_global ) ); |
| 6375 | } |
| 6376 | } |
| 6377 | else |
| 6378 | { |
| 6379 | // special case |
| 6380 | if( v->func_ref ) |
| 6381 | { |
| 6382 | // 1.4.1.0 (ge) added for new static calling convention of passing |
| 6383 | // the type as the first parameter |
| 6384 | if( v->func_ref->is_static ) |
| 6385 | { |
| 6386 | // push the type pointer |
| 6387 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)v->owner_class ) ); |
| 6388 | } |
| 6389 | // push function pointer |
| 6390 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)v->func_ref ) ); |
| 6391 | } |
| 6392 | else if( v->is_global ) |
| 6393 | { |
| 6394 | Chuck_Instr_Reg_Push_Global * instr = |
| 6395 | new Chuck_Instr_Reg_Push_Global( v->name, global_type ); |
| 6396 | instr->set_linepos( line ); |
| 6397 | emit->append( instr ); |
| 6398 | } |
| 6399 | else if( isa(v->type, emit->env->ckt_class) && |
| 6400 | v->owner->lookup_type( v->name, 0, TRUE ) ) // 1.5.4.4 (ge) if the value is of type Type (e.g., <<< SinOsc >>>;) |
| 6401 | { |
| 6402 | // look up the type by name in the value's owner namespace, climb==0, stayWithinClassDef==TRUE |
| 6403 | Chuck_Type * type = v->owner->lookup_type( v->name, 0, TRUE ); |
| 6404 | // append the value pointer directly | 1.5.4.4 (ge) added |
| 6405 | emit->append( new Chuck_Instr_Reg_Push_Imm( (t_CKUINT)type ) ); |
| 6406 | } |
| 6407 | // check size |
| 6408 | // (added 1.3.1.0: iskindofint -- since in some 64-bit systems, sz_INT == sz_FLOAT) |
| 6409 | else if( v->type->size == sz_INT && iskindofint(emit->env, v->type) ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 6410 | emit->append( new Chuck_Instr_Reg_Push_Mem( v->offset, v->is_context_global ) ); |
| 6411 | else if( v->type->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 6412 | emit->append( new Chuck_Instr_Reg_Push_Mem2( v->offset, v->is_context_global ) ); |
| 6413 | else if( v->type->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) |
| 6414 | emit->append( new Chuck_Instr_Reg_Push_Mem4( v->offset, v->is_context_global ) ); // 1.5.1.7 (ge) NOTE also handles vec2 |
| 6415 | else if( v->type->size == sz_VEC3 ) // ge: added 1.3.5.3 |
| 6416 | emit->append( new Chuck_Instr_Reg_Push_Mem_Vec3( v->offset, v->is_context_global ) ); |
| 6417 | else if( v->type->size == sz_VEC4 ) // ge: added 1.3.5.3 |
| 6418 | emit->append( new Chuck_Instr_Reg_Push_Mem_Vec4( v->offset, v->is_context_global ) ); |
| 6419 | else |
| 6420 | { |
| 6421 | // internal error |
| 6422 | EM_error2( where, |
| 6423 | "(emit): internal error: symbol '%s' has size '%i'...", |
| 6424 | S_name(symbol), v->type->size ); |
| 6425 | return FALSE; |
| 6426 | } |
| 6427 | } |
| 6428 | |
| 6429 | return TRUE; |
| 6430 | } |
| 6431 | |
| 6432 | |
| 6433 | |
| 6434 | |
| 6435 | //----------------------------------------------------------------------------- |
| 6436 | // name: addref_on_scope() |
| 6437 | // desc: add references to locals on current scope (added 1.3.0.0) |
| 6438 | //----------------------------------------------------------------------------- |
| 6439 | void Chuck_Emitter::addref_on_scope() |
| 6440 | { |
| 6441 | // sanity |
| 6442 | assert( code != NULL ); |
| 6443 | // clear locals |
| 6444 | locals.clear(); |
| 6445 | // get the current scope |
| 6446 | code->frame->get_scope( locals ); |
| 6447 | |
| 6448 | // iterate over locals |
| 6449 | for( int i = 0; i < locals.size(); i++ ) |
| 6450 | { |
| 6451 | // get it |
| 6452 | Chuck_Local * local = locals[i]; |
| 6453 | // check to see if it's an object |
| 6454 | if( local->is_obj ) |
| 6455 | { |
| 6456 | // REFACTOR-2017: Don't do if local is global |
| 6457 | // Note: I don't think addref_on_scope() is used anywhere, |
| 6458 | // but I am doing this to mirror pop_scope() below, which IS used |
| 6459 | if( !local->is_global ) |
| 6460 | { |
| 6461 | // emit instruction to add reference |
| 6462 | this->append( new Chuck_Instr_AddRef_Object2( local->offset ) ); |
| 6463 | } |
| 6464 | } |
| 6465 | } |
| 6466 | } |
| 6467 | |
| 6468 | |
| 6469 | |
| 6470 | |
| 6471 | //----------------------------------------------------------------------------- |
| 6472 | // name: traverse_scope_on_jump() | 1.5.0.0 (ge) added |
| 6473 | // desc: traverse the scope and emit instructions on a jump statement: |
| 6474 | // 'return', 'break', 'continue' |
| 6475 | // this is done to ensure proper cleanup of objects on scope frames |
| 6476 | //----------------------------------------------------------------------------- |
| 6477 | void Chuck_Emitter::traverse_scope_on_jump( ) |
| 6478 | { |
| 6479 | // this gotta be the case |
| 6480 | assert( code != NULL ); |
| 6481 | |
| 6482 | // clear locals |
| 6483 | locals.clear(); |
| 6484 | // get locals (localOnly=false to get all locals at this point in 'code') |
| 6485 | code->frame->get_scope( locals, false ); |
| 6486 | |
| 6487 | // loop over |
| 6488 | for( int i = 0; i < locals.size(); i++ ) |
| 6489 | { |
| 6490 | // get local |
| 6491 | Chuck_Local * local = locals[i]; |
| 6492 | // skip scope boundary (denoted by NULL) |
| 6493 | if( !local ) continue; |
| 6494 | |
| 6495 | // check to see if it's an object (but not global) |
| 6496 | // (REFACTOR-2017: don't release global objects) |
| 6497 | if( local->is_obj && !local->is_global ) |
| 6498 | { |
| 6499 | // emit instruction to release the object |
| 6500 | this->append( new Chuck_Instr_Release_Object2( local->offset ) ); |
| 6501 | } |
| 6502 | } |
| 6503 | |
| 6504 | // clear it |
| 6505 | locals.clear(); |
| 6506 | } |
| 6507 | |
| 6508 | |
| 6509 | |
| 6510 | |
| 6511 | //----------------------------------------------------------------------------- |
| 6512 | // name: pop_scope() |
| 6513 | // desc: ... |
| 6514 | //----------------------------------------------------------------------------- |
| 6515 | void Chuck_Emitter::pop_scope( ) |
| 6516 | { |
| 6517 | // sanity |
| 6518 | assert( code != NULL ); |
| 6519 | // clear locals |
| 6520 | locals.clear(); |
| 6521 | // get locals |
| 6522 | code->frame->pop_scope( locals ); |
| 6523 | |
| 6524 | // ge (2012 april): attempt to free locals (added 1.3.0.0) |
| 6525 | // was -- TODO: free locals -- chievo! |
| 6526 | for( int i = 0; i < locals.size(); i++ ) |
| 6527 | { |
| 6528 | // get local |
| 6529 | Chuck_Local * local = locals[i]; |
| 6530 | // CK_STDCERR << "local: " << local->offset << " " << local->is_obj << CK_STDENDL; |
| 6531 | // check to see if it's an object |
| 6532 | if( local->is_obj ) |
| 6533 | { |
| 6534 | // (REFACTOR-2017: don't release global objects) |
| 6535 | if( !local->is_global ) |
| 6536 | { |
| 6537 | // emit instruction to release the object |
| 6538 | this->append( new Chuck_Instr_Release_Object2( local->offset ) ); |
| 6539 | } |
| 6540 | } |
| 6541 | |
| 6542 | // reclaim local; null out to be safe |
| 6543 | CK_SAFE_DELETE( local ); locals[i] = NULL; |
| 6544 | } |
| 6545 | |
| 6546 | // clear it |
| 6547 | locals.clear(); |
| 6548 | } |
| 6549 | |
| 6550 | |
| 6551 | |
| 6552 | |
| 6553 | //----------------------------------------------------------------------------- |
| 6554 | // name: find_dur() |
| 6555 | // desc: ... |
| 6556 | //----------------------------------------------------------------------------- |
| 6557 | t_CKBOOL Chuck_Emitter::find_dur( const string & name, t_CKDUR * out ) |
| 6558 | { |
| 6559 | // sanity |
| 6560 | assert( env != NULL ); |
| 6561 | assert( out != NULL ); |
| 6562 | |
| 6563 | // zero |
| 6564 | *out = 0.0; |
| 6565 | // get value from env |
| 6566 | Chuck_Value * value = env->global()->lookup_value( name, FALSE ); |
| 6567 | if( !value || !equals( value->type, env->ckt_dur ) ) return FALSE; |
| 6568 | // copy |
| 6569 | *out = *( (t_CKDUR *)value->addr ); |
| 6570 | |
| 6571 | return TRUE; |
| 6572 | } |