Debian Codegraph @

src/core/chuck_instr.cpp

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_instr.cpp
28 // desc: chuck virtual machine instruction set
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // date: Autumn 2002
32 //-----------------------------------------------------------------------------
33 #include "chuck_instr.h"
34 #include "chuck_absyn.h" // for op2str | 1.5.1.5
35 #include "chuck_type.h"
36 #include "chuck_lang.h"
37 #include "chuck_vm.h"
38 #include "chuck_ugen.h"
39 #include "chuck_dl.h"
40 #include "chuck_io.h"
41 #include "chuck_errmsg.h"
42 #include "chuck_globals.h" // added 1.4.1.0
43 #include "util_math.h"
44 #include "util_string.h"
45
46 #include <math.h>
47 #include <limits.h>
48 #include <typeinfo>
49 using namespace std;
50
51
52
53
54 //-----------------------------------------------------------------------------
55 // name: Chuck_Instr()
56 // desc: base class constructor
57 //-----------------------------------------------------------------------------
58 Chuck_Instr::Chuck_Instr()
59 {
60 // set linepos to 0 so we can tell later whether it has been set properly
61 m_linepos = 0;
62 // default codestr
63 m_codestr_pre = NULL;
64 m_codestr_post = NULL;
65 }
66
67
68
69
70 //-----------------------------------------------------------------------------
71 // name: ~Chuck_Instr()
72 // desc: destructor
73 //-----------------------------------------------------------------------------
74 Chuck_Instr::~Chuck_Instr()
75 {
76 CK_SAFE_DELETE( m_codestr_pre );
77 CK_SAFE_DELETE( m_codestr_post );
78 }
79
80
81
82
83 //-----------------------------------------------------------------------------
84 // name: name()
85 // desc: ...
86 //-----------------------------------------------------------------------------
87 const char * Chuck_Instr::name() const
88 {
89 #ifndef __CHUNREAL_ENGINE__
90 return mini_type( typeid(*this).name() );
91 #else
92 // 1.5.0.0 (ge) | #chureal
93 // Unreal Engine seems to disable C++ run time type information
94 return "[chunreal type info disabled]";
95 #endif // #ifndef __CHUNREAL_ENGINE__
96 }
97
98
99
100
101 //-----------------------------------------------------------------------------
102 // name: prepend_codestr()
103 // desc: prepend codestr associated with this instruction
104 // only certain instructions (e.g., start of stmts) have this
105 //-----------------------------------------------------------------------------
106 void Chuck_Instr::prepend_codestr( const string & str )
107 {
108 // alloc if needed
109 if( !m_codestr_pre ) m_codestr_pre = new vector<string>();
110 // prepend to pre
111 m_codestr_pre->insert( m_codestr_pre->begin(), str );
112 }
113
114
115
116
117 //-----------------------------------------------------------------------------
118 // name: append_codestr()
119 // desc: append codestr associated with this instruction
120 // only certain instructions (e.g., start of stmts) have this
121 //-----------------------------------------------------------------------------
122 void Chuck_Instr::append_codestr( const string & str )
123 {
124 // alloc if needed
125 if( !m_codestr_post ) m_codestr_post = new vector<string>();
126 // append to post
127 m_codestr_post->push_back( str );
128 }
129
130
131
132
133 //-----------------------------------------------------------------------------
134 // name: set_linepos()
135 // desc: store line position for error messages
136 //-----------------------------------------------------------------------------
137 void Chuck_Instr::set_linepos(t_CKUINT linepos)
138 {
139 m_linepos = linepos;
140 }
141
142
143
144
145 //-----------------------------------------------------------------------------
146 // name: ck_handle_overflow()
147 // desc: take evasive action upon detecting stack overflow
148 //-----------------------------------------------------------------------------
149 void ck_handle_overflow( Chuck_VM_Shred * shred, Chuck_VM * vm, const string & reason )
150 {
151 // we have a problem
152 EM_exception( "StackOverflow in shred[id=%lu:%s] [pc=%lu]",
153 shred->xid, shred->name.c_str(), shred->pc );
154 if( reason != "" )
155 EM_error3( "...(possible cause: %s)", reason.c_str() );
156
157 // do something!
158 shred->is_running = FALSE;
159 shred->is_done = TRUE;
160 }
161
162
163 #pragma mark === Integer Arithmetic ===
164
165
166 //-----------------------------------------------------------------------------
167 // name: execute()
168 // desc: ...
169 //-----------------------------------------------------------------------------
170 void Chuck_Instr_Add_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
171 {
172 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
173 pop_( sp, 2 );
174 push_( sp, val_(sp) + val_(sp+1) );
175 }
176
177
178
179
180 //-----------------------------------------------------------------------------
181 // name: execute()
182 // desc: ...
183 //-----------------------------------------------------------------------------
184 void Chuck_Instr_PreInc_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
185 {
186 // t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
187 t_CKINT **& reg_sp = (t_CKINT **&)shred->reg->sp;
188 t_CKINT *& the_sp = (t_CKINT *&)shred->reg->sp;
189
190 // pointer
191 pop_( reg_sp, 1 );
192 // increment value
193 (**(reg_sp))++;
194 // value on stack
195 push_( the_sp, **(reg_sp) );
196 }
197
198
199
200
201 //-----------------------------------------------------------------------------
202 // name: execute()
203 // desc: ...
204 //-----------------------------------------------------------------------------
205 void Chuck_Instr_PostInc_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
206 {
207 // t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
208 t_CKINT **& reg_sp = (t_CKINT **&)shred->reg->sp;
209 t_CKINT *& the_sp = (t_CKINT *&)shred->reg->sp;
210 t_CKINT * ptr;
211
212 // pointer
213 pop_( reg_sp, 1 );
214 // copy
215 ptr = *reg_sp;
216 // value on stack
217 push_( the_sp, **(reg_sp) );
218 // increment value
219 (*(ptr))++;
220 }
221
222
223
224
225 //-----------------------------------------------------------------------------
226 // name: execute()
227 // desc: ...
228 //-----------------------------------------------------------------------------
229 void Chuck_Instr_PreDec_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
230 {
231 // t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
232 t_CKINT **& reg_sp = (t_CKINT **&)shred->reg->sp;
233 t_CKINT *& the_sp = (t_CKINT *&)shred->reg->sp;
234
235 // pointer
236 pop_( reg_sp, 1 );
237 // decrement value
238 (**(reg_sp))--;
239 // value on stack
240 push_( the_sp, **(reg_sp) );
241 }
242
243
244
245
246 //-----------------------------------------------------------------------------
247 // name: execute()
248 // desc: ...
249 //-----------------------------------------------------------------------------
250 void Chuck_Instr_PostDec_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
251 {
252 // t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
253 t_CKINT **& reg_sp = (t_CKINT **&)shred->reg->sp;
254 t_CKINT *& the_sp = (t_CKINT *&)shred->reg->sp;
255 t_CKINT * ptr;
256
257 // pointer
258 pop_( reg_sp, 1 );
259 // copy
260 ptr = *reg_sp;
261 // value on stack
262 push_( the_sp, **(reg_sp) );
263 // decrement value
264 (*(ptr))--;
265 }
266
267
268
269
270 //-----------------------------------------------------------------------------
271 // name: class Chuck_Instr_Dec_int_Addr
272 // desc: ...
273 //-----------------------------------------------------------------------------
274 void Chuck_Instr_Dec_int_Addr::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
275 {
276 // decrement value
277 (*((t_CKINT *)(m_val)))--;
278 }
279
280
281
282
283 //-----------------------------------------------------------------------------
284 // name: exexute()
285 // desc: ...
286 //-----------------------------------------------------------------------------
287 void Chuck_Instr_Complement_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
288 {
289 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
290 (*(sp-1)) = ~(*(sp-1));
291 }
292
293
294
295
296 //-----------------------------------------------------------------------------
297 // name: execute()
298 // desc: module two ints
299 //-----------------------------------------------------------------------------
300 void Chuck_Instr_Mod_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
301 {
302 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
303 pop_( sp, 2 );
304 if( val_(sp+1) == 0 ) goto mod_zero;
305 push_( sp, val_(sp) % val_(sp+1) );
306
307 return;
308
309 mod_zero:
310 // we have a problem | 1.5.4.0 (nshaheed) throw exception
311 EM_exception(
312 "ModuloByZero: on line[%lu] in shred[id=%lu:%s]",
313 m_linepos, shred->xid, shred->name.c_str());
314 goto done;
315
316 done:
317 // do something!
318 shred->is_running = FALSE;
319 shred->is_done = TRUE;
320 }
321
322
323
324
325 //-----------------------------------------------------------------------------
326 // name: execute()
327 // desc: module two ints (in reverse)
328 //-----------------------------------------------------------------------------
329 void Chuck_Instr_Mod_int_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
330 {
331 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
332 pop_( sp, 2 );
333 if( val_(sp) == 0 ) goto mod_zero;
334 push_( sp, val_(sp+1) % val_(sp) );
335
336 return;
337
338 mod_zero:
339 // we have a problem | 1.5.4.0 (nshaheed) throw exception
340 EM_exception(
341 "ModuloByZero: on line[%lu] in shred[id=%lu:%s]",
342 m_linepos, shred->xid, shred->name.c_str());
343 goto done;
344
345 done:
346 // do something!
347 shred->is_running = FALSE;
348 shred->is_done = TRUE;
349 }
350
351
352
353
354 //-----------------------------------------------------------------------------
355 // name: execute()
356 // desc: ...
357 //-----------------------------------------------------------------------------
358 void Chuck_Instr_Minus_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
359 {
360 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
361 pop_( sp, 2 );
362 push_( sp, val_(sp) - val_(sp+1) );
363 }
364
365
366
367
368 //-----------------------------------------------------------------------------
369 // name: execute()
370 // desc: ...
371 //-----------------------------------------------------------------------------
372 void Chuck_Instr_Minus_int_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
373 {
374 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
375 pop_( sp, 2 );
376 push_( sp, val_(sp+1) - val_(sp) );
377 }
378
379
380
381
382 //-----------------------------------------------------------------------------
383 // name: execute()
384 // desc: ...
385 //-----------------------------------------------------------------------------
386 void Chuck_Instr_Times_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
387 {
388 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
389 pop_( sp, 2 );
390 push_( sp, val_(sp) * val_(sp+1) );
391 }
392
393
394
395
396 //-----------------------------------------------------------------------------
397 // name: execute()
398 // desc: ...
399 //-----------------------------------------------------------------------------
400 void Chuck_Instr_Divide_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
401 {
402 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
403 pop_( sp, 2 );
404 if( val_(sp+1) == 0 ) goto div_zero;
405 push_( sp, val_(sp) / val_(sp+1) );
406
407 return;
408 div_zero:
409 // we have a problem
410 EM_exception(
411 "DivideByZero: on line[%lu] in shred[id=%lu:%s]",
412 m_linepos, shred->xid, shred->name.c_str());
413 goto done;
414
415 done:
416 // do something!
417 shred->is_running = FALSE;
418 shred->is_done = TRUE;
419 }
420
421
422
423
424 //-----------------------------------------------------------------------------
425 // name: execute()
426 // desc: ...
427 //-----------------------------------------------------------------------------
428 void Chuck_Instr_Divide_int_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
429 {
430 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
431 pop_( sp, 2 );
432 if( val_(sp) == 0 ) goto div_zero;
433 push_( sp, val_(sp+1) / val_(sp) );
434
435 return;
436 div_zero:
437 // we have a problem
438 EM_exception(
439 "DivideByZero: on line[%lu] in shred[id=%lu:%s]",
440 m_linepos, shred->xid, shred->name.c_str());
441 goto done;
442
443 done:
444 // do something!
445 shred->is_running = FALSE;
446 shred->is_done = TRUE;
447 }
448
449
450
451 #pragma mark === Float Arithmetic ===
452
453
454 //-----------------------------------------------------------------------------
455 // name: execute()
456 // desc: ...
457 //-----------------------------------------------------------------------------
458 void Chuck_Instr_Add_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
459 {
460 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
461 pop_( sp, 2 );
462 push_( sp, val_(sp) + val_(sp+1) );
463 }
464
465
466
467
468 //-----------------------------------------------------------------------------
469 // name: execute()
470 // desc: ...
471 //-----------------------------------------------------------------------------
472 void Chuck_Instr_Minus_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
473 {
474 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
475 pop_( sp, 2 );
476 push_( sp, val_(sp) - val_(sp+1) );
477 }
478
479
480
481
482 //-----------------------------------------------------------------------------
483 // name: execute()
484 // desc: ...
485 //-----------------------------------------------------------------------------
486 void Chuck_Instr_Minus_double_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
487 {
488 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
489 pop_( sp, 2 );
490 push_( sp, val_(sp+1) - val_(sp) );
491 }
492
493
494
495
496 //-----------------------------------------------------------------------------
497 // name: execute()
498 // desc: ...
499 //-----------------------------------------------------------------------------
500 void Chuck_Instr_Times_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
501 {
502 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
503 pop_( sp, 2 );
504 push_( sp, val_(sp) * val_(sp+1) );
505 }
506
507
508
509
510 //-----------------------------------------------------------------------------
511 // name: execute()
512 // desc: ...
513 //-----------------------------------------------------------------------------
514 void Chuck_Instr_Divide_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
515 {
516 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
517 pop_( sp, 2 );
518 push_( sp, val_(sp) / val_(sp+1) );
519 }
520
521
522
523
524
525 //-----------------------------------------------------------------------------
526 // name: execute()
527 // desc: ...
528 //-----------------------------------------------------------------------------
529 void Chuck_Instr_Divide_double_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
530 {
531 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
532 pop_( sp, 2 );
533 push_( sp, val_(sp+1) / val_(sp) );
534 }
535
536
537
538
539 //-----------------------------------------------------------------------------
540 // name: execute()
541 // desc: modulo two floats
542 //-----------------------------------------------------------------------------
543 void Chuck_Instr_Mod_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
544 {
545 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
546 pop_( sp, 2 );
547 if( val_(sp+1) == 0 ) goto mod_zero;
548 push_( sp, ::fmod( val_(sp), val_(sp+1) ) );
549
550 return;
551
552 mod_zero:
553 // we have a problem | 1.5.4.0 (nshaheed) throw exception
554 EM_exception(
555 "ModuloByZero: on line[%lu] in shred[id=%lu:%s]",
556 m_linepos, shred->xid, shred->name.c_str());
557 goto done;
558
559 done:
560 // do something!
561 shred->is_running = FALSE;
562 shred->is_done = TRUE;
563 }
564
565
566
567
568 //-----------------------------------------------------------------------------
569 // name: execute()
570 // desc: modulo two floats (in reverse)
571 //-----------------------------------------------------------------------------
572 void Chuck_Instr_Mod_double_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
573 {
574 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
575 pop_( sp, 2 );
576 if( val_(sp) == 0 ) goto mod_zero;
577 push_( sp, ::fmod( val_(sp+1), val_(sp) ) );
578
579 return;
580
581 mod_zero:
582 // we have a problem
583 EM_exception(
584 "ModuloByZero: on line[%lu] in shred[id=%lu:%s]",
585 m_linepos, shred->xid, shred->name.c_str());
586 goto done;
587
588 done:
589 // do something!
590 shred->is_running = FALSE;
591 shred->is_done = TRUE;
592 }
593
594
595
596 #pragma mark === Complex Arithmetic ===
597
598
599 //-----------------------------------------------------------------------------
600 // name: execute()
601 // desc: ...
602 //-----------------------------------------------------------------------------
603 void Chuck_Instr_Add_complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
604 {
605 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
606 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
607 t_CKFLOAT re, im;
608 pop_( sp, 2 );
609 re = sp->re + (sp+1)->re;
610 im = sp->im + (sp+1)->im;
611 push_( sp_float, re );
612 push_( sp_float, im );
613 }
614
615
616
617
618 //-----------------------------------------------------------------------------
619 // name: execute()
620 // desc: ...
621 //-----------------------------------------------------------------------------
622 void Chuck_Instr_Minus_complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
623 {
624 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
625 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
626 t_CKFLOAT re, im;
627 pop_( sp, 2 );
628 re = sp->re - (sp+1)->re;
629 im = sp->im - (sp+1)->im;
630 push_( sp_float, re );
631 push_( sp_float, im );
632 }
633
634
635
636
637 //-----------------------------------------------------------------------------
638 // name: execute()
639 // desc: ...
640 //-----------------------------------------------------------------------------
641 void Chuck_Instr_Minus_complex_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
642 {
643 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
644 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
645 t_CKFLOAT re, im;
646 pop_( sp, 2 );
647 re = (sp+1)->re - sp->re;
648 im = (sp+1)->im - sp->im;
649 push_( sp_float, re );
650 push_( sp_float, im );
651 }
652
653
654
655
656 //-----------------------------------------------------------------------------
657 // name: execute()
658 // desc: ...
659 //-----------------------------------------------------------------------------
660 void Chuck_Instr_Times_complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
661 {
662 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
663 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
664 t_CKFLOAT re, im;
665 pop_( sp, 2 );
666 re = sp->re * (sp+1)->re - sp->im * (sp+1)->im;
667 im = sp->re * (sp+1)->im + sp->im * (sp+1)->re;
668 push_( sp_float, re );
669 push_( sp_float, im );
670 }
671
672
673
674
675 //-----------------------------------------------------------------------------
676 // name: execute()
677 // desc: ...
678 //-----------------------------------------------------------------------------
679 void Chuck_Instr_Divide_complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
680 {
681 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
682 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
683 t_CKFLOAT re, im, denom;
684
685 // pop
686 pop_( sp, 2 );
687 // complex division -> * complex conjugate of divisor
688 denom = (sp+1)->re*(sp+1)->re + (sp+1)->im*(sp+1)->im;
689 // go
690 re = sp->re*(sp+1)->re + sp->im*(sp+1)->im;
691 im = sp->im*(sp+1)->re - sp->re*(sp+1)->im;
692 // result
693 push_( sp_float, re/denom );
694 push_( sp_float, im/denom );
695 }
696
697
698
699
700
701 //-----------------------------------------------------------------------------
702 // name: execute()
703 // desc: ...
704 //-----------------------------------------------------------------------------
705 void Chuck_Instr_Divide_complex_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
706 {
707 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
708 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
709 t_CKFLOAT re, im, denom;
710
711 // pop
712 pop_( sp, 2 );
713 // complex division -> * complex conjugate of divisor
714 denom = sp->re*sp->re + sp->im*sp->im;
715 // go
716 re = sp->re*(sp+1)->re + sp->im*(sp+1)->im;
717 im = (sp+1)->im*sp->re - (sp+1)->re*sp->im;
718 // result
719 push_( sp_float, re/denom );
720 push_( sp_float, im/denom );
721 }
722
723
724
725
726 //-----------------------------------------------------------------------------
727 // name: execute()
728 // desc: ...
729 //-----------------------------------------------------------------------------
730 void Chuck_Instr_Add_polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
731 {
732 t_CKPOLAR *& sp = (t_CKPOLAR *&)shred->reg->sp;
733 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
734 t_CKCOMPLEX a, b;
735 pop_( sp, 2 );
736 a.re = sp->modulus * ::cos( sp->phase );
737 a.im = sp->modulus * ::sin( sp->phase );
738 b.re = (sp+1)->modulus * ::cos( (sp+1)->phase );
739 b.im = (sp+1)->modulus * ::sin( (sp+1)->phase );
740 a.re += b.re;
741 a.im += b.im;
742 push_( sp_float, ::hypot( a.re, a.im ) );
743 push_( sp_float, ::atan2( a.im, a.re ) );
744 }
745
746
747
748
749 //-----------------------------------------------------------------------------
750 // name: execute()
751 // desc: ...
752 //-----------------------------------------------------------------------------
753 void Chuck_Instr_Minus_polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
754 {
755 t_CKPOLAR *& sp = (t_CKPOLAR *&)shred->reg->sp;
756 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
757 t_CKCOMPLEX a, b;
758 pop_( sp, 2 );
759 a.re = sp->modulus * ::cos( sp->phase );
760 a.im = sp->modulus * ::sin( sp->phase );
761 b.re = (sp+1)->modulus * ::cos( (sp+1)->phase );
762 b.im = (sp+1)->modulus * ::sin( (sp+1)->phase );
763 a.re -= b.re;
764 a.im -= b.im;
765 push_( sp_float, ::hypot( a.re, a.im ) );
766 push_( sp_float, ::atan2( a.im, a.re ) );
767 }
768
769
770
771
772 //-----------------------------------------------------------------------------
773 // name: execute()
774 // desc: ...
775 //-----------------------------------------------------------------------------
776 void Chuck_Instr_Minus_polar_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
777 {
778 t_CKPOLAR *& sp = (t_CKPOLAR *&)shred->reg->sp;
779 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
780 t_CKCOMPLEX a, b;
781 pop_( sp, 2 );
782 a.re = sp->modulus * ::cos( sp->phase );
783 a.im = sp->modulus * ::sin( sp->phase );
784 b.re = (sp+1)->modulus * ::cos( (sp+1)->phase );
785 b.im = (sp+1)->modulus * ::sin( (sp+1)->phase );
786 a.re = b.re - a.re;
787 a.im = b.im - a.im;
788 push_( sp_float, ::hypot( a.re, a.im ) );
789 push_( sp_float, ::atan2( a.im, a.re ) );
790 }
791
792
793
794
795 //-----------------------------------------------------------------------------
796 // name: execute()
797 // desc: ...
798 //-----------------------------------------------------------------------------
799 void Chuck_Instr_Times_polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
800 {
801 t_CKPOLAR *& sp = (t_CKPOLAR *&)shred->reg->sp;
802 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
803 t_CKFLOAT mag, phase;
804 pop_( sp, 2 );
805 mag = sp->modulus * (sp+1)->modulus;
806 phase = sp->phase + (sp+1)->phase;
807 push_( sp_float, mag );
808 push_( sp_float, phase );
809 }
810
811
812
813
814 //-----------------------------------------------------------------------------
815 // name: execute()
816 // desc: ...
817 //-----------------------------------------------------------------------------
818 void Chuck_Instr_Divide_polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
819 {
820 t_CKPOLAR *& sp = (t_CKPOLAR *&)shred->reg->sp;
821 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
822 t_CKFLOAT mag, phase;
823 pop_( sp, 2 );
824 mag = sp->modulus / (sp+1)->modulus;
825 phase = sp->phase - (sp+1)->phase;
826 push_( sp_float, mag );
827 push_( sp_float, phase );
828 }
829
830
831
832
833
834 //-----------------------------------------------------------------------------
835 // name: execute()
836 // desc: ...
837 //-----------------------------------------------------------------------------
838 void Chuck_Instr_Divide_polar_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
839 {
840 t_CKPOLAR *& sp = (t_CKPOLAR *&)shred->reg->sp;
841 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
842 t_CKFLOAT mag, phase;
843 pop_( sp, 2 );
844 mag = (sp+1)->modulus / (sp)->modulus;
845 phase = (sp+1)->phase - sp->phase;
846 push_( sp_float, mag );
847 push_( sp_float, phase );
848 }
849
850
851
852
853 //-----------------------------------------------------------------------------
854 // name: execute()
855 // desc: add two vec2 (ge) added 1.5.1.7 with ChuGL
856 //-----------------------------------------------------------------------------
857 void Chuck_Instr_Add_vec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
858 {
859 t_CKVEC2 *& sp = (t_CKVEC2 *&)shred->reg->sp;
860 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
861 pop_( sp, 2 );
862 // result
863 t_CKVEC2 r;
864 r.x = sp->x + (sp+1)->x;
865 r.y = sp->y + (sp+1)->y;
866 push_( sp_float, r.x );
867 push_( sp_float, r.y );
868 }
869
870
871
872
873 //-----------------------------------------------------------------------------
874 // name: execute()
875 // desc: subtract two vec2 (ge) added 1.5.1.7 with ChuGL
876 //-----------------------------------------------------------------------------
877 void Chuck_Instr_Minus_vec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
878 {
879 t_CKVEC2 *& sp = (t_CKVEC2 *&)shred->reg->sp;
880 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
881 pop_( sp, 2 );
882 // result
883 t_CKVEC2 r;
884 r.x = sp->x - (sp+1)->x;
885 r.y = sp->y - (sp+1)->y;
886 push_( sp_float, r.x );
887 push_( sp_float, r.y );
888 }
889
890
891
892
893 //-----------------------------------------------------------------------------
894 // name: execute()
895 // desc: float * vec2 (ge) added 1.5.1.7 with ChuGL
896 //-----------------------------------------------------------------------------
897 void Chuck_Instr_float_Times_vec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
898 {
899 // stack pointer
900 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
901 // pointer number of bytes
902 pop_( sp, sz_FLOAT+sz_VEC2 );
903 // the float
904 t_CKFLOAT f = *((t_CKFLOAT *)sp);
905 // the vector
906 t_CKVEC2 v = *((t_CKVEC2 *)(sp+sz_FLOAT));
907 // result
908 t_CKVEC2 r;
909 r.x = v.x * f;
910 r.y = v.y * f;
911 // pointer as vec2
912 t_CKVEC2 *& sp_vec2 = (t_CKVEC2 *&)sp;
913 // push on reg stack
914 push_( sp_vec2, r );
915 }
916
917
918
919
920 //-----------------------------------------------------------------------------
921 // name: execute()
922 // desc: vec2*float (ge) added 1.5.1.7 with ChuGL
923 //-----------------------------------------------------------------------------
924 void Chuck_Instr_vec2_Times_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
925 {
926 // stack pointer
927 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
928 // pointer number of bytes
929 pop_( sp, sz_FLOAT+sz_VEC2 );
930 // the vector
931 t_CKVEC2 v = *((t_CKVEC2 *)sp);
932 // the float
933 t_CKFLOAT f = *((t_CKFLOAT *)(sp+sz_VEC2));
934 // result
935 t_CKVEC2 r;
936 r.x = v.x * f;
937 r.y = v.y * f;
938 // pointer as vec2
939 t_CKVEC2 *& sp_vec2 = (t_CKVEC2 *&)sp;
940 // push on reg stack
941 push_( sp_vec2, r );
942 }
943
944
945
946
947 //-----------------------------------------------------------------------------
948 // name: execute()
949 // desc: vec2/float (ge) added 1.5.1.7 with ChuGL
950 //-----------------------------------------------------------------------------
951 void Chuck_Instr_vec2_Divide_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
952 {
953 // stack pointer
954 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
955 // pointer number of bytes
956 pop_( sp, sz_FLOAT+sz_VEC2 );
957 // the vector
958 t_CKVEC2 v = *((t_CKVEC2 *)sp);
959 // the float
960 t_CKFLOAT f = *((t_CKFLOAT *)(sp+sz_VEC2));
961 // result
962 t_CKVEC2 r;
963 r.x = v.x / f;
964 r.y = v.y / f;
965 // pointer as vec2
966 t_CKVEC2 *& sp_vec2 = (t_CKVEC2 *&)sp;
967 // push on reg stack
968 push_( sp_vec2, r );
969 }
970
971
972
973
974 //-----------------------------------------------------------------------------
975 // name: execute()
976 // desc: add two vec3; ge: added 1.3.5.3 with other vec3 vec4 instructions
977 //-----------------------------------------------------------------------------
978 void Chuck_Instr_Add_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
979 {
980 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
981 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
982 pop_( sp, 2 );
983 // result
984 t_CKVEC3 r;
985 r.x = sp->x + (sp+1)->x;
986 r.y = sp->y + (sp+1)->y;
987 r.z = sp->z + (sp+1)->z;
988 push_( sp_float, r.x );
989 push_( sp_float, r.y );
990 push_( sp_float, r.z );
991 }
992
993
994
995
996 //-----------------------------------------------------------------------------
997 // name: execute()
998 // desc: subtract two vec3; ge: added 1.3.5.3 with other vec3 vec4 instructions
999 //-----------------------------------------------------------------------------
1000 void Chuck_Instr_Minus_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1001 {
1002 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
1003 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
1004 pop_( sp, 2 );
1005 // result
1006 t_CKVEC3 r;
1007 r.x = sp->x - (sp+1)->x;
1008 r.y = sp->y - (sp+1)->y;
1009 r.z = sp->z - (sp+1)->z;
1010 push_( sp_float, r.x );
1011 push_( sp_float, r.y );
1012 push_( sp_float, r.z );
1013 }
1014
1015
1016
1017
1018 //-----------------------------------------------------------------------------
1019 // name: execute()
1020 // desc: vector cross-product (ge) added 1.3.5.3 with other vec3 vec4 instructions
1021 //-----------------------------------------------------------------------------
1022 void Chuck_Instr_XProduct_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1023 {
1024 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
1025 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
1026 pop_( sp, 2 );
1027 // result
1028 t_CKVEC3 result;
1029 result.x = (sp->y * (sp+1)->z) - (sp->z * (sp+1)->y);
1030 result.y = (sp->z * (sp+1)->x) - (sp->x * (sp+1)->z);
1031 result.z = (sp->x * (sp+1)->y) - (sp->y * (sp+1)->x);
1032 push_( sp_float, result.x );
1033 push_( sp_float, result.y );
1034 push_( sp_float, result.z );
1035 }
1036
1037
1038
1039
1040 //-----------------------------------------------------------------------------
1041 // name: execute()
1042 // desc: multiply two vec3; ge: added 1.3.5.3 with other vec3 vec4 instructions
1043 //-----------------------------------------------------------------------------
1044 void Chuck_Instr_float_Times_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1045 {
1046 // stack pointer
1047 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1048 // pointer number of bytes
1049 pop_( sp, sz_FLOAT+sz_VEC3 );
1050 // the float
1051 t_CKFLOAT f = *((t_CKFLOAT *)sp);
1052 // the vector
1053 t_CKVEC3 v = *((t_CKVEC3 *)(sp+sz_FLOAT));
1054 // result
1055 t_CKVEC3 r;
1056 r.x = v.x * f;
1057 r.y = v.y * f;
1058 r.z = v.z * f;
1059 // pointer as vec3
1060 t_CKVEC3 *& sp_vec3 = (t_CKVEC3 *&)sp;
1061 // push on reg stack
1062 push_( sp_vec3, r );
1063 }
1064
1065
1066
1067
1068 //-----------------------------------------------------------------------------
1069 // name: execute()
1070 // desc: scaling a vec3; ge: added 1.3.5.3 with other vec3 vec4 instructions
1071 //-----------------------------------------------------------------------------
1072 void Chuck_Instr_vec3_Times_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1073 {
1074 // stack pointer
1075 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1076 // pointer number of bytes
1077 pop_( sp, sz_FLOAT+sz_VEC3 );
1078 // the vector
1079 t_CKVEC3 v = *((t_CKVEC3 *)sp);
1080 // the float
1081 t_CKFLOAT f = *((t_CKFLOAT *)(sp+sz_VEC3));
1082 // result
1083 t_CKVEC3 r;
1084 r.x = v.x * f;
1085 r.y = v.y * f;
1086 r.z = v.z * f;
1087 // pointer as vec3
1088 t_CKVEC3 *& sp_vec3 = (t_CKVEC3 *&)sp;
1089 // push on reg stack
1090 push_( sp_vec3, r );
1091 }
1092
1093
1094
1095
1096 //-----------------------------------------------------------------------------
1097 // name: execute()
1098 // desc: divide by scalar; ge: added 1.3.5.3 with other vec3 vec4 instructions
1099 //-----------------------------------------------------------------------------
1100 void Chuck_Instr_vec3_Divide_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1101 {
1102 // stack pointer
1103 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1104 // pointer number of bytes
1105 pop_( sp, sz_FLOAT+sz_VEC3 );
1106 // the vector
1107 t_CKVEC3 v = *((t_CKVEC3 *)sp);
1108 // the float
1109 t_CKFLOAT f = *((t_CKFLOAT *)(sp+sz_VEC3));
1110 // result
1111 t_CKVEC3 r;
1112 r.x = v.x / f;
1113 r.y = v.y / f;
1114 r.z = v.z / f;
1115 // pointer as vec3
1116 t_CKVEC3 *& sp_vec3 = (t_CKVEC3 *&)sp;
1117 // push on reg stack
1118 push_( sp_vec3, r );
1119 }
1120
1121
1122
1123
1124 //-----------------------------------------------------------------------------
1125 // name: execute()
1126 // desc: ...
1127 //-----------------------------------------------------------------------------
1128 void Chuck_Instr_Add_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1129 {
1130 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
1131 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
1132 pop_( sp, 2 );
1133 // result
1134 t_CKVEC4 r;
1135 r.x = sp->x + (sp+1)->x;
1136 r.y = sp->y + (sp+1)->y;
1137 r.z = sp->z + (sp+1)->z;
1138 r.w = sp->w + (sp+1)->w;
1139 push_( sp_float, r.x );
1140 push_( sp_float, r.y );
1141 push_( sp_float, r.z );
1142 push_( sp_float, r.w );
1143 }
1144
1145
1146
1147
1148 //-----------------------------------------------------------------------------
1149 // name: execute()
1150 // desc: ...
1151 //-----------------------------------------------------------------------------
1152 void Chuck_Instr_Minus_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1153 {
1154 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
1155 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
1156 pop_( sp, 2 );
1157 // result
1158 t_CKVEC4 r;
1159 r.x = sp->x - (sp+1)->x;
1160 r.y = sp->y - (sp+1)->y;
1161 r.z = sp->z - (sp+1)->z;
1162 r.w = sp->w - (sp+1)->w;
1163 push_( sp_float, r.x );
1164 push_( sp_float, r.y );
1165 push_( sp_float, r.z );
1166 push_( sp_float, r.w );
1167 }
1168
1169
1170
1171
1172 //-----------------------------------------------------------------------------
1173 // name: execute()
1174 // desc: ...
1175 //-----------------------------------------------------------------------------
1176 void Chuck_Instr_XProduct_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1177 {
1178 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
1179 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
1180 pop_( sp, 2 );
1181 // result
1182 t_CKVEC4 result;
1183 result.x = (sp->y * (sp+1)->z) - (sp->z * (sp+1)->y);
1184 result.y = (sp->z * (sp+1)->x) - (sp->x * (sp+1)->z);
1185 result.z = (sp->x * (sp+1)->y) - (sp->y * (sp+1)->x);
1186 result.w = 0;
1187 push_( sp_float, result.x );
1188 push_( sp_float, result.y );
1189 push_( sp_float, result.z );
1190 push_( sp_float, result.w );
1191 }
1192
1193
1194
1195
1196 //-----------------------------------------------------------------------------
1197 // name: execute()
1198 // desc: ...
1199 //-----------------------------------------------------------------------------
1200 void Chuck_Instr_float_Times_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1201 {
1202 // stack pointer
1203 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1204 // pointer number of bytes
1205 pop_( sp, sz_FLOAT+sz_VEC4 );
1206 // the float
1207 t_CKFLOAT f = *((t_CKFLOAT *)sp);
1208 // the vector
1209 t_CKVEC4 v = *((t_CKVEC4 *)(sp+sz_FLOAT));
1210 // result
1211 t_CKVEC4 r;
1212 r.x = v.x * f;
1213 r.y = v.y * f;
1214 r.z = v.z * f;
1215 r.w = v.w * f;
1216 // pointer as vec4
1217 t_CKVEC4 *& sp_vec4 = (t_CKVEC4 *&)sp;
1218 // push on reg stack
1219 push_( sp_vec4, r );
1220 }
1221
1222
1223
1224
1225 //-----------------------------------------------------------------------------
1226 // name: execute()
1227 // desc: ...
1228 //-----------------------------------------------------------------------------
1229 void Chuck_Instr_vec4_Times_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1230 {
1231 // stack pointer
1232 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1233 // pointer number of bytes
1234 pop_( sp, sz_FLOAT+sz_VEC4 );
1235 // the vector
1236 t_CKVEC4 v = *((t_CKVEC4 *)sp);
1237 // the float
1238 t_CKFLOAT f = *((t_CKFLOAT *)(sp+sz_VEC4));
1239 // result
1240 t_CKVEC4 r;
1241 r.x = v.x * f;
1242 r.y = v.y * f;
1243 r.z = v.z * f;
1244 r.w = v.w * f;
1245 // pointer as vec4
1246 t_CKVEC4 *& sp_vec4 = (t_CKVEC4 *&)sp;
1247 // push on reg stack
1248 push_( sp_vec4, r );
1249 }
1250
1251
1252
1253
1254 //-----------------------------------------------------------------------------
1255 // name: execute()
1256 // desc: ...
1257 //-----------------------------------------------------------------------------
1258 void Chuck_Instr_vec4_Divide_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1259 {
1260 // stack pointer
1261 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1262 // pointer number of bytes
1263 pop_( sp, sz_FLOAT+sz_VEC4 );
1264 // the vector
1265 t_CKVEC4 v = *((t_CKVEC4 *)sp);
1266 // the float
1267 t_CKFLOAT f = *((t_CKFLOAT *)(sp+sz_VEC4));
1268 // result
1269 t_CKVEC4 r;
1270 r.x = v.x / f;
1271 r.y = v.y / f;
1272 r.z = v.z / f;
1273 r.w = v.w / f;
1274 // pointer as vec4
1275 t_CKVEC4 *& sp_vec4 = (t_CKVEC4 *&)sp;
1276 // push on reg stack
1277 push_( sp_vec4, r );
1278 }
1279
1280
1281
1282
1283 #pragma mark === Arithmetic Assignment ===
1284
1285
1286 //-----------------------------------------------------------------------------
1287 // name: execute()
1288 // desc: ...
1289 //-----------------------------------------------------------------------------
1290 void Chuck_Instr_Add_int_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1291 {
1292 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
1293 pop_( sp, 2 );
1294 temp = **(t_CKINT **)(sp+1) += val_(sp);
1295 push_( sp, temp );
1296 }
1297
1298
1299
1300
1301 //-----------------------------------------------------------------------------
1302 // name: execute()
1303 // desc: modulo assign two ints
1304 //-----------------------------------------------------------------------------
1305 void Chuck_Instr_Mod_int_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1306 {
1307 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
1308 pop_( sp, 2 );
1309 if( val_(sp) == 0 ) goto mod_zero;
1310 temp = **(t_CKINT **)(sp+1) %= val_(sp);
1311 push_( sp, temp );
1312
1313 return;
1314
1315 mod_zero:
1316 // we have a problem | 1.5.4.0 (nshaheed) throw exception
1317 EM_exception(
1318 "ModuloByZero: on line[%lu] in shred[id=%lu:%s]",
1319 m_linepos, shred->xid, shred->name.c_str());
1320 goto done;
1321
1322 done:
1323 // do something!
1324 shred->is_running = FALSE;
1325 shred->is_done = TRUE;
1326 }
1327
1328
1329
1330
1331 //-----------------------------------------------------------------------------
1332 // name: execute()
1333 // desc: ...
1334 //-----------------------------------------------------------------------------
1335 void Chuck_Instr_Minus_int_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1336 {
1337 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
1338 pop_( sp, 2 );
1339 temp = **(t_CKINT **)(sp+1) -= val_(sp);
1340 push_( sp, temp );
1341 }
1342
1343
1344
1345
1346 //-----------------------------------------------------------------------------
1347 // name: execute()
1348 // desc: ...
1349 //-----------------------------------------------------------------------------
1350 void Chuck_Instr_Times_int_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1351 {
1352 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
1353 pop_( sp, 2 );
1354 temp = **(t_CKINT **)(sp+1) *= val_(sp);
1355 push_( sp, temp );
1356 }
1357
1358
1359
1360
1361 //-----------------------------------------------------------------------------
1362 // name: execute()
1363 // desc: ...
1364 //-----------------------------------------------------------------------------
1365 void Chuck_Instr_Divide_int_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1366 {
1367 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
1368 pop_( sp, 2 );
1369 if( val_(sp) == 0 ) goto div_zero;
1370 temp = **(t_CKINT **)(sp+1) /= val_(sp);
1371 push_( sp, temp );
1372
1373 return;
1374 div_zero:
1375 // we have a problem
1376 EM_exception(
1377 "DivideByZero: on line[%lu] in shred[id=%lu:%s]",
1378 m_linepos, shred->xid, shred->name.c_str());
1379 goto done;
1380
1381 done:
1382 // do something!
1383 shred->is_running = FALSE;
1384 shred->is_done = TRUE;
1385 }
1386
1387
1388
1389 //-----------------------------------------------------------------------------
1390 // name: execute()
1391 // desc: ...
1392 //-----------------------------------------------------------------------------
1393 void Chuck_Instr_Add_double_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1394 {
1395 t_CKFLOAT temp;
1396 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1397 // pop value + pointer
1398 pop_( sp, sz_FLOAT + sz_UINT );
1399 // assign
1400 temp = **(t_CKFLOAT **)(sp+sz_FLOAT) += val_((t_CKFLOAT *&)sp);
1401 // push result
1402 push_( (t_CKFLOAT *&)sp, temp );
1403 }
1404
1405
1406
1407
1408 //-----------------------------------------------------------------------------
1409 // name: execute()
1410 // desc: ...
1411 //-----------------------------------------------------------------------------
1412 void Chuck_Instr_Minus_double_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1413 {
1414 t_CKFLOAT temp;
1415 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1416 // pop value + pointer
1417 pop_( sp, sz_FLOAT + sz_UINT );
1418 // assign
1419 temp = **(t_CKFLOAT **)(sp+sz_FLOAT) -= val_((t_CKFLOAT *&)sp);
1420 // push result
1421 push_( (t_CKFLOAT *&)sp, temp );
1422 }
1423
1424
1425
1426
1427 //-----------------------------------------------------------------------------
1428 // name: execute()
1429 // desc: ...
1430 //-----------------------------------------------------------------------------
1431 void Chuck_Instr_Times_double_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1432 {
1433 t_CKFLOAT temp;
1434 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1435 // pop value + pointer
1436 pop_( sp, sz_FLOAT + sz_UINT );
1437 // assign
1438 temp = **(t_CKFLOAT **)(sp+sz_FLOAT) *= val_((t_CKFLOAT *&)sp);
1439 // push result
1440 push_( (t_CKFLOAT *&)sp, temp );
1441 }
1442
1443
1444
1445
1446 //-----------------------------------------------------------------------------
1447 // name: execute()
1448 // desc: ...
1449 //-----------------------------------------------------------------------------
1450 void Chuck_Instr_Divide_double_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1451 {
1452 t_CKFLOAT temp;
1453 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1454 // pop value + pointer
1455 pop_( sp, sz_FLOAT + sz_UINT );
1456 // assign
1457 temp = **(t_CKFLOAT **)(sp+sz_FLOAT) /= val_((t_CKFLOAT *&)sp);
1458 // push result
1459 push_( (t_CKFLOAT *&)sp, temp );
1460 }
1461
1462
1463
1464
1465 //-----------------------------------------------------------------------------
1466 // name: execute()
1467 // desc: modulo assign two doubles
1468 //-----------------------------------------------------------------------------
1469 void Chuck_Instr_Mod_double_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1470 {
1471 t_CKFLOAT temp;
1472 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1473 // pop value + pointer
1474 pop_( sp, sz_FLOAT + sz_UINT );
1475
1476 if( val_((t_CKFLOAT *&)sp) == 0 ) goto mod_zero;
1477 // assign
1478 temp = ::fmod( **(t_CKFLOAT **)(sp+sz_FLOAT), val_((t_CKFLOAT *&)sp) );
1479 **(t_CKFLOAT **)(sp+sz_FLOAT) = temp;
1480 // push result
1481 push_( (t_CKFLOAT *&)sp, temp );
1482
1483 return;
1484
1485 mod_zero:
1486 // we have a problem | 1.5.4.0 (nshaheed) throw exception
1487 EM_exception(
1488 "ModuloByZero: on line[%lu] in shred[id=%lu:%s]",
1489 m_linepos, shred->xid, shred->name.c_str());
1490 goto done;
1491
1492 done:
1493 // do something!
1494 shred->is_running = FALSE;
1495 shred->is_done = TRUE;
1496 }
1497
1498
1499
1500
1501 //-----------------------------------------------------------------------------
1502 // name: execute()
1503 // desc: ...
1504 //-----------------------------------------------------------------------------
1505 void Chuck_Instr_Add_complex_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1506 {
1507 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1508 t_CKCOMPLEX temp;
1509 // pop value + pointer
1510 pop_( sp, sz_COMPLEX + sz_UINT );
1511
1512 // assign
1513 temp.re = (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->re += ((t_CKCOMPLEX *&)sp)->re;
1514 temp.im = (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->im += ((t_CKCOMPLEX *&)sp)->im;
1515 // push result
1516 push_( (t_CKCOMPLEX *&)sp, temp );
1517 }
1518
1519
1520
1521
1522 //-----------------------------------------------------------------------------
1523 // name: execute()
1524 // desc: ...
1525 //-----------------------------------------------------------------------------
1526 void Chuck_Instr_Minus_complex_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1527 {
1528 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1529 t_CKCOMPLEX temp;
1530 // pop value + pointer
1531 pop_( sp, sz_COMPLEX + sz_UINT );
1532
1533 // assign
1534 temp.re = (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->re -= ((t_CKCOMPLEX *&)sp)->re;
1535 temp.im = (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->im -= ((t_CKCOMPLEX *&)sp)->im;
1536 // push result
1537 push_( (t_CKCOMPLEX *&)sp, temp );
1538 }
1539
1540
1541
1542
1543 //-----------------------------------------------------------------------------
1544 // name: execute()
1545 // desc: ...
1546 //-----------------------------------------------------------------------------
1547 void Chuck_Instr_Times_complex_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1548 {
1549 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1550 t_CKCOMPLEX temp, a, b;
1551 // pop value + pointer
1552 pop_( sp, sz_COMPLEX + sz_UINT );
1553 // copy
1554 a = **(t_CKCOMPLEX **)(sp+sz_COMPLEX);
1555 b = *(t_CKCOMPLEX *&)sp;
1556 // calculate
1557 temp.re = a.re * b.re - a.im * b.im;
1558 temp.im = a.re * b.im + a.im * b.re;
1559 // assign
1560 (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->re = temp.re;
1561 (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->im = temp.im;
1562 // push result
1563 push_( (t_CKCOMPLEX *&)sp, temp );
1564 }
1565
1566
1567
1568
1569 //-----------------------------------------------------------------------------
1570 // name: execute()
1571 // desc: ...
1572 //-----------------------------------------------------------------------------
1573 void Chuck_Instr_Divide_complex_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1574 {
1575 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1576 t_CKCOMPLEX temp, a, b;
1577 t_CKFLOAT denom;
1578 // pop value + pointer
1579 pop_( sp, sz_COMPLEX + sz_UINT );
1580 // copy
1581 a = **(t_CKCOMPLEX **)(sp+sz_COMPLEX);
1582 b = *(t_CKCOMPLEX *&)sp;
1583 // calculate
1584 temp.re = a.re * b.re + a.im * b.im;
1585 temp.im = a.im * b.re - a.re * b.im;
1586 denom = b.re * b.re + b.im * b.im;
1587 temp.re /= denom;
1588 temp.im /= denom;
1589 // assign
1590 (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->re = temp.re;
1591 (*(t_CKCOMPLEX **)(sp+sz_COMPLEX))->im = temp.im;
1592 // push result
1593 push_( (t_CKCOMPLEX *&)sp, temp );
1594 }
1595
1596
1597
1598
1599 //-----------------------------------------------------------------------------
1600 // name: execute()
1601 // desc: ...
1602 //-----------------------------------------------------------------------------
1603 void Chuck_Instr_Add_polar_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1604 {
1605 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1606 t_CKPOLAR result, * pa, * pb;
1607 t_CKCOMPLEX temp, a, b;
1608 // pop value + pointer
1609 pop_( sp, sz_POLAR + sz_UINT );
1610 // pointer copy
1611 pa = *(t_CKPOLAR **)(sp+sz_POLAR);
1612 pb = (t_CKPOLAR *&)sp;
1613 // rectangular
1614 a.re = pa->modulus * ::cos(pa->phase);
1615 a.im = pa->modulus * ::sin(pa->phase);
1616 b.re = pb->modulus * ::cos(pb->phase);
1617 b.im = pb->modulus * ::sin(pb->phase);
1618 // calculate
1619 temp.re = a.re + b.re;
1620 temp.im = a.im + b.im;
1621 // assign
1622 result.modulus = (*(t_CKPOLAR **)(sp+sz_POLAR))->modulus = ::hypot(temp.re,temp.im);
1623 result.phase = (*(t_CKPOLAR **)(sp+sz_POLAR))->phase = ::atan2(temp.im,temp.re);
1624 // push result
1625 push_( (t_CKPOLAR *&)sp, result );
1626 }
1627
1628
1629
1630
1631 //-----------------------------------------------------------------------------
1632 // name: execute()
1633 // desc: ...
1634 //-----------------------------------------------------------------------------
1635 void Chuck_Instr_Minus_polar_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1636 {
1637 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1638 t_CKPOLAR result, * pa, * pb;
1639 t_CKCOMPLEX temp, a, b;
1640 // pop value + pointer
1641 pop_( sp, sz_POLAR + sz_UINT );
1642 // pointer copy
1643 pa = *(t_CKPOLAR **)(sp+sz_POLAR);
1644 pb = (t_CKPOLAR *&)sp;
1645 // rectangular
1646 a.re = pa->modulus * ::cos(pa->phase);
1647 a.im = pa->modulus * ::sin(pa->phase);
1648 b.re = pb->modulus * ::cos(pb->phase);
1649 b.im = pb->modulus * ::sin(pb->phase);
1650 // calculate
1651 temp.re = a.re - b.re;
1652 temp.im = a.im - b.im;
1653 // assign
1654 result.modulus = (*(t_CKPOLAR **)(sp+sz_POLAR))->modulus = ::hypot(temp.re,temp.im);
1655 result.phase = (*(t_CKPOLAR **)(sp+sz_POLAR))->phase = ::atan2(temp.im,temp.re);
1656 // push result
1657 push_( (t_CKPOLAR *&)sp, result );
1658 }
1659
1660
1661
1662
1663 //-----------------------------------------------------------------------------
1664 // name: execute()
1665 // desc: ...
1666 //-----------------------------------------------------------------------------
1667 void Chuck_Instr_Times_polar_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1668 {
1669 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1670 t_CKPOLAR temp;
1671 // pop value + pointer
1672 pop_( sp, sz_POLAR + sz_UINT );
1673
1674 // assign
1675 temp.modulus = (*(t_CKPOLAR **)(sp+sz_POLAR))->modulus *= ((t_CKPOLAR *&)sp)->modulus;
1676 temp.phase = (*(t_CKPOLAR **)(sp+sz_POLAR))->phase += ((t_CKPOLAR *&)sp)->phase;
1677 // push result
1678 push_( (t_CKPOLAR *&)sp, temp );
1679 }
1680
1681
1682
1683
1684 //-----------------------------------------------------------------------------
1685 // name: execute()
1686 // desc: ...
1687 //-----------------------------------------------------------------------------
1688 void Chuck_Instr_Divide_polar_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1689 {
1690 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1691 t_CKPOLAR temp;
1692 // pop value + pointer
1693 pop_( sp, sz_POLAR + sz_UINT );
1694
1695 // assign
1696 temp.modulus = (*(t_CKPOLAR **)(sp+sz_POLAR))->modulus /= ((t_CKPOLAR *&)sp)->modulus;
1697 temp.phase = (*(t_CKPOLAR **)(sp+sz_POLAR))->phase -= ((t_CKPOLAR *&)sp)->phase;
1698 // push result
1699 push_( (t_CKPOLAR *&)sp, temp );
1700 }
1701
1702
1703
1704
1705 //-----------------------------------------------------------------------------
1706 // name: execute()
1707 // desc: vec2 +=> vec2var
1708 //-----------------------------------------------------------------------------
1709 void Chuck_Instr_Add_vec2_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1710 {
1711 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1712 t_CKVEC2 temp;
1713 // pop value + pointer
1714 pop_( sp, sz_VEC2 + sz_UINT );
1715
1716 // assign
1717 temp.x = (*(t_CKVEC2 **)(sp+sz_VEC2))->x += ((t_CKVEC2 *&)sp)->x;
1718 temp.y = (*(t_CKVEC2 **)(sp+sz_VEC2))->y += ((t_CKVEC2 *&)sp)->y;
1719 // push result
1720 push_( (t_CKVEC2 *&)sp, temp );
1721 }
1722
1723
1724
1725
1726 //-----------------------------------------------------------------------------
1727 // name: execute()
1728 // desc: vec2 -=> vec2var
1729 //-----------------------------------------------------------------------------
1730 void Chuck_Instr_Minus_vec2_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1731 {
1732 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1733 t_CKVEC2 temp;
1734 // pop value + pointer
1735 pop_( sp, sz_VEC2 + sz_UINT );
1736
1737 // assign
1738 temp.x = (*(t_CKVEC2 **)(sp+sz_VEC2))->x -= ((t_CKVEC2 *&)sp)->x;
1739 temp.y = (*(t_CKVEC2 **)(sp+sz_VEC2))->y -= ((t_CKVEC2 *&)sp)->y;
1740 // push result
1741 push_( (t_CKVEC2 *&)sp, temp );
1742 }
1743
1744
1745
1746
1747 //-----------------------------------------------------------------------------
1748 // name: execute()
1749 // desc: float *=> vec2var
1750 //-----------------------------------------------------------------------------
1751 void Chuck_Instr_float_Times_vec2_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1752 {
1753 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1754 t_CKVEC2 temp;
1755 // pop float + pointer
1756 pop_( sp, sz_FLOAT + sz_UINT );
1757
1758 // assign
1759 temp.x = (*(t_CKVEC2 **)(sp+sz_FLOAT))->x *= *((t_CKFLOAT *)sp);
1760 temp.y = (*(t_CKVEC2 **)(sp+sz_FLOAT))->y *= *((t_CKFLOAT *)sp);
1761 // push result
1762 push_( (t_CKVEC2 *&)sp, temp );
1763 }
1764
1765
1766
1767
1768 //-----------------------------------------------------------------------------
1769 // name: execute()
1770 // desc: float /=> vec2var
1771 //-----------------------------------------------------------------------------
1772 void Chuck_Instr_vec2_Divide_float_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1773 {
1774 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1775 t_CKVEC2 temp;
1776 // pop float + pointer
1777 pop_( sp, sz_FLOAT + sz_UINT );
1778
1779 // assign
1780 temp.x = (*(t_CKVEC2 **)(sp+sz_FLOAT))->x /= *((t_CKFLOAT *)sp);
1781 temp.y = (*(t_CKVEC2 **)(sp+sz_FLOAT))->y /= *((t_CKFLOAT *)sp);
1782 // push result
1783 push_( (t_CKVEC2 *&)sp, temp );
1784 }
1785
1786
1787
1788
1789 //-----------------------------------------------------------------------------
1790 // name: execute()
1791 // desc: vec3 +=> vec3var
1792 //-----------------------------------------------------------------------------
1793 void Chuck_Instr_Add_vec3_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1794 {
1795 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1796 t_CKVEC3 temp;
1797 // pop value + pointer
1798 pop_( sp, sz_VEC3 + sz_UINT );
1799
1800 // assign
1801 temp.x = (*(t_CKVEC3 **)(sp+sz_VEC3))->x += ((t_CKVEC3 *&)sp)->x;
1802 temp.y = (*(t_CKVEC3 **)(sp+sz_VEC3))->y += ((t_CKVEC3 *&)sp)->y;
1803 temp.z = (*(t_CKVEC3 **)(sp+sz_VEC3))->z += ((t_CKVEC3 *&)sp)->z;
1804 // push result
1805 push_( (t_CKVEC3 *&)sp, temp );
1806 }
1807
1808
1809
1810
1811 //-----------------------------------------------------------------------------
1812 // name: execute()
1813 // desc: vec3 -=> vec3var
1814 //-----------------------------------------------------------------------------
1815 void Chuck_Instr_Minus_vec3_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1816 {
1817 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1818 t_CKVEC3 temp;
1819 // pop value + pointer
1820 pop_( sp, sz_VEC3 + sz_UINT );
1821
1822 // assign
1823 temp.x = (*(t_CKVEC3 **)(sp+sz_VEC3))->x -= ((t_CKVEC3 *&)sp)->x;
1824 temp.y = (*(t_CKVEC3 **)(sp+sz_VEC3))->y -= ((t_CKVEC3 *&)sp)->y;
1825 temp.z = (*(t_CKVEC3 **)(sp+sz_VEC3))->z -= ((t_CKVEC3 *&)sp)->z;
1826 // push result
1827 push_( (t_CKVEC3 *&)sp, temp );
1828 }
1829
1830
1831
1832
1833 //-----------------------------------------------------------------------------
1834 // name: execute()
1835 // desc: float *=> vec3var
1836 //-----------------------------------------------------------------------------
1837 void Chuck_Instr_float_Times_vec3_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1838 {
1839 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1840 t_CKVEC3 temp;
1841 // pop float + pointer
1842 pop_( sp, sz_FLOAT + sz_UINT );
1843
1844 // assign
1845 temp.x = (*(t_CKVEC3 **)(sp+sz_FLOAT))->x *= *((t_CKFLOAT *)sp);
1846 temp.y = (*(t_CKVEC3 **)(sp+sz_FLOAT))->y *= *((t_CKFLOAT *)sp);
1847 temp.z = (*(t_CKVEC3 **)(sp+sz_FLOAT))->z *= *((t_CKFLOAT *)sp);
1848 // push result
1849 push_( (t_CKVEC3 *&)sp, temp );
1850 }
1851
1852
1853
1854
1855 //-----------------------------------------------------------------------------
1856 // name: execute()
1857 // desc: float /=> vec3var
1858 //-----------------------------------------------------------------------------
1859 void Chuck_Instr_vec3_Divide_float_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1860 {
1861 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1862 t_CKVEC3 temp;
1863 // pop float + pointer
1864 pop_( sp, sz_FLOAT + sz_UINT );
1865
1866 // assign
1867 temp.x = (*(t_CKVEC3 **)(sp+sz_FLOAT))->x /= *((t_CKFLOAT *)sp);
1868 temp.y = (*(t_CKVEC3 **)(sp+sz_FLOAT))->y /= *((t_CKFLOAT *)sp);
1869 temp.z = (*(t_CKVEC3 **)(sp+sz_FLOAT))->z /= *((t_CKFLOAT *)sp);
1870 // push result
1871 push_( (t_CKVEC3 *&)sp, temp );
1872 }
1873
1874
1875
1876
1877 //-----------------------------------------------------------------------------
1878 // name: execute()
1879 // desc: float +=> vec4var
1880 //-----------------------------------------------------------------------------
1881 void Chuck_Instr_Add_vec4_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1882 {
1883 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1884 t_CKVEC4 temp;
1885 // pop value + pointer
1886 pop_( sp, sz_VEC4 + sz_UINT );
1887
1888 // assign
1889 temp.x = (*(t_CKVEC4 **)(sp+sz_VEC4))->x += ((t_CKVEC4 *&)sp)->x;
1890 temp.y = (*(t_CKVEC4 **)(sp+sz_VEC4))->y += ((t_CKVEC4 *&)sp)->y;
1891 temp.z = (*(t_CKVEC4 **)(sp+sz_VEC4))->z += ((t_CKVEC4 *&)sp)->z;
1892 temp.w = (*(t_CKVEC4 **)(sp+sz_VEC4))->w += ((t_CKVEC4 *&)sp)->w;
1893 // push result
1894 push_( (t_CKVEC4 *&)sp, temp );
1895 }
1896
1897
1898
1899
1900 //-----------------------------------------------------------------------------
1901 // name: execute()
1902 // desc: floa -=> vec4var
1903 //-----------------------------------------------------------------------------
1904 void Chuck_Instr_Minus_vec4_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1905 {
1906 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1907 t_CKVEC4 temp;
1908 // pop value + pointer
1909 pop_( sp, sz_VEC4 + sz_UINT );
1910
1911 // assign
1912 temp.x = (*(t_CKVEC4 **)(sp+sz_VEC4))->x -= ((t_CKVEC4 *&)sp)->x;
1913 temp.y = (*(t_CKVEC4 **)(sp+sz_VEC4))->y -= ((t_CKVEC4 *&)sp)->y;
1914 temp.z = (*(t_CKVEC4 **)(sp+sz_VEC4))->z -= ((t_CKVEC4 *&)sp)->z;
1915 temp.w = (*(t_CKVEC4 **)(sp+sz_VEC4))->w -= ((t_CKVEC4 *&)sp)->w;
1916 // push result
1917 push_( (t_CKVEC4 *&)sp, temp );
1918 }
1919
1920
1921
1922
1923 //-----------------------------------------------------------------------------
1924 // name: execute()
1925 // desc: float *=> vec4var
1926 //-----------------------------------------------------------------------------
1927 void Chuck_Instr_float_Times_vec4_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1928 {
1929 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1930 t_CKVEC4 temp;
1931 // pop float + pointer
1932 pop_( sp, sz_FLOAT + sz_UINT );
1933
1934 // assign
1935 temp.x = (*(t_CKVEC4 **)(sp+sz_FLOAT))->x *= *((t_CKFLOAT *)sp);
1936 temp.y = (*(t_CKVEC4 **)(sp+sz_FLOAT))->y *= *((t_CKFLOAT *)sp);
1937 temp.z = (*(t_CKVEC4 **)(sp+sz_FLOAT))->z *= *((t_CKFLOAT *)sp);
1938 temp.w = (*(t_CKVEC4 **)(sp+sz_FLOAT))->w *= *((t_CKFLOAT *)sp);
1939 // push result
1940 push_( (t_CKVEC4 *&)sp, temp );
1941 }
1942
1943
1944
1945
1946 //-----------------------------------------------------------------------------
1947 // name: execute()
1948 // desc: float /=> vec4var
1949 //-----------------------------------------------------------------------------
1950 void Chuck_Instr_vec4_Divide_float_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1951 {
1952 t_CKBYTE *& sp = (t_CKBYTE *&)shred->reg->sp;
1953 t_CKVEC4 temp;
1954 // pop float + pointer
1955 pop_( sp, sz_FLOAT + sz_UINT );
1956
1957 // assign
1958 temp.x = (*(t_CKVEC4 **)(sp+sz_FLOAT))->x *= *((t_CKFLOAT *)sp);
1959 temp.y = (*(t_CKVEC4 **)(sp+sz_FLOAT))->y *= *((t_CKFLOAT *)sp);
1960 temp.z = (*(t_CKVEC4 **)(sp+sz_FLOAT))->z *= *((t_CKFLOAT *)sp);
1961 temp.w = (*(t_CKVEC4 **)(sp+sz_FLOAT))->w *= *((t_CKFLOAT *)sp);
1962 // push result
1963 push_( (t_CKVEC4 *&)sp, temp );
1964 }
1965
1966
1967
1968
1969 #pragma mark === String Arithmetic ===
1970
1971
1972 //-----------------------------------------------------------------------------
1973 // name: execute()
1974 // desc: string + string
1975 // (no longer used; string concat now handled by op overloading)
1976 //-----------------------------------------------------------------------------
1977 //void Chuck_Instr_Add_string::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
1978 //{
1979 // t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
1980 // Chuck_String * lhs = NULL;
1981 // Chuck_String * rhs = NULL;
1982 // Chuck_String * result = NULL;
1983 //
1984 // // pop word from reg stack
1985 // pop_( reg_sp, 2 );
1986 // // left
1987 // lhs = (Chuck_String *)(*(reg_sp));
1988 // // right
1989 // rhs = (Chuck_String *)(*(reg_sp+1));
1990 //
1991 // // make sure no null
1992 // if( !rhs || !lhs ) goto null_pointer;
1993 //
1994 // // make new string
1995 // result = (Chuck_String *)instantiate_and_initialize_object( vm->env()->ckt_string, shred );
1996 //
1997 // // concat
1998 // // result->str = lhs->str + rhs->str;
1999 // result->set( lhs->str() + rhs->str() );
2000 //
2001 // // push the reference value to reg stack
2002 // push_( reg_sp, (t_CKUINT)(result) );
2003 //
2004 // return;
2005 //
2006 //null_pointer:
2007 // // we have a problem
2008 // EM_exception(
2009 // "NullPointer: (string + string) on line[%lu] in shred[id=%lu:%s]",
2010 // m_linepos, shred->xid, shred->name.c_str() ); // , shred->pc );
2011 // goto done;
2012 //
2013 //done:
2014 // // do something!
2015 // shred->is_running = FALSE;
2016 // shred->is_done = TRUE;
2017 //}
2018
2019
2020
2021
2022 //-----------------------------------------------------------------------------
2023 // name: execute()
2024 // desc: add assign string
2025 //-----------------------------------------------------------------------------
2026 void Chuck_Instr_Add_string_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2027 {
2028 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2029 Chuck_String * lhs = NULL;
2030 Chuck_String ** rhs_ptr = NULL;
2031
2032 // pop word from reg stack
2033 pop_( reg_sp, 2 );
2034 // the previous reference
2035 rhs_ptr = (Chuck_String **)(*(reg_sp+1));
2036 // copy popped value into memory
2037 lhs = (Chuck_String *)(*(reg_sp));
2038
2039 // make sure no null
2040 if( !lhs || !(*rhs_ptr) ) goto null_pointer;
2041
2042 // concat
2043 // (*rhs_ptr)->str += lhs->str;
2044 (*rhs_ptr)->set( (*rhs_ptr)->str() + lhs->str() );
2045
2046 // push the reference value to reg stack
2047 push_( reg_sp, (t_CKUINT)(*rhs_ptr) );
2048
2049 return;
2050
2051 null_pointer:
2052 // we have a problem
2053 EM_exception(
2054 "NullPointer: (string +=> string) on line[%lu] in shred[id=%lu:%s]",
2055 m_linepos, shred->xid, shred->name.c_str() );
2056 goto done;
2057
2058 done:
2059 // do something!
2060 shred->is_running = FALSE;
2061 shred->is_done = TRUE;
2062 }
2063
2064
2065
2066
2067 //-----------------------------------------------------------------------------
2068 // name: execute()
2069 // desc: string + int
2070 //-----------------------------------------------------------------------------
2071 void Chuck_Instr_Add_string_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2072 {
2073 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2074 Chuck_String * lhs = NULL;
2075 t_CKINT rhs = 0;
2076 Chuck_String * result = NULL;
2077
2078 // pop word from reg stack
2079 pop_( reg_sp, 2 );
2080 // left
2081 lhs = (Chuck_String *)(*(reg_sp));
2082 // right
2083 rhs = (*(t_CKINT *)(reg_sp+1));
2084
2085 // make sure no null
2086 if( !lhs ) goto null_pointer;
2087
2088 // make new string
2089 result = (Chuck_String *)instantiate_and_initialize_object( vm->env()->ckt_string, shred );
2090
2091 // concat
2092 // result->str = lhs->str + ::ck_itoa(rhs);
2093 result->set( lhs->str() + ::ck_itoa( rhs ) );
2094
2095 // push the reference value to reg stack
2096 push_( reg_sp, (t_CKUINT)(result) );
2097
2098 return;
2099
2100 null_pointer:
2101 // we have a problem
2102 EM_exception(
2103 "NullPointer: (string + int) on line[%lu] in shred[id=%lu:%s]",
2104 m_linepos, shred->xid, shred->name.c_str() );
2105 goto done;
2106
2107 done:
2108 // do something!
2109 shred->is_running = FALSE;
2110 shred->is_done = TRUE;
2111 }
2112
2113
2114
2115
2116 //-----------------------------------------------------------------------------
2117 // name: execute()
2118 // desc: string + float
2119 //-----------------------------------------------------------------------------
2120 void Chuck_Instr_Add_string_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2121 {
2122 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2123 Chuck_String * lhs = NULL;
2124 t_CKFLOAT rhs = 0;
2125 Chuck_String * result = NULL;
2126
2127 // pop word from reg stack (1.3.1.0: add size check)
2128 pop_( reg_sp, 1 + (sz_FLOAT / sz_UINT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
2129 // left
2130 lhs = (Chuck_String *)(*(reg_sp));
2131 // right
2132 rhs = (*(t_CKFLOAT *)(reg_sp+1));
2133
2134 // make sure no null
2135 if( !lhs ) goto null_pointer;
2136
2137 // make new string
2138 result = (Chuck_String *)instantiate_and_initialize_object( vm->env()->ckt_string, shred );
2139
2140 // concat
2141 // result->str = lhs->str + ::ck_ftoa(rhs, 4);
2142 result->set( lhs->str() + ::ck_ftoa( rhs, 4 ) );
2143
2144 // push the reference value to reg stack
2145 push_( reg_sp, (t_CKUINT)(result) );
2146
2147 return;
2148
2149 null_pointer:
2150 // we have a problem
2151 EM_exception(
2152 "NullPointer: (string + float) on line[%lu] in shred[id=%lu:%s]",
2153 m_linepos, shred->xid, shred->name.c_str() );
2154 goto done;
2155
2156 done:
2157 // do something!
2158 shred->is_running = FALSE;
2159 shred->is_done = TRUE;
2160 }
2161
2162
2163
2164
2165 //-----------------------------------------------------------------------------
2166 // name: execute()
2167 // desc: int + string
2168 //-----------------------------------------------------------------------------
2169 void Chuck_Instr_Add_int_string::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2170 {
2171 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2172 t_CKINT lhs = 0;
2173 Chuck_String * rhs = NULL;
2174 Chuck_String * result = NULL;
2175
2176 // pop word from reg stack
2177 pop_( reg_sp, 2 );
2178 // left
2179 lhs = (*(t_CKINT *)(reg_sp));
2180 // right
2181 rhs = (Chuck_String *)(*(reg_sp+1));
2182
2183 // make sure no null
2184 if( !rhs ) goto null_pointer;
2185
2186 // make new string
2187 result = (Chuck_String *)instantiate_and_initialize_object( vm->env()->ckt_string, shred );
2188
2189 // concat
2190 // result->str = ::ck_itoa(lhs) + rhs->str;
2191 result->set( ::ck_itoa(lhs) + rhs->str() );
2192
2193 // push the reference value to reg stack
2194 push_( reg_sp, (t_CKUINT)(result) );
2195
2196 return;
2197
2198 null_pointer:
2199 // we have a problem
2200 EM_exception(
2201 "NullPointer: (int + string) on line[%lu] in shred[id=%lu:%s]",
2202 m_linepos, shred->xid, shred->name.c_str() );
2203 goto done;
2204
2205 done:
2206 // do something!
2207 shred->is_running = FALSE;
2208 shred->is_done = TRUE;
2209 }
2210
2211
2212
2213
2214 //-----------------------------------------------------------------------------
2215 // name: execute()
2216 // desc: float + string
2217 //-----------------------------------------------------------------------------
2218 void Chuck_Instr_Add_float_string::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2219 {
2220 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2221 t_CKFLOAT lhs = 0;
2222 Chuck_String * rhs = NULL;
2223 Chuck_String * result = NULL;
2224
2225 // pop word from reg stack (1.3.1.0: added size check)
2226 pop_( reg_sp, 1 + (sz_FLOAT / sz_UINT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
2227 // left (2 word)
2228 lhs = (*(t_CKFLOAT *)(reg_sp));
2229 // right (1.3.1.0: added size)
2230 rhs = (Chuck_String *)(*(reg_sp+(sz_FLOAT/sz_INT))); // ISSUE: 64-bit (fixed 1.3.1.0)
2231
2232 // make sure no null
2233 if( !rhs ) goto null_pointer;
2234
2235 // make new string
2236 result = (Chuck_String *)instantiate_and_initialize_object( vm->env()->ckt_string, shred );
2237
2238 // concat
2239 // result->str = ::ck_ftoa(lhs, 4) + rhs->str;
2240 result->set( ::ck_ftoa( lhs, 4 ) + rhs->str() );
2241
2242 // push the reference value to reg stack
2243 push_( reg_sp, (t_CKUINT)(result) );
2244
2245 return;
2246
2247 null_pointer:
2248 // we have a problem
2249 EM_exception(
2250 "NullPointer: (float + string) on line[%lu] in shred[id=%lu:%s]",
2251 m_linepos, shred->xid, shred->name.c_str() );
2252 goto done;
2253
2254 done:
2255 // do something!
2256 shred->is_running = FALSE;
2257 shred->is_done = TRUE;
2258 }
2259
2260
2261
2262
2263 //-----------------------------------------------------------------------------
2264 // name: execute()
2265 // desc: add assign int string
2266 //-----------------------------------------------------------------------------
2267 void Chuck_Instr_Add_int_string_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2268 {
2269 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2270 t_CKINT lhs = 0;
2271 Chuck_String ** rhs_ptr = NULL;
2272
2273 // pop word from reg stack
2274 pop_( reg_sp, 2 );
2275 // the previous reference
2276 rhs_ptr = (Chuck_String **)(*(reg_sp+1));
2277 // copy popped value into memory
2278 lhs = (*(t_CKINT *)(reg_sp));
2279
2280 // make sure no null
2281 if( !(*rhs_ptr) ) goto null_pointer;
2282
2283 // concat
2284 // (*rhs_ptr)->str += ::ck_itoa(lhs);
2285 (*rhs_ptr)->set( (*rhs_ptr)->str() + ::ck_itoa(lhs) );
2286
2287 // push the reference value to reg stack
2288 push_( reg_sp, (t_CKUINT)(*rhs_ptr) );
2289
2290 return;
2291
2292 null_pointer:
2293 // we have a problem
2294 EM_exception(
2295 "NullPointer: (int +=> string) on line[%lu] in shred[id=%lu:%s]",
2296 m_linepos, shred->xid, shred->name.c_str() );
2297 goto done;
2298
2299 done:
2300 // do something!
2301 shred->is_running = FALSE;
2302 shred->is_done = TRUE;
2303 }
2304
2305
2306
2307
2308 //-----------------------------------------------------------------------------
2309 // name: execute()
2310 // desc: add assign float string
2311 //-----------------------------------------------------------------------------
2312 void Chuck_Instr_Add_float_string_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2313 {
2314 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2315 t_CKFLOAT lhs = 0;
2316 Chuck_String ** rhs_ptr = NULL;
2317
2318 // pop word from reg stack (1.3.1.0: added size check)
2319 pop_( reg_sp, 1 + (sz_FLOAT / sz_UINT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
2320 // the previous reference (1.3.1.0: added size check)
2321 rhs_ptr = (Chuck_String **)(*(reg_sp+(sz_FLOAT/sz_UINT))); // ISSUE: 64-bit (fixed 1.3.1.0)
2322 // copy popped value into memory
2323 lhs = (*(t_CKFLOAT *)(reg_sp));
2324
2325 // make sure no null
2326 if( !(*rhs_ptr) ) goto null_pointer;
2327
2328 // concat
2329 // (*rhs_ptr)->str += ::ck_ftoa(lhs, 4);
2330 (*rhs_ptr)->set( (*rhs_ptr)->str() + ::ck_ftoa(lhs, 4) );
2331
2332 // push the reference value to reg stack
2333 push_( reg_sp, (t_CKUINT)(*rhs_ptr) );
2334
2335 return;
2336
2337 null_pointer:
2338 // we have a problem
2339 EM_exception(
2340 "NullPointer: (float +=> string) on line[%lu] in shred[id=%lu:%s]",
2341 m_linepos, shred->xid, shred->name.c_str() );
2342 goto done;
2343
2344 done:
2345 // do something!
2346 shred->is_running = FALSE;
2347 shred->is_done = TRUE;
2348 }
2349
2350
2351
2352 #pragma mark === Stack Operations ===
2353
2354
2355 //-----------------------------------------------------------------------------
2356 // name: execute()
2357 // desc: ...
2358 //-----------------------------------------------------------------------------
2359 void Chuck_Instr_Reg_Push_Imm::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2360 {
2361 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2362
2363 // push val into reg stack
2364 push_( reg_sp, m_val );
2365 }
2366
2367
2368
2369
2370 //-----------------------------------------------------------------------------
2371 // name: execute()
2372 // desc: ...
2373 //-----------------------------------------------------------------------------
2374 void Chuck_Instr_Reg_Push_Imm2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2375 {
2376 t_CKFLOAT *& reg_sp = (t_CKFLOAT *&)shred->reg->sp;
2377
2378 // push val into reg stack
2379 push_( reg_sp, m_val );
2380 }
2381
2382
2383
2384
2385 //-----------------------------------------------------------------------------
2386 // name: execute()
2387 // desc: ...
2388 //-----------------------------------------------------------------------------
2389 void Chuck_Instr_Reg_Push_Imm4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2390 {
2391 t_CKFLOAT *& reg_sp = (t_CKFLOAT *&)shred->reg->sp;
2392
2393 // push val into reg stack
2394 push_( reg_sp, m_val );
2395 push_( reg_sp, m_val2 );
2396 }
2397
2398
2399
2400
2401 //-----------------------------------------------------------------------------
2402 // name: execute()
2403 // desc: push Chuck_VM_Code * onto register stack
2404 //-----------------------------------------------------------------------------
2405 void Chuck_Instr_Reg_Push_Code::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2406 {
2407 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2408
2409 // push val into reg stack
2410 push_( reg_sp, (t_CKUINT)m_code );
2411 }
2412
2413
2414
2415
2416 //-----------------------------------------------------------------------------
2417 // name: params()
2418 // desc: params for printing
2419 //-----------------------------------------------------------------------------
2420 const char * Chuck_Instr_Reg_Push_Code::params() const
2421 {
2422 static char buffer[CK_PRINT_BUF_LENGTH];
2423 snprintf( buffer, CK_PRINT_BUF_LENGTH, "'%s'", m_code ? m_code->name.c_str() : "[null]" );
2424 return buffer;
2425 }
2426
2427
2428
2429
2430 //-----------------------------------------------------------------------------
2431 // name: execute()
2432 // desc: ...
2433 //-----------------------------------------------------------------------------
2434 void Chuck_Instr_Reg_Push_Zero::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2435 {
2436 t_CKBYTE *& reg_sp = (t_CKBYTE *&)shred->reg->sp;
2437
2438 // push val into reg stack
2439 memset( reg_sp, 0, m_val );
2440 // move stack pointer
2441 reg_sp += m_val;
2442 }
2443
2444
2445
2446
2447 //-----------------------------------------------------------------------------
2448 // name: execute()
2449 // desc: ...
2450 //-----------------------------------------------------------------------------
2451 void Chuck_Instr_Reg_Dup_Last::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2452 {
2453 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2454
2455 // dup val into reg stack
2456 push_( reg_sp, *(reg_sp-1) );
2457 }
2458
2459
2460
2461
2462 //-----------------------------------------------------------------------------
2463 // name: execute()
2464 // desc: ...
2465 //-----------------------------------------------------------------------------
2466 void Chuck_Instr_Reg_Dup_Last2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2467 {
2468 t_CKFLOAT *& reg_sp = (t_CKFLOAT *&)shred->reg->sp;
2469
2470 // dup val into reg stack
2471 push_( reg_sp, *(reg_sp-1) );
2472 }
2473
2474
2475
2476
2477 //-----------------------------------------------------------------------------
2478 // allocate memory in special-primitive storage; used for vec2/3/4 member function calls
2479 // 1.5.4.2 (ge) added as part of #special-primitive-member-func-from-literal
2480 //-----------------------------------------------------------------------------
2481 static t_CKBYTE * special_primitive_alloc( const t_CKBYTE * copyFrom, t_CKUINT numBytes )
2482 {
2483 t_CKBYTE * copy = new t_CKBYTE[numBytes];
2484 memcpy( copy, copyFrom, numBytes );
2485 return copy;
2486 }
2487 //-----------------------------------------------------------------------------
2488 // reclaim memory in special-primitive storage; used for vec2/3/4 member function calls
2489 // 1.5.4.2 (ge) added as part of #special-primitive-member-func-from-literal
2490 //-----------------------------------------------------------------------------
2491 static void special_primitive_cleanup( t_CKBYTE * reclaimMe )
2492 {
2493 CK_SAFE_DELETE_ARRAY( reclaimMe );
2494 }
2495 //-----------------------------------------------------------------------------
2496 // name: execute() | 1.5.4.2 (ge) added
2497 // desc: assume a (primitive, e.g., vec2/3/4) value on reg stack, pop from stack,
2498 // put into special-primitive storage, push its address to reg
2499 // stack for a potential function call #special-primitive-member-func-from-literal
2500 //-----------------------------------------------------------------------------
2501 void Chuck_Instr_Reg_Transmute_Value_To_Pointer::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2502 {
2503 // stack pointer
2504 t_CKBYTE *& reg_sp = (t_CKBYTE *&)shred->reg->sp;
2505 // pop the specified number of bytes
2506 pop_( reg_sp, m_val );
2507 // allocate
2508 t_CKBYTE * ptr = special_primitive_alloc( reg_sp, m_val );
2509 // get as uint pointer so the push_() pointer arithmetic works correctly
2510 t_CKUINT *& the_sp = (t_CKUINT *&)reg_sp;
2511 // push pointer into reg stack
2512 push_( the_sp, (t_CKUINT)ptr );
2513 }
2514
2515
2516
2517
2518 //-----------------------------------------------------------------------------
2519 // name: execute()
2520 // desc: ...
2521 //-----------------------------------------------------------------------------
2522 void Chuck_Instr_Reg_Push_Now::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2523 {
2524 t_CKTIME *& reg_sp = (t_CKTIME *&)shred->reg->sp;
2525
2526 // push val into reg stack
2527 push_( reg_sp, shred->now );
2528 }
2529
2530
2531
2532
2533 //-----------------------------------------------------------------------------
2534 // name: execute()
2535 // desc: ...
2536 //-----------------------------------------------------------------------------
2537 void Chuck_Instr_Reg_Push_Me::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2538 {
2539 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2540
2541 // push val into reg stack
2542 push_( reg_sp, (t_CKUINT)shred );
2543 }
2544
2545
2546
2547
2548 //-----------------------------------------------------------------------------
2549 // name: execute()
2550 // desc: ...
2551 //-----------------------------------------------------------------------------
2552 void Chuck_Instr_Reg_Push_This::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2553 {
2554 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2555 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
2556
2557 // push val into reg stack
2558 push_( reg_sp, *(mem_sp) );
2559 }
2560
2561
2562
2563
2564 //-----------------------------------------------------------------------------
2565 // name: execute()
2566 // desc: ...
2567 //-----------------------------------------------------------------------------
2568 void Chuck_Instr_Reg_Push_Start::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2569 {
2570 t_CKTIME *& reg_sp = (t_CKTIME *&)shred->reg->sp;
2571
2572 // push val into reg stack
2573 push_( reg_sp, shred->start );
2574 }
2575
2576
2577
2578
2579 //-----------------------------------------------------------------------------
2580 // name: execute()
2581 // desc: ...
2582 //-----------------------------------------------------------------------------
2583 void Chuck_Instr_Reg_Push_Maybe::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2584 {
2585 t_CKINT *& reg_sp = (t_CKINT *&)shred->reg->sp;
2586
2587 // push val into reg stack
2588 t_CKFLOAT num = ck_random_f();
2589 push_( reg_sp, num > .5 );
2590 }
2591
2592
2593
2594
2595 //-----------------------------------------------------------------------------
2596 // name: execute()
2597 // desc: push the value pointed to by m_val onto register stack
2598 //-----------------------------------------------------------------------------
2599 void Chuck_Instr_Reg_Push_Deref::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2600 {
2601 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2602
2603 // (added 1.3.1.0: made this integer only)
2604 // ISSUE: 64-bit (fixed 1.3.1.0)
2605 push_( reg_sp, *((t_CKUINT *)m_val) );
2606 }
2607
2608
2609
2610
2611 //-----------------------------------------------------------------------------
2612 // name: execute()
2613 // desc: push the value pointed to by m_val onto register stack
2614 //-----------------------------------------------------------------------------
2615 void Chuck_Instr_Reg_Push_Deref2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2616 {
2617 t_CKFLOAT *& reg_sp = (t_CKFLOAT *&)shred->reg->sp;
2618
2619 // (added 1.3.1.0)
2620 push_( reg_sp, *((t_CKFLOAT *)m_val) );
2621 }
2622
2623
2624
2625
2626 //-----------------------------------------------------------------------------
2627 // name: execute()
2628 // desc: push value from memory stack to register stack
2629 //-----------------------------------------------------------------------------
2630 void Chuck_Instr_Reg_Push_Mem::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2631 {
2632 t_CKBYTE *& mem_sp = (t_CKBYTE *&)(base?shred->base_ref->stack:shred->mem->sp);
2633 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2634
2635 // push mem stack content into reg stack
2636 push_( reg_sp, *((t_CKUINT *)(mem_sp + m_val)) );
2637 }
2638
2639
2640
2641
2642 //-----------------------------------------------------------------------------
2643 // name: execute()
2644 // desc: ...
2645 //-----------------------------------------------------------------------------
2646 void Chuck_Instr_Reg_Push_Mem2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2647 {
2648 t_CKBYTE *& mem_sp = (t_CKBYTE *&)(base?shred->base_ref->stack:shred->mem->sp);
2649 t_CKFLOAT *& reg_sp = (t_CKFLOAT *&)shred->reg->sp;
2650
2651 // push mem stack content into reg stack
2652 push_( reg_sp, *((t_CKFLOAT *)(mem_sp + m_val)) );
2653 }
2654
2655
2656
2657
2658 //-----------------------------------------------------------------------------
2659 // name: execute()
2660 // desc: ...
2661 //-----------------------------------------------------------------------------
2662 void Chuck_Instr_Reg_Push_Mem4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2663 {
2664 t_CKBYTE *& mem_sp = (t_CKBYTE *&)(base?shred->base_ref->stack:shred->mem->sp);
2665 t_CKCOMPLEX *& reg_sp = (t_CKCOMPLEX *&)shred->reg->sp;
2666
2667 // push mem stack content into reg stack
2668 push_( reg_sp, *((t_CKCOMPLEX *)(mem_sp + m_val)) );
2669 }
2670
2671
2672
2673
2674 //-----------------------------------------------------------------------------
2675 // name: execute()
2676 // desc: ...
2677 //-----------------------------------------------------------------------------
2678 void Chuck_Instr_Reg_Push_Mem_Vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2679 {
2680 t_CKBYTE *& mem_sp = (t_CKBYTE *&)(base?shred->base_ref->stack:shred->mem->sp);
2681 t_CKVEC3 *& reg_sp = (t_CKVEC3 *&)shred->reg->sp;
2682
2683 // push mem stack content into reg stack
2684 push_( reg_sp, *((t_CKVEC3 *)(mem_sp + m_val)) );
2685 }
2686
2687
2688
2689
2690 //-----------------------------------------------------------------------------
2691 // name: execute()
2692 // desc: ...
2693 //-----------------------------------------------------------------------------
2694 void Chuck_Instr_Reg_Push_Mem_Vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2695 {
2696 t_CKBYTE *& mem_sp = (t_CKBYTE *&)(base?shred->base_ref->stack:shred->mem->sp);
2697 t_CKVEC4 *& reg_sp = (t_CKVEC4 *&)shred->reg->sp;
2698
2699 // push mem stack content into reg stack
2700 push_( reg_sp, *((t_CKVEC4 *)(mem_sp + m_val)) );
2701 }
2702
2703
2704
2705
2706 //-----------------------------------------------------------------------------
2707 // name: execute()
2708 // desc: push value from global maps to register stack
2709 //-----------------------------------------------------------------------------
2710 void Chuck_Instr_Reg_Push_Global::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2711 {
2712 // get global map content
2713 switch( m_type ) {
2714 case te_globalInt:
2715 {
2716 // int pointer to registers
2717 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2718 t_CKUINT val = (t_CKUINT) vm->globals_manager()->get_global_int_value( m_name );
2719
2720 // push global map content into int-reg stack
2721 push_( reg_sp, val );
2722 }
2723 break;
2724 case te_globalFloat:
2725 {
2726 // float pointer to registers
2727 t_CKFLOAT *& reg_sp = (t_CKFLOAT *&)shred->reg->sp;
2728 t_CKFLOAT val = (t_CKFLOAT) vm->globals_manager()->get_global_float_value( m_name );
2729
2730 // push global map content into float-reg stack
2731 push_( reg_sp, val );
2732 }
2733 break;
2734 case te_globalString:
2735 {
2736 // pointer to registers
2737 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2738 t_CKUINT val = (t_CKUINT) vm->globals_manager()->get_global_string( m_name );
2739
2740 // push global map content into string-reg stack
2741 push_( reg_sp, val );
2742 }
2743 break;
2744 case te_globalEvent:
2745 {
2746 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2747 t_CKUINT val = (t_CKUINT) vm->globals_manager()->get_global_event( m_name );
2748
2749 // push global map content into event-reg stack
2750 push_( reg_sp, val );
2751 }
2752 break;
2753 case te_globalUGen:
2754 {
2755 if( !vm->globals_manager()->is_global_ugen_valid( m_name ) )
2756 {
2757 // we have a problem
2758 EM_exception(
2759 "UninitializedUGen: on line[%lu] in shred[id=%lu:%s]",
2760 m_linepos, shred->xid, shred->name.c_str());
2761 EM_exception(
2762 "...(hint: need to declare global UGen earlier in file)" );
2763 goto error;
2764 }
2765
2766 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2767 t_CKUINT val = (t_CKUINT) vm->globals_manager()->get_global_ugen( m_name );
2768
2769 // push global map content into ugen-reg stack
2770 push_( reg_sp, val );
2771 }
2772 break;
2773 case te_globalObject:
2774 {
2775 if( !vm->globals_manager()->is_global_object_valid( m_name ) )
2776 {
2777 // we have a problem
2778 EM_exception(
2779 "UninitializedObject: on line[%lu] in shred[id=%lu:%s]",
2780 m_linepos, shred->xid, shred->name.c_str());
2781 EM_exception(
2782 "...(hint: need to declare global Object earlier in file)" );
2783 goto error;
2784 }
2785
2786 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2787 t_CKUINT val = (t_CKUINT) vm->globals_manager()->get_global_object( m_name );
2788
2789 // push global map content into object-reg stack
2790 push_( reg_sp, val );
2791 }
2792 break;
2793 case te_globalArraySymbol:
2794 {
2795 // all array allocations return a Chuck_Object * casted to an int
2796 // --> put exactly that on the stack
2797 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2798 t_CKUINT val = (t_CKUINT) vm->globals_manager()->get_global_array( m_name );
2799
2800 // push global map content into object-reg stack
2801 push_( reg_sp, val );
2802
2803 }
2804 break;
2805 default:
2806 // we have a problem | 1.5.0.1 (ge) added
2807 EM_exception(
2808 "UnhandledGlobalType: on line[%lu] in shred[id=%lu:%s] unhandled-type[flag=%d]",
2809 m_linepos, shred->xid, shred->name.c_str(), m_type );
2810 goto error;
2811 break;
2812 }
2813
2814 return;
2815
2816 error:
2817 // do something!
2818 shred->is_running = FALSE;
2819 shred->is_done = TRUE;
2820
2821 }
2822
2823
2824
2825
2826 //-----------------------------------------------------------------------------
2827 // name: execute()
2828 // desc: ...
2829 //-----------------------------------------------------------------------------
2830 void Chuck_Instr_Reg_Push_Mem_Addr::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2831 {
2832 t_CKBYTE *& mem_sp = (t_CKBYTE *&)(base?shred->base_ref->stack:shred->mem->sp);
2833 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2834
2835 // push mem stack addr into reg stack
2836 push_( reg_sp, (t_CKUINT)(mem_sp + m_val) );
2837 }
2838
2839
2840
2841
2842 //-----------------------------------------------------------------------------
2843 // name: execute()
2844 // desc: ...
2845 //-----------------------------------------------------------------------------
2846 void Chuck_Instr_Reg_Push_Global_Addr::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2847 {
2848 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2849
2850 // find addr
2851 t_CKUINT addr = 0;
2852 switch( m_type ) {
2853 case te_globalInt:
2854 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_int( m_name );
2855 break;
2856 case te_globalFloat:
2857 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_float( m_name );
2858 break;
2859 case te_globalString:
2860 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_string( m_name );
2861 break;
2862 case te_globalEvent:
2863 // TODO: should this be a * or a * * ?
2864 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_event( m_name );
2865 break;
2866 case te_globalUGen:
2867 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_ugen( m_name );
2868 break;
2869 case te_globalObject:
2870 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_object( m_name );
2871 break;
2872 case te_globalArraySymbol:
2873 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_array( m_name );
2874 break;
2875 default:
2876 EM_error3( "Chuck_Instr_Reg_Push_Global_Addr: unrecognized type flag %d...", m_type );
2877 break;
2878 }
2879
2880 // push mem stack addr into reg stack
2881 push_( reg_sp, addr );
2882 }
2883
2884
2885
2886
2887 //-----------------------------------------------------------------------------
2888 // name: execute()
2889 // desc: ...
2890 //-----------------------------------------------------------------------------
2891 void Chuck_Instr_Reg_Pop_Mem::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2892 {
2893 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
2894 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2895
2896 // pop word from reg stack
2897 pop_( reg_sp, 2 );
2898 // copy popped value into mem stack
2899 *((t_CKUINT *)(mem_sp + *(reg_sp+1) )) = *reg_sp;
2900 }
2901
2902
2903
2904
2905 //-----------------------------------------------------------------------------
2906 // name: execute()
2907 // desc: pop an int value from reg stack
2908 //-----------------------------------------------------------------------------
2909 void Chuck_Instr_Reg_Pop_Int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2910 {
2911 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
2912
2913 // pop word from reg stack
2914 pop_( reg_sp, 1 );
2915 }
2916
2917
2918
2919
2920 //-----------------------------------------------------------------------------
2921 // name: execute()
2922 // desc: pop a float value from reg stack
2923 //-----------------------------------------------------------------------------
2924 void Chuck_Instr_Reg_Pop_Float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2925 {
2926 t_CKFLOAT *& reg_sp = (t_CKFLOAT *&)shred->reg->sp;
2927
2928 // pop word from reg stack
2929 pop_( reg_sp, 1 );
2930 }
2931
2932
2933
2934
2935 //-----------------------------------------------------------------------------
2936 // name: execute()
2937 // desc: pop a vec2/complex/polar value from reg stack
2938 //-----------------------------------------------------------------------------
2939 void Chuck_Instr_Reg_Pop_Vec2ComplexPolar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2940 {
2941 t_CKVEC2 *& reg_sp = (t_CKVEC2 *&)shred->reg->sp;
2942
2943 // pop word from reg stack
2944 pop_( reg_sp, 1 );
2945 }
2946
2947
2948
2949
2950 //-----------------------------------------------------------------------------
2951 // name: execute()
2952 // desc: pop a vec3 value from reg stack
2953 //-----------------------------------------------------------------------------
2954 void Chuck_Instr_Reg_Pop_Vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2955 {
2956 t_CKVEC3 *& reg_sp = (t_CKVEC3 *&)shred->reg->sp;
2957
2958 // pop word from reg stack
2959 pop_( reg_sp, 1 );
2960 }
2961
2962
2963
2964
2965 //-----------------------------------------------------------------------------
2966 // name: execute()
2967 // desc: pop a vec4 value from reg stack
2968 //-----------------------------------------------------------------------------
2969 void Chuck_Instr_Reg_Pop_Vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2970 {
2971 t_CKVEC4 *& reg_sp = (t_CKVEC4 *&)shred->reg->sp;
2972
2973 // pop word from reg stack
2974 pop_( reg_sp, 1 );
2975 }
2976
2977
2978
2979
2980 //-----------------------------------------------------------------------------
2981 // name: execute()
2982 // desc: ...
2983 //-----------------------------------------------------------------------------
2984 void Chuck_Instr_Reg_Pop_WordsMulti::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
2985 {
2986 t_CKBYTE *& reg_sp = (t_CKBYTE *&)shred->reg->sp;
2987
2988 // pop word from reg stack (changed 1.3.1.0 to use sz_WORD)
2989 pop_( reg_sp, m_val * sz_WORD );
2990 }
2991
2992
2993
2994
2995 #pragma mark === Memory Operations ===
2996
2997
2998
2999 //-----------------------------------------------------------------------------
3000 // name: execute()
3001 // desc: ...
3002 //-----------------------------------------------------------------------------
3003 void Chuck_Instr_Mem_Set_Imm::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3004 {
3005 t_CKUINT * mem_sp = (t_CKUINT *)(shred->mem->sp + m_offset);
3006
3007 // set
3008 *(mem_sp) = m_val;
3009 }
3010
3011
3012
3013
3014 //-----------------------------------------------------------------------------
3015 // name: execute()
3016 // desc: ...
3017 //-----------------------------------------------------------------------------
3018 void Chuck_Instr_Mem_Set_Imm2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3019 {
3020 t_CKFLOAT * mem_sp = (t_CKFLOAT *)(shred->mem->sp + m_offset);
3021
3022 // set
3023 *(mem_sp) = m_val;
3024 }
3025
3026
3027
3028
3029 //-----------------------------------------------------------------------------
3030 // name: execute()
3031 // desc: ...
3032 //-----------------------------------------------------------------------------
3033 void Chuck_Instr_Mem_Push_Imm::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3034 {
3035 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
3036
3037 // pop word from reg stack
3038 push_( mem_sp, m_val );
3039 }
3040
3041
3042
3043
3044 //-----------------------------------------------------------------------------
3045 // name: execute()
3046 // desc: ...
3047 //-----------------------------------------------------------------------------
3048 void Chuck_Instr_Mem_Push_Imm2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3049 {
3050 t_CKFLOAT *& mem_sp = (t_CKFLOAT *&)shred->mem->sp;
3051
3052 // pop word from reg stack
3053 push_( mem_sp, m_val );
3054 }
3055
3056
3057
3058
3059 //-----------------------------------------------------------------------------
3060 // name: execute()
3061 // desc: ...
3062 //-----------------------------------------------------------------------------
3063 void Chuck_Instr_Mem_Pop_Word::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3064 {
3065 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
3066
3067 // pop word from reg stack
3068 pop_( mem_sp, 1 );
3069 }
3070
3071
3072
3073
3074 //-----------------------------------------------------------------------------
3075 // name: execute()
3076 // desc: ...
3077 //-----------------------------------------------------------------------------
3078 void Chuck_Instr_Mem_Pop_Word2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3079 {
3080 t_CKFLOAT *& mem_sp = (t_CKFLOAT *&)shred->mem->sp;
3081
3082 // pop word from reg stack
3083 pop_( mem_sp, 1 );
3084 }
3085
3086
3087
3088
3089 //-----------------------------------------------------------------------------
3090 // name: execute()
3091 // desc: ...
3092 //-----------------------------------------------------------------------------
3093 void Chuck_Instr_Mem_Pop_Word3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3094 {
3095 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
3096
3097 // pop word from reg stack
3098 pop_( mem_sp, m_val );
3099 }
3100
3101
3102
3103 #pragma mark === Branching ===
3104
3105
3106 //-----------------------------------------------------------------------------
3107 // name: execute()
3108 // desc: ...
3109 //-----------------------------------------------------------------------------
3110 void Chuck_Instr_Branch_Lt_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3111 {
3112 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3113 pop_( sp, 2 );
3114 if( val_(sp) < val_(sp+1) )
3115 shred->next_pc = m_jmp;
3116 }
3117
3118
3119
3120
3121 //-----------------------------------------------------------------------------
3122 // name: execute()
3123 // desc: ...
3124 //-----------------------------------------------------------------------------
3125 void Chuck_Instr_Branch_Gt_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3126 {
3127 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3128 pop_( sp, 2 );
3129 if( val_(sp) > val_(sp+1) )
3130 shred->next_pc = m_jmp;
3131 }
3132
3133
3134
3135
3136 //-----------------------------------------------------------------------------
3137 // name: execute()
3138 // desc: ...
3139 //-----------------------------------------------------------------------------
3140 void Chuck_Instr_Branch_Le_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3141 {
3142 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3143 pop_( sp, 2 );
3144 if( val_(sp) <= val_(sp+1) )
3145 shred->next_pc = m_jmp;
3146 }
3147
3148
3149
3150
3151 //-----------------------------------------------------------------------------
3152 // name: execute()
3153 // desc: ...
3154 //-----------------------------------------------------------------------------
3155 void Chuck_Instr_Branch_Ge_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3156 {
3157 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3158 pop_( sp, 2 );
3159 if( val_(sp) >= val_(sp+1) )
3160 shred->next_pc = m_jmp;
3161 }
3162
3163
3164
3165
3166 //-----------------------------------------------------------------------------
3167 // name: execute()
3168 // desc: ...
3169 //-----------------------------------------------------------------------------
3170 void Chuck_Instr_Branch_Eq_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3171 {
3172 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3173 pop_( sp, 2 );
3174 if( val_(sp) == val_(sp+1) )
3175 shred->next_pc = m_jmp;
3176 }
3177
3178
3179
3180
3181 //-----------------------------------------------------------------------------
3182 // name: execute()
3183 // desc: ...
3184 //-----------------------------------------------------------------------------
3185 void Chuck_Instr_Branch_Neq_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3186 {
3187 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3188 pop_( sp, 2 );
3189 if( val_(sp) != val_(sp+1) )
3190 shred->next_pc = m_jmp;
3191 }
3192
3193
3194
3195
3196 //-----------------------------------------------------------------------------
3197 // name: execute()
3198 // desc: ...
3199 //-----------------------------------------------------------------------------
3200 void Chuck_Instr_Not_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3201 {
3202 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3203 (*(sp-1)) = !(*(sp-1));
3204 }
3205
3206
3207
3208
3209 //-----------------------------------------------------------------------------
3210 // name: execute()
3211 // desc: ...
3212 //-----------------------------------------------------------------------------
3213 void Chuck_Instr_Negate_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3214 {
3215 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3216 (*(sp-1)) = -(*(sp-1));
3217 }
3218
3219
3220
3221
3222 //-----------------------------------------------------------------------------
3223 // name: execute()
3224 // desc: ...
3225 //-----------------------------------------------------------------------------
3226 void Chuck_Instr_Negate_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3227 {
3228 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3229 (*(sp-1)) = -(*(sp-1));
3230 }
3231
3232
3233
3234
3235 //-----------------------------------------------------------------------------
3236 // name: execute()
3237 // desc: ...
3238 //-----------------------------------------------------------------------------
3239 void Chuck_Instr_Negate_vec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3240 {
3241 t_CKVEC2* sp = (t_CKVEC2 *)shred->reg->sp;
3242 (sp-1)->x *= -1;
3243 (sp-1)->y *= -1;
3244 }
3245
3246
3247
3248
3249 //-----------------------------------------------------------------------------
3250 // name: execute()
3251 // desc: ...
3252 //-----------------------------------------------------------------------------
3253 void Chuck_Instr_Negate_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3254 {
3255 t_CKVEC3* sp = (t_CKVEC3 *)shred->reg->sp;
3256 (sp-1)->x *= -1;
3257 (sp-1)->y *= -1;
3258 (sp-1)->z *= -1;
3259 }
3260
3261
3262
3263
3264 //-----------------------------------------------------------------------------
3265 // name: execute()
3266 // desc: ...
3267 //-----------------------------------------------------------------------------
3268 void Chuck_Instr_Negate_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3269 {
3270 t_CKVEC4* sp = (t_CKVEC4 *)shred->reg->sp;
3271 (sp-1)->x *= -1;
3272 (sp-1)->y *= -1;
3273 (sp-1)->z *= -1;
3274 (sp-1)->w *= -1;
3275 }
3276
3277
3278
3279
3280 //-----------------------------------------------------------------------------
3281 // name: execute()
3282 // desc: ...
3283 //-----------------------------------------------------------------------------
3284 void Chuck_Instr_Branch_Lt_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3285 {
3286 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3287 pop_( sp, 2 );
3288 if( val_(sp) < val_(sp+1) )
3289 shred->next_pc = m_jmp;
3290 }
3291
3292
3293
3294
3295 //-----------------------------------------------------------------------------
3296 // name: execute()
3297 // desc: ...
3298 //-----------------------------------------------------------------------------
3299 void Chuck_Instr_Branch_Gt_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3300 {
3301 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3302 pop_( sp, 2 );
3303 if( val_(sp) > val_(sp+1) )
3304 shred->next_pc = m_jmp;
3305 }
3306
3307
3308
3309
3310 //-----------------------------------------------------------------------------
3311 // name: execute()
3312 // desc: ...
3313 //-----------------------------------------------------------------------------
3314 void Chuck_Instr_Branch_Le_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3315 {
3316 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3317 pop_( sp, 2 );
3318 if( val_(sp) <= val_(sp+1) )
3319 shred->next_pc = m_jmp;
3320 }
3321
3322
3323
3324
3325 //-----------------------------------------------------------------------------
3326 // name: execute()
3327 // desc: ...
3328 //-----------------------------------------------------------------------------
3329 void Chuck_Instr_Branch_Ge_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3330 {
3331 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3332 pop_( sp, 2 );
3333 if( val_(sp) >= val_(sp+1) )
3334 shred->next_pc = m_jmp;
3335 }
3336
3337
3338
3339
3340 //-----------------------------------------------------------------------------
3341 // name: execute()
3342 // desc: ...
3343 //-----------------------------------------------------------------------------
3344 void Chuck_Instr_Branch_Eq_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3345 {
3346 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3347 pop_( sp, 2 );
3348 if( val_(sp) == val_(sp+1) )
3349 shred->next_pc = m_jmp;
3350 }
3351
3352
3353
3354
3355 //-----------------------------------------------------------------------------
3356 // name: execute()
3357 // desc: ...
3358 //-----------------------------------------------------------------------------
3359 void Chuck_Instr_Branch_Neq_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3360 {
3361 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3362 pop_( sp, 2 );
3363 if( val_(sp) != val_(sp+1) )
3364 shred->next_pc = m_jmp;
3365 }
3366
3367
3368
3369
3370 //-----------------------------------------------------------------------------
3371 // name: execute()
3372 // desc: ...
3373 //-----------------------------------------------------------------------------
3374 void Chuck_Instr_Branch_Eq_int_IO_good::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3375 {
3376 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3377 Chuck_IO **& ppIO = (Chuck_IO **&)shred->reg->sp;
3378 t_CKINT result = 0;
3379 pop_( sp, 2 );
3380
3381 if( (*ppIO) != NULL )
3382 {
3383 // TODO: verify this logic
3384 result = (*ppIO)->good() && !(*ppIO)->eof();
3385 }
3386
3387 if( result == val_(sp+1) || !(*ppIO) )
3388 shred->next_pc = m_jmp;
3389 }
3390
3391
3392
3393
3394 //-----------------------------------------------------------------------------
3395 // name: execute()
3396 // desc: ...
3397 //-----------------------------------------------------------------------------
3398 void Chuck_Instr_Branch_Neq_int_IO_good::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3399 {
3400 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3401 Chuck_IO **& ppIO = (Chuck_IO **&)shred->reg->sp;
3402 t_CKINT result = 0;
3403 pop_( sp, 2 );
3404
3405 if( (*ppIO) != NULL )
3406 {
3407 // fixed 1.3.0.0 -- removed the t_CKINT
3408 // TODO: verify this logic?
3409 result = (*ppIO)->good() && !(*ppIO)->eof();
3410 }
3411
3412 if( result != val_(sp+1) || !(ppIO) )
3413 shred->next_pc = m_jmp;
3414 }
3415
3416
3417
3418 #pragma mark === Bitwise Arithmetic ===
3419
3420
3421 //-----------------------------------------------------------------------------
3422 // name: execute()
3423 // desc: ...
3424 //-----------------------------------------------------------------------------
3425 void Chuck_Instr_Binary_And::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3426 {
3427 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3428 pop_( sp, 2 );
3429 push_( sp, val_(sp) & val_(sp+1) );
3430 }
3431
3432
3433
3434
3435 //-----------------------------------------------------------------------------
3436 // name: execute()
3437 // desc: ...
3438 //-----------------------------------------------------------------------------
3439 void Chuck_Instr_Binary_Or::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3440 {
3441 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3442 pop_( sp, 2 );
3443 push_( sp, val_(sp) | val_(sp+1) );
3444 }
3445
3446
3447
3448
3449 //-----------------------------------------------------------------------------
3450 // name: execute()
3451 // desc: ...
3452 //-----------------------------------------------------------------------------
3453 void Chuck_Instr_Binary_Xor::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3454 {
3455 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3456 pop_( sp, 2 );
3457 push_( sp, val_(sp) ^ val_(sp+1) );
3458 }
3459
3460
3461
3462
3463 //-----------------------------------------------------------------------------
3464 // name: execute()
3465 // desc: ...
3466 //-----------------------------------------------------------------------------
3467 void Chuck_Instr_Binary_Shift_Right::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3468 {
3469 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3470 pop_( sp, 2 );
3471 push_( sp, val_(sp) >> val_(sp+1) );
3472 }
3473
3474
3475
3476
3477 //-----------------------------------------------------------------------------
3478 // name: execute()
3479 // desc: ...
3480 //-----------------------------------------------------------------------------
3481 void Chuck_Instr_Binary_Shift_Right_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3482 {
3483 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3484 pop_( sp, 2 );
3485 push_( sp, val_(sp+1) >> val_(sp) );
3486 }
3487
3488
3489
3490
3491 //-----------------------------------------------------------------------------
3492 // name: execute()
3493 // desc: ...
3494 //-----------------------------------------------------------------------------
3495 void Chuck_Instr_Binary_Shift_Left::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3496 {
3497 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3498 pop_( sp, 2 );
3499 push_( sp, val_(sp) << val_(sp+1) );
3500 }
3501
3502
3503
3504
3505 //-----------------------------------------------------------------------------
3506 // name: execute()
3507 // desc: ...
3508 //-----------------------------------------------------------------------------
3509 void Chuck_Instr_Binary_Shift_Left_Reverse::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3510 {
3511 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3512 pop_( sp, 2 );
3513 push_( sp, val_(sp+1) << val_(sp) );
3514 }
3515
3516
3517
3518
3519 //-----------------------------------------------------------------------------
3520 // name: execute()
3521 // desc: ...
3522 //-----------------------------------------------------------------------------
3523 void Chuck_Instr_Binary_And_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3524 {
3525 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
3526 pop_( sp, 2 );
3527 temp = **(t_CKINT **)(sp+1) &= val_(sp);
3528 push_( sp, temp );
3529 }
3530
3531
3532
3533
3534 //-----------------------------------------------------------------------------
3535 // name: execute()
3536 // desc: ...
3537 //-----------------------------------------------------------------------------
3538 void Chuck_Instr_Binary_Or_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3539 {
3540 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
3541 pop_( sp, 2 );
3542 temp = **(t_CKINT **)(sp+1) |= val_(sp);
3543 push_( sp, temp );
3544 }
3545
3546
3547
3548
3549 //-----------------------------------------------------------------------------
3550 // name: execute()
3551 // desc: ...
3552 //-----------------------------------------------------------------------------
3553 void Chuck_Instr_Binary_Xor_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3554 {
3555 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
3556 pop_( sp, 2 );
3557 temp = **(t_CKINT **)(sp+1) ^= val_(sp);
3558 push_( sp, temp );
3559 }
3560
3561
3562
3563
3564 //-----------------------------------------------------------------------------
3565 // name: execute()
3566 // desc: ...
3567 //-----------------------------------------------------------------------------
3568 void Chuck_Instr_Binary_Shift_Right_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3569 {
3570 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
3571 pop_( sp, 2 );
3572 temp = **(t_CKINT **)(sp+1) >>= val_(sp);
3573 push_( sp, temp );
3574 }
3575
3576
3577
3578
3579 //-----------------------------------------------------------------------------
3580 // name: execute()
3581 // desc: ...
3582 //-----------------------------------------------------------------------------
3583 void Chuck_Instr_Binary_Shift_Left_Assign::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3584 {
3585 t_CKINT temp, *& sp = (t_CKINT *&)shred->reg->sp;
3586 pop_( sp, 2 );
3587 temp = **(t_CKINT **)(sp+1) <<= val_(sp);
3588 push_( sp, temp );
3589 }
3590
3591
3592
3593 #pragma mark === Comparison ===
3594
3595
3596 //-----------------------------------------------------------------------------
3597 // name: execute()
3598 // desc: ...
3599 //-----------------------------------------------------------------------------
3600 void Chuck_Instr_Lt_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3601 {
3602 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3603 pop_( sp, 2 );
3604 push_( sp, val_(sp) < val_(sp+1) );
3605 }
3606
3607
3608
3609
3610 //-----------------------------------------------------------------------------
3611 // name: execute()
3612 // desc: ...
3613 //-----------------------------------------------------------------------------
3614 void Chuck_Instr_Gt_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3615 {
3616 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3617 pop_( sp, 2 );
3618 push_( sp, val_(sp) > val_(sp+1) );
3619 }
3620
3621
3622
3623
3624 //-----------------------------------------------------------------------------
3625 // name: execute()
3626 // desc: ...
3627 //-----------------------------------------------------------------------------
3628 void Chuck_Instr_Le_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3629 {
3630 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3631 pop_( sp, 2 );
3632 push_( sp, val_(sp) <= val_(sp+1) );
3633 }
3634
3635
3636
3637
3638 //-----------------------------------------------------------------------------
3639 // name: execute()
3640 // desc: ...
3641 //-----------------------------------------------------------------------------
3642 void Chuck_Instr_Ge_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3643 {
3644 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3645 pop_( sp, 2 );
3646 push_( sp, val_(sp) >= val_(sp+1) );
3647 }
3648
3649
3650
3651
3652 //-----------------------------------------------------------------------------
3653 // name: execute()
3654 // desc: ...
3655 //-----------------------------------------------------------------------------
3656 void Chuck_Instr_Eq_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3657 {
3658 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3659 pop_( sp, 2 );
3660 push_( sp, val_(sp) == val_(sp+1) );
3661 }
3662
3663
3664
3665
3666 //-----------------------------------------------------------------------------
3667 // name: execute()
3668 // desc: ...
3669 //-----------------------------------------------------------------------------
3670 void Chuck_Instr_Neq_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3671 {
3672 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
3673 pop_( sp, 2 );
3674 push_( sp, val_(sp) != val_(sp+1) );
3675 }
3676
3677
3678
3679
3680 //-----------------------------------------------------------------------------
3681 // name: execute()
3682 // desc: ...
3683 //-----------------------------------------------------------------------------
3684 void Chuck_Instr_Lt_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3685 {
3686 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3687 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3688 pop_( sp, 2 );
3689 push_( sp_uint, val_(sp) < val_(sp+1) );
3690 }
3691
3692
3693
3694
3695 //-----------------------------------------------------------------------------
3696 // name: execute()
3697 // desc: ...
3698 //-----------------------------------------------------------------------------
3699 void Chuck_Instr_Gt_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3700 {
3701 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3702 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3703 pop_( sp, 2 );
3704 push_( sp_uint, val_(sp) > val_(sp+1) );
3705 }
3706
3707
3708
3709
3710 //-----------------------------------------------------------------------------
3711 // name: execute()
3712 // desc: ...
3713 //-----------------------------------------------------------------------------
3714 void Chuck_Instr_Le_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3715 {
3716 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3717 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3718 pop_( sp, 2 );
3719 push_( sp_uint, val_(sp) <= val_(sp+1) );
3720 }
3721
3722
3723
3724
3725 //-----------------------------------------------------------------------------
3726 // name: execute()
3727 // desc: ...
3728 //-----------------------------------------------------------------------------
3729 void Chuck_Instr_Ge_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3730 {
3731 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3732 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3733 pop_( sp, 2 );
3734 push_( sp_uint, val_(sp) >= val_(sp+1) );
3735 }
3736
3737
3738
3739
3740 //-----------------------------------------------------------------------------
3741 // name: execute()
3742 // desc: ...
3743 //-----------------------------------------------------------------------------
3744 void Chuck_Instr_Eq_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3745 {
3746 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3747 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3748 pop_( sp, 2 );
3749 push_( sp_uint, val_(sp) == val_(sp+1) );
3750 }
3751
3752
3753
3754
3755 //-----------------------------------------------------------------------------
3756 // name: execute()
3757 // desc: ...
3758 //-----------------------------------------------------------------------------
3759 void Chuck_Instr_Neq_double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3760 {
3761 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
3762 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3763 pop_( sp, 2 );
3764 push_( sp_uint, val_(sp) != val_(sp+1) );
3765 }
3766
3767
3768
3769 //-----------------------------------------------------------------------------
3770 // name: execute()
3771 // desc: ...
3772 //-----------------------------------------------------------------------------
3773 void Chuck_Instr_Eq_complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3774 {
3775 // also works for polar
3776 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
3777 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3778 pop_( sp, 2 );
3779 push_( sp_uint, (sp->re == (sp+1)->re) && (sp->im == (sp+1)->im) );
3780 }
3781
3782
3783
3784
3785 //-----------------------------------------------------------------------------
3786 // name: execute()
3787 // desc: ...
3788 //-----------------------------------------------------------------------------
3789 void Chuck_Instr_Neq_complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3790 {
3791 // also works for polar
3792 t_CKCOMPLEX *& sp = (t_CKCOMPLEX *&)shred->reg->sp;
3793 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3794 pop_( sp, 2 );
3795 push_( sp_uint, (sp->re != (sp+1)->re) || (sp->im != (sp+1)->im) );
3796 }
3797
3798
3799
3800
3801 //-----------------------------------------------------------------------------
3802 // name: execute()
3803 // desc: vec2 == vec2
3804 //-----------------------------------------------------------------------------
3805 void Chuck_Instr_Eq_vec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3806 {
3807 // also works for polar
3808 t_CKVEC2 *& sp = (t_CKVEC2 *&)shred->reg->sp;
3809 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3810 pop_( sp, 2 );
3811 push_( sp_uint, (sp->x == (sp+1)->x) && (sp->y == (sp+1)->y) );
3812 }
3813
3814
3815
3816
3817 //-----------------------------------------------------------------------------
3818 // name: execute()
3819 // desc: vec2 != vec2
3820 //-----------------------------------------------------------------------------
3821 void Chuck_Instr_Neq_vec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3822 {
3823 // also works for polar
3824 t_CKVEC2 *& sp = (t_CKVEC2 *&)shred->reg->sp;
3825 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3826 pop_( sp, 2 );
3827 push_( sp_uint, (sp->x != (sp+1)->x) || (sp->y != (sp+1)->y) );
3828 }
3829
3830
3831
3832
3833 //-----------------------------------------------------------------------------
3834 // name: execute()
3835 // desc: vec3 == vec3
3836 //-----------------------------------------------------------------------------
3837 void Chuck_Instr_Eq_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3838 {
3839 // also works for polar
3840 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
3841 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3842 pop_( sp, 2 );
3843 push_( sp_uint, (sp->x == (sp+1)->x) && (sp->y == (sp+1)->y)
3844 && (sp->z == (sp+1)->z) );
3845 }
3846
3847
3848
3849
3850 //-----------------------------------------------------------------------------
3851 // name: execute()
3852 // desc: vec3 != vec3
3853 //-----------------------------------------------------------------------------
3854 void Chuck_Instr_Neq_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3855 {
3856 // also works for polar
3857 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
3858 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3859 pop_( sp, 2 );
3860 push_( sp_uint, (sp->x != (sp+1)->x) || (sp->y != (sp+1)->y)
3861 || (sp->z != (sp+1)->z) );
3862 }
3863
3864
3865
3866
3867 //-----------------------------------------------------------------------------
3868 // name: execute()
3869 // desc: vec4 == vec4
3870 //-----------------------------------------------------------------------------
3871 void Chuck_Instr_Eq_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3872 {
3873 // also works for polar
3874 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
3875 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3876 pop_( sp, 2 );
3877 push_( sp_uint, (sp->x == (sp+1)->x) && (sp->y == (sp+1)->y)
3878 && (sp->z == (sp+1)->z) && (sp->w == (sp+1)->w) );
3879 }
3880
3881
3882
3883
3884 //-----------------------------------------------------------------------------
3885 // name: execute()
3886 // desc: vec4 != vec4
3887 //-----------------------------------------------------------------------------
3888 void Chuck_Instr_Neq_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3889 {
3890 // also works for polar
3891 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
3892 t_CKUINT *& sp_uint = (t_CKUINT *&)sp;
3893 pop_( sp, 2 );
3894 push_( sp_uint, (sp->x != (sp+1)->x) || (sp->y != (sp+1)->y)
3895 || (sp->z != (sp+1)->z) || (sp->w != (sp+1)->w) );
3896 }
3897
3898
3899
3900
3901 #pragma mark === Boolean Arithmetic ===
3902
3903
3904 //-----------------------------------------------------------------------------
3905 // name: execute()
3906 // desc: ...
3907 //-----------------------------------------------------------------------------
3908 void Chuck_Instr_And::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3909 {
3910 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3911 pop_( sp, 2 );
3912 push_( sp, val_(sp) && val_(sp+1) );
3913 }
3914
3915
3916
3917
3918 //-----------------------------------------------------------------------------
3919 // name: execute()
3920 // desc: ...
3921 //-----------------------------------------------------------------------------
3922 void Chuck_Instr_Or::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3923 {
3924 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3925 pop_( sp, 2 );
3926 push_( sp, val_(sp) || val_(sp+1) );
3927 }
3928
3929
3930
3931
3932 //-----------------------------------------------------------------------------
3933 // name: execute()
3934 // desc: ...
3935 //-----------------------------------------------------------------------------
3936 void Chuck_Instr_Boolify::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3937 {
3938 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
3939 pop_( sp, 1 );
3940 t_CKINT v = val_(sp);
3941 push_( sp, v != 0 );
3942 }
3943
3944
3945
3946
3947 #pragma mark === Miscellany ===
3948
3949
3950 //-----------------------------------------------------------------------------
3951 // name: execute()
3952 // desc: ...
3953 //-----------------------------------------------------------------------------
3954 void Chuck_Instr_Goto::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3955 {
3956 shred->next_pc = m_jmp;
3957 }
3958
3959
3960
3961
3962 //-----------------------------------------------------------------------------
3963 // name: execute()
3964 // desc: ...
3965 //-----------------------------------------------------------------------------
3966 void Chuck_Instr_Nop::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3967 {
3968 // no op
3969 }
3970
3971
3972
3973
3974 //-----------------------------------------------------------------------------
3975 // name: execute()
3976 // desc: ...
3977 //-----------------------------------------------------------------------------
3978 void Chuck_Instr_EOC::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3979 {
3980 // end the shred
3981 shred->is_done = TRUE;
3982 shred->is_running = FALSE;
3983 }
3984
3985
3986
3987 #pragma mark === Allocation ===
3988
3989
3990
3991
3992 //-----------------------------------------------------------------------------
3993 // name: execute()
3994 // desc: alloc local variable (int)
3995 //-----------------------------------------------------------------------------
3996 void Chuck_Instr_Alloc_Word::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
3997 {
3998 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
3999 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4000
4001 // overflow detection | 1.5.1.5 (ge) added
4002 if( would_overflow_( mem_sp+m_val, shred->mem ) ) goto overflow;
4003
4004 // zero out the memory stack
4005 *( (t_CKUINT *)(mem_sp + m_val) ) = 0;
4006 // push addr onto operand stack
4007 push_( reg_sp, (t_CKUINT)(mem_sp + m_val) );
4008
4009 // done
4010 return;
4011
4012 overflow:
4013 // handle overflow
4014 ck_handle_overflow( shred, vm, "too many local variables" );
4015 }
4016
4017
4018
4019
4020 //-----------------------------------------------------------------------------
4021 // name: execute()
4022 // desc: alloc local variable (float)
4023 //-----------------------------------------------------------------------------
4024 void Chuck_Instr_Alloc_Word2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4025 {
4026 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
4027 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4028
4029 // overflow detection | 1.5.1.5 (ge) added
4030 if( would_overflow_( mem_sp+m_val, shred->mem ) ) goto overflow;
4031
4032 // zero out the memory stack
4033 *( (t_CKFLOAT *)(mem_sp + m_val) ) = 0.0;
4034 // push addr onto operand stack
4035 push_( reg_sp, (t_CKUINT)(mem_sp + m_val) );
4036
4037 // done
4038 return;
4039
4040 overflow:
4041 // handle overflow
4042 ck_handle_overflow( shred, vm, "too many local variables" );
4043 }
4044
4045
4046
4047
4048 //-----------------------------------------------------------------------------
4049 // name: execute()
4050 // desc: alloc local variable (complex or polar)
4051 //-----------------------------------------------------------------------------
4052 void Chuck_Instr_Alloc_Word4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4053 {
4054 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
4055 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4056
4057 // overflow detection | 1.5.1.5 (ge) added
4058 if( would_overflow_( mem_sp+m_val, shred->mem ) ) goto overflow;
4059
4060 // zero out the memory stack
4061 ( (t_CKCOMPLEX *)(mem_sp + m_val) )->re = 0.0;
4062 ( (t_CKCOMPLEX *)(mem_sp + m_val) )->im = 0.0;
4063 // push addr onto operand stack
4064 push_( reg_sp, (t_CKUINT)(mem_sp + m_val) );
4065
4066 // done
4067 return;
4068
4069 overflow:
4070 // handle overflow
4071 ck_handle_overflow( shred, vm, "too many local variables" );
4072 }
4073
4074
4075
4076
4077 //-----------------------------------------------------------------------------
4078 // name: execute()
4079 // desc: alloc local variable (vec3)
4080 //-----------------------------------------------------------------------------
4081 void Chuck_Instr_Alloc_Vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4082 {
4083 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
4084 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4085
4086 // overflow detection | 1.5.1.5 (ge) added
4087 if( would_overflow_( mem_sp+m_val, shred->mem ) ) goto overflow;
4088
4089 // zero out the memory stack
4090 ( (t_CKVEC3 *)(mem_sp + m_val) )->x = 0.0;
4091 ( (t_CKVEC3 *)(mem_sp + m_val) )->y = 0.0;
4092 ( (t_CKVEC3 *)(mem_sp + m_val) )->z = 0.0;
4093 // push addr onto operand stack
4094 push_( reg_sp, (t_CKUINT)(mem_sp + m_val) );
4095
4096 // done
4097 return;
4098
4099 overflow:
4100 // handle overflow
4101 ck_handle_overflow( shred, vm, "too many local variables" );
4102 }
4103
4104
4105
4106
4107 //-----------------------------------------------------------------------------
4108 // name: execute()
4109 // desc: alloc local variable (vec4)
4110 //-----------------------------------------------------------------------------
4111 void Chuck_Instr_Alloc_Vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4112 {
4113 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
4114 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4115
4116 // overflow detection | 1.5.1.5 (ge) added
4117 if( would_overflow_( mem_sp+m_val, shred->mem ) ) goto overflow;
4118
4119 // zero out the memory stack
4120 ( (t_CKVEC4 *)(mem_sp + m_val) )->x = 0.0;
4121 ( (t_CKVEC4 *)(mem_sp + m_val) )->y = 0.0;
4122 ( (t_CKVEC4 *)(mem_sp + m_val) )->z = 0.0;
4123 ( (t_CKVEC4 *)(mem_sp + m_val) )->w = 0.0;
4124 // push addr onto operand stack
4125 push_( reg_sp, (t_CKUINT)(mem_sp + m_val) );
4126
4127 // done
4128 return;
4129
4130 overflow:
4131 // handle overflow
4132 ck_handle_overflow( shred, vm, "too many local variables" );
4133 }
4134
4135
4136
4137
4138 //-----------------------------------------------------------------------------
4139 // name: execute()
4140 // desc: alloc member variable (int)
4141 //-----------------------------------------------------------------------------
4142 void Chuck_Instr_Alloc_Member_Word::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4143 {
4144 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
4145 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4146
4147 // get the object
4148 Chuck_Object * obj = (Chuck_Object *)*(mem_sp);
4149 // zero out the object data block entry
4150 *( (t_CKUINT *)(obj->data + m_val) ) = 0;
4151 // push addr onto operand stack
4152 push_( reg_sp, (t_CKUINT)(obj->data + m_val) );
4153 }
4154
4155
4156
4157
4158 //-----------------------------------------------------------------------------
4159 // name: execute()
4160 // desc: alloc member variable (float)
4161 //-----------------------------------------------------------------------------
4162 void Chuck_Instr_Alloc_Member_Word2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4163 {
4164 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
4165 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4166
4167 // get the object
4168 Chuck_Object * obj = (Chuck_Object *)*(mem_sp);
4169 // zero out the object data block entry
4170 *( (t_CKFLOAT *)(obj->data + m_val) ) = 0.0;
4171 // push addr onto operand stack
4172 push_( reg_sp, (t_CKUINT)(obj->data + m_val) );
4173 }
4174
4175
4176
4177
4178 //-----------------------------------------------------------------------------
4179 // name: execute()
4180 // desc: alloc member variable (complex or polar)
4181 //-----------------------------------------------------------------------------
4182 void Chuck_Instr_Alloc_Member_Word4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4183 {
4184 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
4185 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4186
4187 // get the object
4188 Chuck_Object * obj = (Chuck_Object *)*(mem_sp);
4189 // zero out the object data block entry
4190 ( (t_CKCOMPLEX *)(obj->data + m_val) )->re = 0.0;
4191 ( (t_CKCOMPLEX *)(obj->data + m_val) )->im = 0.0;
4192 // push addr onto operand stack
4193 push_( reg_sp, (t_CKUINT)(obj->data + m_val) );
4194 }
4195
4196
4197
4198
4199 //-----------------------------------------------------------------------------
4200 // name: execute()
4201 // desc: alloc member variable (vec3)
4202 //-----------------------------------------------------------------------------
4203 void Chuck_Instr_Alloc_Member_Vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4204 {
4205 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
4206 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4207
4208 // get the object
4209 Chuck_Object * obj = (Chuck_Object *)*(mem_sp);
4210 // zero out the object data block entry
4211 ( (t_CKVEC3 *)(obj->data + m_val) )->x = 0.0;
4212 ( (t_CKVEC3 *)(obj->data + m_val) )->y = 0.0;
4213 ( (t_CKVEC3 *)(obj->data + m_val) )->z = 0.0;
4214 // push addr onto operand stack
4215 push_( reg_sp, (t_CKUINT)(obj->data + m_val) );
4216 }
4217
4218
4219
4220
4221 //-----------------------------------------------------------------------------
4222 // name: execute()
4223 // desc: alloc member variable (vec4)
4224 //-----------------------------------------------------------------------------
4225 void Chuck_Instr_Alloc_Member_Vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4226 {
4227 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
4228 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4229
4230 // get the object
4231 Chuck_Object * obj = (Chuck_Object *)*(mem_sp);
4232 // zero out the object data block entry
4233 ( (t_CKVEC4 *)(obj->data + m_val) )->x = 0.0;
4234 ( (t_CKVEC4 *)(obj->data + m_val) )->y = 0.0;
4235 ( (t_CKVEC4 *)(obj->data + m_val) )->z = 0.0;
4236 ( (t_CKVEC4 *)(obj->data + m_val) )->w = 0.0;
4237 // push addr onto operand stack
4238 push_( reg_sp, (t_CKUINT)(obj->data + m_val) );
4239 }
4240
4241
4242
4243
4244 // function prototype
4245 void call_pre_constructor( Chuck_VM * vm, Chuck_VM_Shred * shred,
4246 Chuck_VM_Code * pre_ctor, t_CKUINT stack_offset );
4247 //-----------------------------------------------------------------------------
4248 // name: call_all_parent_pre_constructors()
4249 // desc: traverse up type parent tree calling pre constructors top-down
4250 //-----------------------------------------------------------------------------
4251 void call_all_parent_pre_constructors( Chuck_VM * vm, Chuck_VM_Shred * shred,
4252 Chuck_Type * type, t_CKUINT stack_offset )
4253 {
4254 // first, call parent ctor
4255 if( type->parent_type != NULL )
4256 {
4257 call_all_parent_pre_constructors( vm, shred, type->parent_type, stack_offset );
4258 }
4259 // next, call my pre-ctor
4260 if( type->has_pre_ctor )
4261 {
4262 call_pre_constructor( vm, shred, type->nspc->pre_ctor, stack_offset );
4263 }
4264 // next, call my default ctor | 1.5.2.2 (ge)
4265 if( type->ctor_default && type->ctor_default->code )
4266 {
4267 call_pre_constructor( vm, shred, type->ctor_default->code, stack_offset );
4268 }
4269 }
4270
4271
4272
4273
4274 //-----------------------------------------------------------------------------
4275 // name: execute()
4276 // desc: alloc global
4277 //-----------------------------------------------------------------------------
4278 void Chuck_Instr_Alloc_Word_Global::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4279 {
4280 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4281 t_CKUINT addr = 0;
4282
4283 // init in the correct vm map according to the type
4284 if( m_is_array )
4285 {
4286 switch( m_type )
4287 {
4288 case te_globalInt:
4289 case te_globalFloat:
4290 vm->globals_manager()->init_global_array( m_name, m_chuck_type, m_type );
4291 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_array( m_name );
4292 break;
4293 case te_globalString:
4294 EM_error2( 0, "global string arrays are currently disabled." );
4295 goto error;
4296 break;
4297 case te_globalEvent:
4298 EM_error2( 0, "global Event arrays are currently disabled." );
4299 goto error;
4300 break;
4301 case te_globalUGen:
4302 EM_error2( 0, "global UGen arrays are currently disabled." );
4303 goto error;
4304 break;
4305 case te_globalObject:
4306 EM_error2( 0, "global Object arrays are currently disabled." );
4307 goto error;
4308 break;
4309 case te_globalArraySymbol:
4310 EM_error2( 0, "(internal error) symbol-only global type used in allocation" );
4311 goto error;
4312 default:
4313 // we have a problem | 1.5.0.1 (ge) added
4314 EM_exception(
4315 "UnhandledGlobalType: on line[%lu] in shred[id=%lu:%s] unhandled-type[flag=%d]",
4316 m_linepos, shred->xid, shred->name.c_str(), m_type );
4317 goto error;
4318 break;
4319 }
4320
4321 }
4322 else
4323 {
4324 // not array
4325 switch( m_type ) {
4326 case te_globalInt:
4327 vm->globals_manager()->init_global_int( m_name );
4328 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_int( m_name );
4329 break;
4330 case te_globalFloat:
4331 vm->globals_manager()->init_global_float( m_name );
4332 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_float( m_name );
4333 break;
4334 case te_globalString:
4335 vm->globals_manager()->init_global_string( m_name );
4336 addr = (t_CKUINT) vm->globals_manager()->get_ptr_to_global_string( m_name );
4337 break;
4338 case te_globalEvent:
4339 // events are already init in emit
4340 // but might need to execute ctors (below)
4341 addr = (t_CKUINT) vm->globals_manager()->get_global_event( m_name );
4342 break;
4343 case te_globalUGen:
4344 // ugens are already init in emit
4345 // but might need to execute ctors (below)
4346 addr = (t_CKUINT) vm->globals_manager()->get_global_ugen( m_name );
4347 break;
4348 case te_globalObject:
4349 // object are already init in emit
4350 // but might need to execute ctors (below)
4351 addr = (t_CKUINT) vm->globals_manager()->get_global_object( m_name );
4352 break;
4353 case te_globalArraySymbol:
4354 EM_error2( 0, "(internal error) symbol-only global type used in allocation" );
4355 goto error;
4356 default:
4357 // we have a problem | 1.5.0.1 (ge) added
4358 EM_exception(
4359 "UnhandledGlobalType: on line[%lu] in shred[id=%lu:%s] unhandled-type[flag=%d]",
4360 m_linepos, shred->xid, shred->name.c_str(), m_type );
4361 goto error;
4362 break;
4363 }
4364 }
4365
4366 // push addr onto operand stack
4367 push_( reg_sp, addr );
4368
4369 // if we have ctors to execute, do it
4370 if( m_should_execute_ctors &&
4371 vm->globals_manager()->should_call_global_ctor( m_name, m_type ) )
4372 {
4373 // call ctors (normally done by the pre_constructor instruction)
4374 call_all_parent_pre_constructors( vm, shred,
4375 m_chuck_type, m_stack_offset );
4376 // tell VM we did it so that it will never be done again for m_name
4377 vm->globals_manager()->global_ctor_was_called( m_name, m_type );
4378 }
4379
4380 return;
4381
4382 error:
4383 // do something!
4384 shred->is_running = FALSE;
4385 shred->is_done = TRUE;
4386 }
4387
4388
4389
4390
4391 #pragma mark === Object Initialization/Construction ===
4392
4393
4394 static Chuck_Instr_Func_Call g_func_call;
4395 static Chuck_Instr_Func_Call_Member g_func_call_member( 0, NULL );
4396 //-----------------------------------------------------------------------------
4397 // name: call_pre_constructor()
4398 // desc: invoke class pre-constructor code
4399 //-----------------------------------------------------------------------------
4400 inline void call_pre_constructor( Chuck_VM * vm, Chuck_VM_Shred * shred,
4401 Chuck_VM_Code * pre_ctor, t_CKUINT stack_offset )
4402 {
4403 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4404
4405 // sanity
4406 assert( pre_ctor != NULL );
4407
4408 // first duplicate the top of the stack, which should be object pointer
4409 push_( reg_sp, *(reg_sp-1) );
4410 // push the pre constructor
4411 push_( reg_sp, (t_CKUINT)pre_ctor );
4412 // push the stack offset
4413 push_( reg_sp, stack_offset );
4414
4415 // NOTE (1.5.0.0): pre-constructors by their nature have no arguments...
4416 // if they did, they would need to clean up argument list, in the built-in/native
4417 // func case, by setting g_func_call_member.m_func_ref with the appropriate func
4418
4419 // call the function
4420 if( pre_ctor->native_func != 0 )
4421 g_func_call_member.execute( vm, shred );
4422 else
4423 g_func_call.execute( vm, shred );
4424 }
4425
4426
4427
4428
4429 //-----------------------------------------------------------------------------
4430 // name: execute()
4431 // desc: object pre construct
4432 //-----------------------------------------------------------------------------
4433 void Chuck_Instr_Pre_Constructor::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4434 {
4435 call_pre_constructor( vm, shred, pre_ctor, stack_offset );
4436 }
4437
4438
4439
4440
4441 //-----------------------------------------------------------------------------
4442 // name: params()
4443 // desc: params for printing
4444 //-----------------------------------------------------------------------------
4445 const char * Chuck_Instr_Pre_Constructor::params() const
4446 {
4447 static char buffer[CK_PRINT_BUF_LENGTH];
4448 snprintf( buffer, CK_PRINT_BUF_LENGTH, "ctor='%s', offset=%lu", pre_ctor ? pre_ctor->name.c_str() : "[null]", (unsigned long)stack_offset );
4449 return buffer;
4450 }
4451
4452
4453
4454
4455 //-----------------------------------------------------------------------------
4456 // name: initialize_object()
4457 // desc: initialize Object including data and virtual table
4458 //-----------------------------------------------------------------------------
4459 t_CKBOOL initialize_object( Chuck_Object * object, Chuck_Type * type, Chuck_VM_Shred * shred, Chuck_VM * vm, t_CKBOOL setShredOrigin )
4460 {
4461 // check if already initialized | 1.5.1.5
4462 if( object->vtable != NULL ) return TRUE;
4463
4464 // sanity
4465 assert( type != NULL );
4466 assert( type->nspc != NULL );
4467
4468 // REFACTOR-2017: added | 1.5.1.5 (ge & andrew) moved here from instantiate_...
4469 object->setOriginVM( vm );
4470 // set origin shred for non-ugens | 1.5.1.5 (ge & andrew) moved here from instantiate_...
4471 // change logic: if ugen OR setShredOrigin==TRUE | 1.5.4.2 (ge) part of #ugen-refs
4472 if( type->ugen_info || setShredOrigin ) object->setOriginShred( shred );
4473
4474 // allocate virtual table
4475 object->vtable = new Chuck_VTable;
4476 if( !object->vtable ) goto out_of_memory;
4477 // copy the object's virtual table
4478 object->vtable->funcs = type->nspc->obj_v_table.funcs;
4479 // set the type reference
4480 object->type_ref = type;
4481 // reference count
4482 CK_SAFE_ADD_REF(object->type_ref);
4483 // get the size
4484 object->data_size = type->obj_size;
4485 // allocate memory
4486 if( object->data_size )
4487 {
4488 // check to ensure enough memory
4489 object->data = new t_CKBYTE[object->data_size];
4490 if( !object->data ) goto out_of_memory;
4491 // zero it out
4492 memset( object->data, 0, object->data_size );
4493 }
4494 else object->data = NULL;
4495
4496 // special
4497 if( type->ugen_info )
4498 {
4499 // ugen
4500 Chuck_UGen * ugen = (Chuck_UGen *)object;
4501 //---------------------------------------
4502 // UGens: needs shred for auto-disconnect when shred is removed
4503 // 1.5.1.5 (ge & andrew) moved from instantiate_and_initialize_object()
4504 //---------------------------------------
4505 // 1.5.4.2 (ge) revisiting the above mechanism, part of #ugen-refs
4506 // now UGens are not ref-counted by shred, is subject to the normal GC,
4507 // and when refcount goes to 0, will remove it self from UGen graph
4508 //---------------------------------------
4509 if( shred )
4510 {
4511 // register ugen with originShred; if the shred is preemptively removed
4512 // (e.g., through OTF / Machine.remove()), it will trigger a ugen_detach()
4513 // to disconnect UGens that were created on it...
4514 // 1.5.4.2 (ge) no longer ref-counted as part of #ugen-refs
4515 // FYI the ugen's originShred should be set already (above) for UGens
4516 shred->add( ugen );
4517 }
4518 //---------------------------------------
4519 // set tick
4520 if( type->ugen_info->tick ) ugen->tick = type->ugen_info->tick;
4521 // added 1.3.0.0 -- tickf for multi-channel tick
4522 if( type->ugen_info->tickf ) ugen->tickf = type->ugen_info->tickf;
4523 if( type->ugen_info->pmsg ) ugen->pmsg = type->ugen_info->pmsg;
4524 // TODO: another hack!
4525 if( type->ugen_info->tock ) ((Chuck_UAna *)ugen)->tock = type->ugen_info->tock;
4526 // allocate multi chan
4527 ugen->alloc_multi_chan( type->ugen_info->num_ins,
4528 type->ugen_info->num_outs );
4529 // allocate the channels
4530 for( t_CKUINT i = 0; i < ugen->m_multi_chan_size; i++ )
4531 {
4532 // allocate ugen for each | REFACTOR-2017: added ugen->vm
4533 // NOTE the channels currently are also detached as part of the
4534 // origin shred's ugen_detach() routine when the shred
4535 // is removed; as of 1.5.4.2, however, ugens are no longer
4536 // reference-counted when added to their origin shreds
4537 Chuck_Object * obj = instantiate_and_initialize_object(
4538 ugen->originVM()->env()->ckt_ugen, ugen->originShred(), ugen->originVM() );
4539 // cast to ugen
4540 ugen->m_multi_chan[i] = (Chuck_UGen *)obj;
4541 // additional reference count
4542 CK_SAFE_ADD_REF(obj);
4543 // owner
4544 ugen->m_multi_chan[i]->owner_ugen = ugen;
4545 // ref count
4546 // spencer 2013-5-20: don't add extra ref, to avoid a ref cycle
4547 // ugen->add_ref();
4548 }
4549 // TODO: alloc channels for uana
4550 }
4551
4552 return TRUE;
4553
4554 out_of_memory:
4555
4556 // we have a problem
4557 EM_exception(
4558 "OutOfMemory: while instantiating object '%s'",
4559 type->c_name() );
4560
4561 // delete
4562 if( object ) CK_SAFE_DELETE( object->vtable );
4563
4564 // return FALSE
4565 return FALSE;
4566 }
4567
4568
4569
4570
4571 //-----------------------------------------------------------------------------
4572 // name: instantiate_and_initialize_object()
4573 // desc: call this one if you have a non-null shred
4574 //-----------------------------------------------------------------------------
4575 Chuck_Object * instantiate_and_initialize_object( Chuck_Type * type, Chuck_VM_Shred * shred )
4576 {
4577 assert( shred != NULL );
4578 return instantiate_and_initialize_object( type, shred, shred->vm_ref );
4579 }
4580
4581
4582
4583
4584 //-----------------------------------------------------------------------------
4585 // name: instantiate_and_initialize_object()
4586 // desc: call this one if you don't have a shred
4587 //-----------------------------------------------------------------------------
4588 Chuck_Object * instantiate_and_initialize_object( Chuck_Type * type, Chuck_VM * vm )
4589 {
4590 assert( vm != NULL );
4591 return instantiate_and_initialize_object( type, NULL, vm );
4592 }
4593
4594
4595
4596
4597 //-----------------------------------------------------------------------------
4598 // name: instantiate_and_initialize_object()
4599 // desc: instiate and initialize a ChucK Object of a particular Type
4600 // NOTE: the returned Object will have a reference count of 0
4601 // NOTE: you probably shouldn't call this version. call the one that
4602 // takes a shred if you have a non-null shred, otherwise call the one
4603 // that takes a vm
4604 //-----------------------------------------------------------------------------
4605 Chuck_Object * instantiate_and_initialize_object( Chuck_Type * type, Chuck_VM_Shred * shred, Chuck_VM * vm )
4606 {
4607 Chuck_Object * object = NULL;
4608 Chuck_UGen * ugen = NULL;
4609 Chuck_UAna * uana = NULL;
4610 vector<Chuck_Type::CallbackOnInstantiate> instance_cbs;
4611 t_CKBOOL setShredOrigin = FALSE;
4612
4613 // sanity
4614 assert( type != NULL );
4615 assert( type->nspc != NULL );
4616
4617 // allocate the VM object
4618 if( !type->ugen_info )
4619 {
4620 // check type TODO: make this faster
4621 if( type->allocator )
4622 object = type->allocator( vm, shred, Chuck_DL_Api::instance() );
4623 else if( isa( type, vm->env()->ckt_fileio ) ) object = new Chuck_IO_File( vm );
4624 else if( isa( type, vm->env()->ckt_event ) ) object = new Chuck_Event;
4625 else if( isa( type, vm->env()->ckt_string ) ) object = new Chuck_String;
4626 // TODO: is this ok?
4627 else if( isa( type, vm->env()->ckt_shred ) )
4628 {
4629 // instantiate shred
4630 Chuck_VM_Shred * newShred = new Chuck_VM_Shred();
4631 // set vm_ref
4632 newShred->vm_ref = vm;
4633 // copy to object reference (in case of error, object will be deleted)
4634 object = newShred;
4635
4636 // get stack size hints | 1.5.1.5
4637 t_CKINT mems = shred ? shred->childGetMemSize() : 0;
4638 t_CKINT regs = shred ? shred->childGetRegSize() : 0;
4639 // initialize shred | 1.5.1.5 (ge) added, along with child mem and reg stack size hints
4640 if( !newShred->initialize( NULL, mems, regs ) ) goto error;
4641 }
4642 // 1.5.0.0 (ge) added -- here my feeble brain starts leaking out of my eyeballs
4643 else if( isa( type, vm->env()->ckt_class ) ) object = new Chuck_Type( vm->env(), te_class, type->base_name, type, type->size );
4644 // TODO: is this ok?
4645 else object = new Chuck_Object;
4646 }
4647 else
4648 {
4649 // ugen vs. uana
4650 if( type->ugen_info->tock != NULL )
4651 {
4652 // uana
4653 object = ugen = uana = new Chuck_UAna;
4654 ugen->alloc_v( vm->shreduler()->m_max_block_size );
4655 }
4656 else
4657 {
4658 object = ugen = new Chuck_UGen;
4659 ugen->alloc_v( vm->shreduler()->m_max_block_size );
4660 }
4661 }
4662
4663 // check to see enough memory
4664 if( !object ) goto out_of_memory;
4665
4666 // check for callback
4667 setShredOrigin = type->cbs_on_instantiate( instance_cbs );
4668
4669 // initialize
4670 if( !initialize_object( object, type, shred, vm, setShredOrigin ) ) goto error;
4671
4672 // check for callback
4673 if( instance_cbs.size() )
4674 {
4675 // loop over callbacks to call
4676 for( t_CKUINT i = 0; i < instance_cbs.size(); i++ )
4677 {
4678 // call it
4679 instance_cbs[i].callback( object, type, shred, vm );
4680 }
4681 }
4682
4683 // return the instantiated object
4684 return object;
4685
4686 out_of_memory:
4687
4688 // we have a problem
4689 EM_exception(
4690 "OutOfMemory: while instantiating object '%s'",
4691 type->c_name() );
4692
4693 error:
4694
4695 // delete
4696 CK_SAFE_DELETE( object );
4697
4698 // return NULL
4699 return NULL;
4700 }
4701
4702
4703
4704
4705 //-----------------------------------------------------------------------------
4706 // name: instantiate_object()
4707 // desc: instantiate a object, push its pointer on reg stack
4708 //-----------------------------------------------------------------------------
4709 inline void instantiate_object( Chuck_VM * vm, Chuck_VM_Shred * shred,
4710 Chuck_Type * type, t_CKBOOL addRef )
4711 {
4712 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4713
4714 // allocate the VM object
4715 Chuck_Object * object = instantiate_and_initialize_object( type, shred );
4716 if( !object ) goto error;
4717
4718 // push the pointer on the operand stack
4719 push_( reg_sp, (t_CKUINT)object );
4720
4721 // add reference? | 1.5.4.3 (ge) added this logic
4722 // for explanation see: Chuck_Instr_Instantiate_Object_Complete::execute()
4723 if( addRef ) object->add_ref();
4724
4725 // call preconstructor
4726 // call_pre_constructor( vm, shred, object, type, stack_offset );
4727
4728 return;
4729
4730 error:
4731
4732 // do something!
4733 shred->is_running = FALSE;
4734 shred->is_done = TRUE;
4735 }
4736
4737
4738
4739
4740 //-----------------------------------------------------------------------------
4741 // name: execute()
4742 // desc: instantiate a ChucK Object (starting step)
4743 //-----------------------------------------------------------------------------
4744 void Chuck_Instr_Instantiate_Object_Start::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4745 {
4746 // instantiate a chuck Object
4747 // 1.5.4.3 (ge) add (temporary) reference that will be decremented when
4748 // instantiation complete; see Chuck_Instr_Instantiate_Object_Complete::execute()
4749 // for explanation
4750 instantiate_object( vm, shred, this->type, TRUE );
4751 }
4752
4753
4754
4755
4756 //-----------------------------------------------------------------------------
4757 // name: params()
4758 // desc: ...
4759 //-----------------------------------------------------------------------------
4760 const char * Chuck_Instr_Instantiate_Object_Start::params() const
4761 {
4762 static char buffer[CK_PRINT_BUF_LENGTH];
4763 snprintf( buffer, CK_PRINT_BUF_LENGTH, "%s", this->type->c_name() );
4764 return buffer;
4765 }
4766
4767
4768
4769
4770 //-----------------------------------------------------------------------------
4771 // name: execute()
4772 // desc: complete the process of instantiating an Object | 1.5.4.3 (ge) added
4773 //-----------------------------------------------------------------------------
4774 void Chuck_Instr_Instantiate_Object_Complete::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4775 {
4776 // operand stack pointer
4777 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4778 // get the object reference (should be top of operand stack)
4779 Chuck_Object * obj = (Chuck_Object *)(*(reg_sp-1));
4780 // leave stack contents unchanged (no push or pop)...
4781
4782 // NOTE: decrement (but NOT release) the temporary reference added by
4783 // Chuck_Instr_Instantiate_Object_Start; this is done so that a constructor
4784 // can work with `this` pointer with refcount > 0 BEFORE the
4785 // instatiated object has a chance to be assigned to a variable (at which
4786 // point it would have a proper reference) | 1.5.4.3 (ge) added
4787 //
4788 // NOTE: we are derementing only because of the possibility of
4789 // the reference going back to 0, but we don't want to delete it at
4790 // this point in case it's about to be assigned to a variable
4791 //
4792 // EXAMPLE: a situation that needs this: a constructor that calls a member
4793 // function that returns itself: the statement cleanup logic could delete
4794 // this object before the object can be assigned to a variable -- to account
4795 // for the above, this mechanism temporarily boosts the instantiating
4796 // object's refcount throughout Object instantiation
4797 obj->dec_ref_no_release();
4798
4799 /*--------------------------------------------------------------------------
4800 | example -- also test/01-Basic/264-instantiation-integrity.ck
4801 ----------------------------------------------------------------------------
4802 // a class definition
4803 public class Foo
4804 {
4805 // a member function that returns `this`
4806 fun Foo getFoo() { return this; }
4807 // a constructor (takes argument for good measure)
4808 fun Foo( vec3 sz )
4809 {
4810 // return ourself -- note this is INSIDE Foo constructor
4811 // which means it's before this instance can be assigned
4812 // to a variable (as below); yet statements likes these
4813 // have a clean-up logic that balances reference counts;
4814 // so the refcount for Foo during instantiation must
4815 // account for this interregnum; FYI this is typically
4816 // handled by temporarily boosting the refcount during
4817 // instantiation so it cannot be deleted from within
4818 // its own constructor!
4819 this.getFoo();
4820 }
4821 }
4822 // instantiate and assignment to variable
4823 Foo foo( @(1,2,3) );
4824 --------------------------------------------------------------------------*/
4825 }
4826
4827
4828
4829
4830 //-----------------------------------------------------------------------------
4831 // name: params()
4832 // desc: ...
4833 //-----------------------------------------------------------------------------
4834 const char * Chuck_Instr_Instantiate_Object_Complete::params() const
4835 {
4836 static char buffer[CK_PRINT_BUF_LENGTH];
4837 snprintf( buffer, CK_PRINT_BUF_LENGTH, "%s", this->type->c_name() );
4838 return buffer;
4839 }
4840
4841
4842
4843
4844 //-----------------------------------------------------------------------------
4845 // name: params()
4846 // desc: text description
4847 //-----------------------------------------------------------------------------
4848 const char * Chuck_Instr_Pre_Ctor_Array_Top::params() const
4849 {
4850 static char buffer[CK_PRINT_BUF_LENGTH];
4851 snprintf( buffer, CK_PRINT_BUF_LENGTH, "val=%ld, type=\"%s\"", (long)m_val, type ? type->c_name() : "[empty]" );
4852 return buffer;
4853 }
4854
4855
4856
4857
4858 //-----------------------------------------------------------------------------
4859 // name: execute()
4860 // desc: object pre construct top
4861 //-----------------------------------------------------------------------------
4862 void Chuck_Instr_Pre_Ctor_Array_Top::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4863 {
4864 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4865
4866 // see if we are done with all elements in the array
4867 if( *(reg_sp-2) >= *(reg_sp-1) )
4868 shred->next_pc = m_val;
4869 else
4870 {
4871 // instantiate
4872 instantiate_object( vm, shred, type, FALSE );
4873 }
4874 }
4875
4876
4877
4878
4879 //-----------------------------------------------------------------------------
4880 // name: execute()
4881 // desc: object pre construct bottom
4882 //-----------------------------------------------------------------------------
4883 void Chuck_Instr_Pre_Ctor_Array_Bottom::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4884 {
4885 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4886
4887 // pop the object
4888 pop_( reg_sp, 1 );
4889
4890 // cast the object
4891 Chuck_Object * obj = (Chuck_Object *)(*(reg_sp));
4892
4893 // assign object
4894 t_CKUINT * array = (t_CKUINT *)(*(reg_sp-3));
4895 // get the object pointer
4896 Chuck_Object ** dest = (Chuck_Object **)(array[*(reg_sp-2)]);
4897 // copy
4898 *dest = obj;
4899 // ref count
4900 obj->add_ref();
4901 // increment the index
4902 (*(reg_sp-2))++; //= (*(reg_sp-2)) + 1;
4903
4904 // goto top
4905 shred->next_pc = m_val;
4906 }
4907
4908
4909
4910
4911 //-----------------------------------------------------------------------------
4912 // name: execute()
4913 // desc: object pre construct post
4914 //-----------------------------------------------------------------------------
4915 void Chuck_Instr_Pre_Ctor_Array_Post::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4916 {
4917 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4918
4919 // pop the array, index, and size
4920 pop_( reg_sp, 3 );
4921
4922 // clean up the array
4923 t_CKUINT * arr = (t_CKUINT *)*reg_sp;
4924 CK_SAFE_DELETE_ARRAY( arr );
4925 }
4926
4927
4928
4929 #pragma mark === Assignment ===
4930
4931
4932 //-----------------------------------------------------------------------------
4933 // name: execute()
4934 // desc: assign primitive (word)
4935 //-----------------------------------------------------------------------------
4936 void Chuck_Instr_Assign_Primitive::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4937 {
4938 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4939
4940 // pop word from reg stack
4941 pop_( reg_sp, 2 );
4942 // copy popped value into mem stack
4943 *((t_CKUINT *)(*(reg_sp+1))) = *reg_sp;
4944
4945 push_( reg_sp, *reg_sp );
4946 }
4947
4948
4949
4950
4951 //-----------------------------------------------------------------------------
4952 // name: execute()
4953 // desc: assign primitive (2 word)
4954 //-----------------------------------------------------------------------------
4955 void Chuck_Instr_Assign_Primitive2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4956 {
4957 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4958
4959 // pop word from reg stack // ISSUE: 64-bit (fixed 1.3.1.0)
4960 pop_( reg_sp, 1 + (sz_FLOAT / sz_UINT) );
4961 // copy popped value into mem stack // ISSUE: 64-bit (fixed 1.3.1.0)
4962 *( (t_CKFLOAT *)(*(reg_sp+(sz_FLOAT/sz_UINT))) ) = *(t_CKFLOAT *)reg_sp;
4963
4964 t_CKFLOAT *& sp_double = (t_CKFLOAT *&)reg_sp;
4965 push_( sp_double, *sp_double );
4966 }
4967
4968
4969
4970
4971 //-----------------------------------------------------------------------------
4972 // name: execute()
4973 // desc: assign primitive (4 word) vec2 complex polar
4974 //-----------------------------------------------------------------------------
4975 void Chuck_Instr_Assign_Primitive4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4976 {
4977 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4978
4979 // pop word from reg stack
4980 pop_( reg_sp, 1 + (sz_VEC2 / sz_UINT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
4981 // copy popped value into mem stack
4982 *( (t_CKVEC2*)(*(reg_sp+(sz_VEC2/sz_UINT))) ) = *(t_CKVEC2 *)reg_sp; // ISSUE: 64-bit (fixed 1.3.1.0)
4983
4984 t_CKVEC2 *& sp_vec2 = (t_CKVEC2 *&)reg_sp;
4985 push_( sp_vec2, *sp_vec2 );
4986 }
4987
4988
4989
4990
4991 //-----------------------------------------------------------------------------
4992 // name: execute()
4993 // desc: assign primitive (vec3), 1.3.5.3
4994 //-----------------------------------------------------------------------------
4995 void Chuck_Instr_Assign_PrimitiveVec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
4996 {
4997 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
4998
4999 // pop word from reg stack
5000 pop_( reg_sp, 1 + (sz_VEC3 / sz_UINT) );
5001 // copy popped value into mem stack
5002 *( (t_CKVEC3*)(*(reg_sp+(sz_VEC3/sz_UINT))) ) = *(t_CKVEC3 *)reg_sp;
5003
5004 t_CKVEC3 *& sp_vec3 = (t_CKVEC3 *&)reg_sp;
5005 push_( sp_vec3, *sp_vec3 );
5006 }
5007
5008
5009
5010
5011 //-----------------------------------------------------------------------------
5012 // name: execute()
5013 // desc: assign primitive (vec4)
5014 //-----------------------------------------------------------------------------
5015 void Chuck_Instr_Assign_PrimitiveVec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5016 {
5017 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5018
5019 // pop word from reg stack
5020 pop_( reg_sp, 1 + (sz_VEC4 / sz_UINT) );
5021 // copy popped value into mem stack
5022 *( (t_CKVEC4*)(*(reg_sp+(sz_VEC4/sz_UINT))) ) = *(t_CKVEC4 *)reg_sp;
5023
5024 t_CKVEC4 *& sp_vec4 = (t_CKVEC4 *&)reg_sp;
5025 push_( sp_vec4, *sp_vec4 );
5026 }
5027
5028
5029
5030
5031 //-----------------------------------------------------------------------------
5032 // name: execute()
5033 // desc: assign object with reference counting and releasing previous reference
5034 //-----------------------------------------------------------------------------
5035 void Chuck_Instr_Assign_Object::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5036 {
5037 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5038 Chuck_VM_Object ** obj = NULL, * done = NULL;
5039
5040 // pop word from reg stack
5041 pop_( reg_sp, 2 );
5042 // the previous reference
5043 obj = (Chuck_VM_Object **)(*(reg_sp+1));
5044 // save the reference (release should come after, in case same object)
5045 done = *obj;
5046 // copy popped value into memory
5047 *obj = (Chuck_VM_Object *)(*(reg_sp));
5048
5049 // hmm if need to debug
5050 // CK_FPRINTF_STDERR( "obj: 0x%08x 0x%08x\n", *obj, done );
5051
5052 // add reference
5053 if( *obj ) (*obj)->add_ref();
5054 // release
5055 if( done ) done->release();
5056
5057 // FYI this instruction expects a variable address in obj
5058 // but ends up pushing *obj (the value pointed to by obj)
5059 // push the reference value to reg stack
5060 push_( reg_sp, (t_CKUINT)*obj );
5061 }
5062
5063
5064
5065
5066 //-----------------------------------------------------------------------------
5067 // name: execute()
5068 // desc: assign string
5069 //-----------------------------------------------------------------------------
5070 void Chuck_Instr_Assign_String::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5071 {
5072 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5073 Chuck_String * lhs = NULL;
5074 Chuck_String ** rhs_ptr = NULL;
5075
5076 // pop word from reg stack
5077 pop_( reg_sp, 2 );
5078 // the previous reference
5079 rhs_ptr = (Chuck_String **)(*(reg_sp+1));
5080 // copy popped value into memory
5081 lhs = (Chuck_String *)(*(reg_sp));
5082 // release any previous reference
5083 if( *rhs_ptr )
5084 {
5085 if( lhs ) (*rhs_ptr)->set( lhs->str() );
5086 else
5087 {
5088 // release reference
5089 (*rhs_ptr)->release();
5090 (*rhs_ptr) = NULL;
5091 }
5092 }
5093 else
5094 {
5095 // if left is not null, yes
5096 if( lhs != NULL )
5097 {
5098 (*rhs_ptr) = (Chuck_String *)instantiate_and_initialize_object( vm->env()->ckt_string, shred );
5099 // add ref
5100 (*rhs_ptr)->add_ref();
5101 (*rhs_ptr)->set( lhs->str() );
5102 }
5103 //EM_error2( 0, "(internal error) somehow the type checker has allowed NULL strings" );
5104 //EM_error2( 0, "we are sorry for the inconvenience but..." );
5105 //EM_error2( 0, "we have to crash now. Thanks." );
5106 //assert( FALSE );
5107 }
5108
5109 // copy
5110 // memcpy( (void *)*(reg_sp+1), *obj, sizeof(t_CKUINT) );
5111 // push the reference value to reg stack
5112 push_( reg_sp, (t_CKUINT)*rhs_ptr );
5113 }
5114
5115
5116 #pragma mark === Reference Counting ===
5117
5118
5119
5120 //-----------------------------------------------------------------------------
5121 // name: execute()
5122 // desc: add one reference on object (added 1.3.0.0)
5123 //-----------------------------------------------------------------------------
5124 void Chuck_Instr_AddRef_Object::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5125 {
5126 // ISSUE: 64-bit?
5127 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
5128 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5129 Chuck_VM_Object * obj = NULL;
5130
5131 // pop word from reg stack
5132 pop_( reg_sp, 1 );
5133 // copy popped value into mem stack
5134 obj = *( (Chuck_VM_Object **)(mem_sp + *(reg_sp)) );
5135 // ge (2012 april): check for NULL (added 1.3.0.0)
5136 if( obj != NULL )
5137 {
5138 // release
5139 obj->add_ref();
5140 }
5141 }
5142
5143
5144
5145
5146 //-----------------------------------------------------------------------------
5147 // name: execute()
5148 // desc: add one reference on object (ge 2012 april | added 1.3.0.0)
5149 //-----------------------------------------------------------------------------
5150 void Chuck_Instr_AddRef_Object2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5151 {
5152 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
5153 Chuck_VM_Object * obj = NULL;
5154
5155 // copy popped value into mem stack
5156 obj = *( (Chuck_VM_Object **)(mem_sp + m_val) );
5157 // check for NULL
5158 if( obj != NULL )
5159 {
5160 // add reference
5161 obj->add_ref();
5162 }
5163 }
5164
5165
5166
5167
5168 //-----------------------------------------------------------------------------
5169 // name: execute()
5170 // desc: ref-count increment the top of the register stack (added 1.3.0.0)
5171 // 1.5.0.0 (ge) additional notes...
5172 // this is used to keep objects referenced while on stack;
5173 // relevant for cases like `return obj;` where obj may need to
5174 // released for the local stack AND need to be kept on the stack
5175 // to be return; in this case, we need this additional add_ref();
5176 // releasing obj from stack is responsibility of the caller
5177 //-----------------------------------------------------------------------------
5178 void Chuck_Instr_Reg_AddRef_Object3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5179 {
5180 // NOTE: this pointer is NOT a reference pointer
5181 t_CKUINT * reg_sp = (t_CKUINT *&)shred->reg->sp;
5182 Chuck_VM_Object * obj = NULL;
5183
5184 // move word without popping
5185 reg_sp--;
5186 // copy popped value into mem stack
5187 obj = *( (Chuck_VM_Object **)(reg_sp) );
5188 // ge (2012 april): check for NULL (added 1.3.0.0)
5189 if( obj != NULL )
5190 {
5191 // add reference
5192 obj->add_ref();
5193 }
5194 }
5195
5196
5197
5198
5199 //-----------------------------------------------------------------------------
5200 // name: execute()
5201 // desc: release one reference on object
5202 //-----------------------------------------------------------------------------
5203 void Chuck_Instr_Release_Object::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5204 {
5205 // ISSUE: 64-bit?
5206 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
5207 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5208 Chuck_VM_Object * obj = NULL;
5209
5210 // pop word from reg stack
5211 pop_( reg_sp, 1 );
5212 // copy popped value into mem stack
5213 obj = *( (Chuck_VM_Object **)(mem_sp + *(reg_sp)) );
5214 // ge (2012 april): check for NULL (added 1.3.0.0)
5215 if( obj != NULL )
5216 {
5217 // release
5218 obj->release();
5219 }
5220 }
5221
5222
5223
5224
5225 //-----------------------------------------------------------------------------
5226 // name: execute()
5227 // desc: release one reference on object (added ge 2012 april | added 1.3.0.0)
5228 //-----------------------------------------------------------------------------
5229 void Chuck_Instr_Release_Object2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5230 {
5231 t_CKBYTE *& mem_sp = (t_CKBYTE *&)shred->mem->sp;
5232 Chuck_VM_Object * obj = NULL;
5233
5234 // copy popped value into mem stack
5235 obj = *( (Chuck_VM_Object **)(mem_sp + m_val) );
5236 // check for NULL
5237 if( obj != NULL )
5238 {
5239 // release
5240 obj->release();
5241 }
5242 }
5243
5244
5245
5246
5247 //-----------------------------------------------------------------------------
5248 // name: execute()
5249 // desc: release object reference + pop from reg stack | 1.5.0.0 (ge) added
5250 // the variant assumes object pointer directly on stack (not offset)
5251 // used to release returned object pointer after a function call;
5252 // FYI the return value is conveyed on the reg stack; this undoes
5253 // the addref before the return; see Chuck_Instr_Reg_AddRef_Object3
5254 //-----------------------------------------------------------------------------
5255 void Chuck_Instr_Release_Object3_Pop_Int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5256 {
5257 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5258 Chuck_VM_Object * obj = NULL;
5259
5260 // pop
5261 pop_( reg_sp, 1 );
5262 // copy popped value into mem stack
5263 obj = *( (Chuck_VM_Object **)(reg_sp) );
5264 // ge (2012 april): check for NULL (added 1.3.0.0)
5265 if( obj != NULL )
5266 {
5267 // release
5268 obj->release();
5269 }
5270 }
5271
5272
5273
5274
5275 //-----------------------------------------------------------------------------
5276 // name: execute()
5277 // desc: release object reference from reg stack | 1.5.0.0 (ge) added
5278 // the variant assumes object pointer directly on stack (not offset)
5279 // used to release objects on function arguments, specifically for
5280 // built-in / imported (chugins) functions; code-defined functions
5281 // do their own argument cleanup
5282 //-----------------------------------------------------------------------------
5283 void Chuck_Instr_Release_Object4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5284 {
5285 // NOTE: this pointer is NOT a reference pointer
5286 t_CKBYTE * mem_sp = (t_CKBYTE *&)shred->mem->sp;
5287 Chuck_VM_Object * obj = NULL;
5288
5289 // move it
5290 mem_sp += m_val;
5291 // copy popped value into mem stack
5292 obj = *( (Chuck_VM_Object **)(mem_sp) );
5293 if( obj != NULL )
5294 {
5295 // release
5296 obj->release();
5297 }
5298 }
5299
5300
5301
5302
5303 #pragma mark === Function Calls ===
5304
5305
5306
5307 //-----------------------------------------------------------------------------
5308 // name: execute()
5309 // desc: converts, in place on operand stack, a Func to its Code (instructions)
5310 //-----------------------------------------------------------------------------
5311 void Chuck_Instr_Func_To_Code::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5312 {
5313 // ISSUE: 64-bit?
5314 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5315
5316 // get func
5317 Chuck_Func * func = (Chuck_Func *)*(reg_sp-1);
5318 // make sure
5319 assert( func != NULL );
5320 // code
5321 *(reg_sp-1) = (t_CKUINT)func->code;
5322 }
5323
5324
5325
5326
5327 //-----------------------------------------------------------------------------
5328 // name: func_release_args() | 1.5.0.0 (ge) added
5329 // desc: helper function to release objects an function arg list
5330 // input: VM, args list, mem stack pointer
5331 // context: this is designed for builtin/imported functions
5332 // functions defined in chuck code do their own arg list cleanup
5333 //-----------------------------------------------------------------------------
5334 t_CKBOOL func_release_args( Chuck_VM * vm, a_Arg_List args, t_CKBYTE * mem_sp )
5335 {
5336 // keep track of offset
5337 t_CKUINT offset = 0;
5338 // the type in question
5339 Chuck_Type * type = NULL;
5340 // object pointer
5341 Chuck_VM_Object * object = NULL;
5342
5343 // loop over
5344 while( args )
5345 {
5346 // get the type
5347 type = args->type;
5348 // check we have one!
5349 if( type != NULL )
5350 {
5351 // is an object?
5352 if( isobj( vm->env(), args->type) )
5353 {
5354 // get the object pointer
5355 object = (Chuck_VM_Object *)(*(t_CKUINT *)(mem_sp+offset));
5356 // check
5357 if( object != NULL )
5358 {
5359 // release it
5360 object->release();
5361 }
5362 }
5363
5364 // advance offset
5365 offset += args->type->size;
5366 }
5367
5368 // go
5369 args = args->next;
5370 }
5371
5372 return TRUE;
5373 }
5374
5375
5376
5377
5378 //-----------------------------------------------------------------------------
5379 // for printing
5380 //-----------------------------------------------------------------------------
5381 const char * Chuck_Instr_Func_Call::params() const
5382 {
5383 static char buffer[CK_PRINT_BUF_LENGTH];
5384 snprintf( buffer, CK_PRINT_BUF_LENGTH, "convention='%s'", m_arg_convention == CK_FUNC_CALL_THIS_IN_BACK ? "this:back" : "this:front" );
5385 return buffer;
5386 }
5387
5388
5389
5390
5391 //-----------------------------------------------------------------------------
5392 // name: execute()
5393 // desc: general function call for in-language defined functions
5394 //-----------------------------------------------------------------------------
5395 void Chuck_Instr_Func_Call::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5396 {
5397 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
5398 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5399
5400 // pop words
5401 pop_( reg_sp, 2 );
5402 // get the function to be called as code
5403 Chuck_VM_Code * func = (Chuck_VM_Code *)*reg_sp;
5404 // get the local stack depth - caller local variables
5405 t_CKUINT local_depth = *(reg_sp+1);
5406 // convert to number of int's (was: 4-byte words), extra partial word counts as additional word
5407 local_depth = ( local_depth / sz_INT ) + ( local_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5408 // get the stack depth of the callee function args
5409 t_CKUINT stack_depth_ints = ( func->stack_depth / sz_INT ) + ( func->stack_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5410 // get the previous stack depth - caller function args
5411 t_CKUINT prev_stack = ( *(mem_sp-1) / sz_INT ) + ( *(mem_sp-1) & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5412
5413 // jump the sp
5414 mem_sp += prev_stack + local_depth;
5415 // push the prev stack
5416 push_( mem_sp, prev_stack + local_depth );
5417 // push the current function
5418 push_( mem_sp, (t_CKUINT)shred->code );
5419 // push the pc
5420 push_( mem_sp, (t_CKUINT)(shred->pc + 1) );
5421 // push the stack depth
5422 push_( mem_sp, stack_depth_ints );
5423 // set the pc to 0
5424 shred->next_pc = 0;
5425 // set the code
5426 shred->code = func;
5427 // set the instruction to the function instruction
5428 shred->instr = func->instr;
5429
5430 // detect overflow
5431 if( overflow_( shred->mem ) ) goto error_overflow;
5432
5433 // if there are arguments to be passed
5434 if( stack_depth_ints )
5435 {
5436 // pop the arguments, by number of words
5437 pop_( reg_sp, stack_depth_ints );
5438
5439 // make copies (but without modifying actual stack pointers)
5440 t_CKUINT * mem_sp2 = (t_CKUINT *)mem_sp;
5441 t_CKUINT * reg_sp2 = (t_CKUINT *)reg_sp;
5442
5443 // detect would-be overflow | 1.5.1.5 (ge) added
5444 if( would_overflow_( mem_sp2+stack_depth_ints, shred->mem ) ) goto error_overflow;
5445
5446 // need this
5447 if( func->need_this )
5448 {
5449 // check convention | 1.5.2.0 (ge) added
5450 if( m_arg_convention == CK_FUNC_CALL_THIS_IN_BACK )
5451 {
5452 // copy this from end of arguments to the front
5453 *mem_sp2++ = *(reg_sp2 + stack_depth_ints - 1);
5454 } else {
5455 // copy this from start of arguments
5456 *mem_sp2++ = *reg_sp2++;
5457 }
5458 // one less word to copy
5459 stack_depth_ints--;
5460 }
5461 // static inside class | 1.4.1.0 (ge) added
5462 else if( func->is_static )
5463 {
5464 // check convention | 1.5.2.0 (ge) added
5465 if( m_arg_convention == CK_FUNC_CALL_THIS_IN_BACK )
5466 {
5467 // copy type from end of arguments to the front
5468 *mem_sp2++ = *(reg_sp2 + stack_depth_ints - 1);
5469 } else {
5470 // copy type from start of arguments
5471 *mem_sp2++ = *reg_sp2++;
5472 }
5473 // one less word to copy
5474 stack_depth_ints--;
5475 }
5476
5477 // push the arguments
5478 for( t_CKUINT i = 0; i < stack_depth_ints; i++ )
5479 *mem_sp2++ = *reg_sp2++;
5480 }
5481
5482 return;
5483
5484 error_overflow:
5485
5486 ck_handle_overflow( shred, vm, "too many nested/recursive function calls" );
5487 }
5488
5489
5490
5491
5492 //-----------------------------------------------------------------------------
5493 // for printing
5494 //-----------------------------------------------------------------------------
5495 const char * Chuck_Instr_Func_Call_Member::params() const
5496 {
5497 static char buffer[CK_PRINT_BUF_LENGTH];
5498 snprintf( buffer, CK_PRINT_BUF_LENGTH, "%s, %s, %s",
5499 m_func_ref ? m_func_ref->signature(FALSE,FALSE).c_str() : "[null]",
5500 m_arg_convention == CK_FUNC_CALL_THIS_IN_BACK ? "this:back" : "this:front",
5501 m_special_primitive_cleanup_this ? "transmute:yes" : "transmute:no" );
5502 return buffer;
5503 }
5504
5505
5506
5507
5508 //-----------------------------------------------------------------------------
5509 // name: execute()
5510 // desc: imported member function call with return
5511 //-----------------------------------------------------------------------------
5512 void Chuck_Instr_Func_Call_Member::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5513 {
5514 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
5515 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5516 Chuck_DL_Return retval;
5517 // 1.5.4.2 (ge) added; #special-primitive-member-func-from-literal
5518 t_CKUINT ckTHIS = 0;
5519
5520 // pop word
5521 pop_( reg_sp, 2 );
5522 // get the function to be called as code
5523 Chuck_VM_Code * func = (Chuck_VM_Code *)*reg_sp;
5524 // get the local stack depth - caller local variables
5525 t_CKUINT local_depth = *(reg_sp+1);
5526 // convert to number of int's (was: 4-byte words), extra partial word counts as additional word
5527 local_depth = ( local_depth / sz_INT ) + ( local_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5528 // get the stack depth of the callee function args
5529 t_CKUINT stack_depth = ( func->stack_depth / sz_INT ) + ( func->stack_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5530 // UNUSED: get the previous stack depth - caller function args
5531 // UNUSED: t_CKUINT prev_stack = ( *(mem_sp-1) >> 2 ) + ( *(mem_sp-1) & 0x3 ? 1 : 0 );
5532 // the amount to push in 4-byte words
5533 t_CKUINT push = local_depth;
5534 // push the mem stack passed the current function variables and arguments
5535 mem_sp += push;
5536
5537 // detect overflow
5538 if( overflow_( shred->mem ) ) goto error_overflow;
5539
5540 // pass args
5541 if( stack_depth )
5542 {
5543 // pop the arguments for pass to callee function
5544 reg_sp -= stack_depth;
5545
5546 // make copies (without modifying actual stack pointers)
5547 t_CKUINT * reg_sp2 = reg_sp;
5548 t_CKUINT * mem_sp2 = mem_sp;
5549
5550 // detect would-be overflow | 1.5.1.5 (ge) added
5551 if( would_overflow_( mem_sp2+stack_depth, shred->mem ) ) goto error_overflow;
5552
5553 // need this
5554 if( func->need_this )
5555 {
5556 // check convention | 1.5.2.0 (ge) added
5557 if( m_arg_convention == CK_FUNC_CALL_THIS_IN_BACK )
5558 {
5559 // copy this from end of arguments to the front
5560 *mem_sp2++ = *(reg_sp2 + stack_depth - 1);
5561 } else {
5562 // copy this from start of arguments
5563 *mem_sp2++ = *reg_sp2++;
5564 }
5565 // one less word to copy
5566 stack_depth--;
5567 }
5568 // copy to args
5569 for( t_CKUINT i = 0; i < stack_depth; i++ )
5570 *mem_sp2++ = *reg_sp2++;
5571 }
5572
5573 // remember THIS #special-primitive-member-func-from-literal
5574 ckTHIS = (t_CKUINT)(*mem_sp);
5575
5576 // check the function pointer kind: ctor or mfun?
5577 if( func->native_func_kind == ae_fp_ctor ) // ctor
5578 {
5579 // cast to right type
5580 f_ctor f = (f_ctor)func->native_func;
5581 // call (added 1.3.0.0 -- Chuck_DL_Api::instance())
5582 f( (Chuck_Object *)(*mem_sp), mem_sp + 1, vm, shred, Chuck_DL_Api::instance() );
5583 }
5584 else // mfun
5585 {
5586 // make sure is mfun
5587 assert( func->native_func_kind == ae_fp_mfun );
5588 // cast to right type
5589 f_mfun f = (f_mfun)func->native_func;
5590 // call the function (added 1.3.0.0 -- Chuck_DL_Api::instance())
5591 f( (Chuck_Object *)(*mem_sp), mem_sp + 1, &retval, vm, shred, Chuck_DL_Api::instance() );
5592 }
5593
5594 // push the return
5595 // 1.3.1.0: check type to use kind instead of size
5596 if( m_val == kindof_INT ) // ISSUE: 64-bit (fixed: 1.3.1.0)
5597 {
5598 // push the return args
5599 push_( reg_sp, retval.v_uint );
5600
5601 // 1.5.0.0 (ge) | added -- ensure ref count
5602 if( m_func_ref && isobj(vm->env(), m_func_ref->def()->ret_type) )
5603 {
5604 // get return value as object reference
5605 Chuck_VM_Object * obj = (Chuck_VM_Object *)retval.v_uint;
5606 if( obj )
5607 {
5608 // always add reference to returned objects (should release outside)
5609 obj->add_ref();
5610 }
5611 }
5612 }
5613 else if( m_val == kindof_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
5614 {
5615 // push the return args
5616 t_CKFLOAT *& sp_double = (t_CKFLOAT *&)reg_sp;
5617 push_( sp_double, retval.v_float );
5618 }
5619 else if( m_val == kindof_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) | 1.5.1.7 (ge) complex -> vec2
5620 {
5621 // push the return args
5622 t_CKVEC2 *& sp_vec2 = (t_CKVEC2 *&)reg_sp;
5623 // in this context vec2 = complex = polar
5624 push_( sp_vec2, retval.v_vec2 );
5625 }
5626 else if( m_val == kindof_VEC3 ) // 1.3.5.3
5627 {
5628 // push the return args
5629 t_CKVEC3 *& sp_vec3 = (t_CKVEC3 *&)reg_sp;
5630 push_( sp_vec3, retval.v_vec3 );
5631 }
5632 else if( m_val == kindof_VEC4 ) // 1.3.5.3
5633 {
5634 // push the return args
5635 t_CKVEC4 *& sp_vec4 = (t_CKVEC4 *&)reg_sp;
5636 push_( sp_vec4, retval.v_vec4 );
5637 }
5638 else if( m_val == kindof_VOID ) { }
5639 else assert( FALSE );
5640
5641 // if we have a func def | 1.5.0.0 (ge) added
5642 if( m_func_ref != NULL )
5643 {
5644 // cleanup / release objects on the arg list
5645 // context: this should be done here for builtin/import functions
5646 // user-defined functions do their own arg list cleanup
5647 // note: this is done after pushing the return value, in case the
5648 // return value is one of these args being released;
5649 // e.g., functions that pass through args;
5650 // e.g., string Sndbuf.read(string)
5651 func_release_args( vm, m_func_ref->def()->arg_list, (t_CKBYTE *)(mem_sp+1) );
5652 }
5653
5654 // check if we need to do special-primitive cleanup | 1.5.4.2 (ge) added
5655 // this is for special primitives that have "member" functions
5656 // e.g., vec2/3/4 -- specifically, this supports calling a "member" function
5657 // from a literal value (i.e., not a variable) -- e.g., @(1,0).magnitude()
5658 // NOTE: this approach is cursed because it adds more special case treatment
5659 // to existing special cases for special primitives; if there is any silver
5660 // lining, it might be that this logic is isolated and contained
5661 // (see #special-primitive-member-func-from-literal for related code)
5662 if( m_special_primitive_cleanup_this ) special_primitive_cleanup( (t_CKBYTE *)ckTHIS );
5663
5664 // pop the stack pointer
5665 mem_sp -= push;
5666
5667 return;
5668
5669 error_overflow:
5670
5671 ck_handle_overflow( shred, vm, "too many nested/recursive function calls" );
5672 }
5673
5674
5675
5676
5677 //-----------------------------------------------------------------------------
5678 // for printing
5679 //-----------------------------------------------------------------------------
5680 const char * Chuck_Instr_Func_Call_Static::params() const
5681 {
5682 static char buffer[CK_PRINT_BUF_LENGTH];
5683 snprintf( buffer, CK_PRINT_BUF_LENGTH, "%s, %s",
5684 m_func_ref ? m_func_ref->signature(FALSE,FALSE).c_str() : "[null]",
5685 m_arg_convention == CK_FUNC_CALL_THIS_IN_BACK ? "this:back" : "this:front" );
5686 return buffer;
5687 }
5688
5689
5690
5691
5692 //-----------------------------------------------------------------------------
5693 // name: execute()
5694 // desc: imported static function call with return
5695 //-----------------------------------------------------------------------------
5696 void Chuck_Instr_Func_Call_Static::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5697 {
5698 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
5699 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5700 Chuck_DL_Return retval;
5701
5702 // pop code and local depth
5703 pop_( reg_sp, 2 );
5704 // get the function to be called as code
5705 Chuck_VM_Code * func = (Chuck_VM_Code *)*(reg_sp);
5706 // get the function to be called
5707 f_sfun f = (f_sfun)func->native_func;
5708 // verify | 1.5.2.0
5709 assert( func->native_func_kind == ae_fp_sfun );
5710 // get the local stack depth - caller local variables
5711 t_CKUINT local_depth = *(reg_sp+1);
5712 // convert to number of int's (was: 4-byte words), extra partial word counts as additional word
5713 local_depth = ( local_depth / sz_INT ) + ( local_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5714 // get the stack depth of the callee function args
5715 t_CKUINT stack_depth = ( func->stack_depth / sz_INT ) + ( func->stack_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5716 // UNUSED: get the previous stack depth - caller function args
5717 // UNUSED: t_CKUINT prev_stack = ( *(mem_sp-1) >> 2 ) + ( *(mem_sp-1) & 0x3 ? 1 : 0 );
5718 // the amount to push in 4-byte words
5719 t_CKUINT push = local_depth;
5720 // push the mem stack past the current function variables and arguments
5721 mem_sp += push;
5722
5723 // detect overflow
5724 if( overflow_( shred->mem ) ) goto error_overflow;
5725
5726 // pass args
5727 if( stack_depth )
5728 {
5729 // pop the arguments for pass to callee function
5730 reg_sp -= stack_depth;
5731
5732 // make copies (without modifying actual stack pointers)
5733 t_CKUINT * reg_sp2 = reg_sp;
5734 t_CKUINT * mem_sp2 = mem_sp;
5735
5736 // detect would-be overflow | 1.5.1.5 (ge) added
5737 if( would_overflow_( mem_sp2+stack_depth, shred->mem ) ) goto error_overflow;
5738
5739 // need type
5740 if( func->is_static )
5741 {
5742 // check convention | 1.5.2.0 (ge) added
5743 if( m_arg_convention == CK_FUNC_CALL_THIS_IN_BACK )
5744 {
5745 // copy this from end of arguments to the front
5746 *mem_sp2++ = *(reg_sp2 + stack_depth - 1);
5747 } else {
5748 // copy this from start of arguments
5749 *mem_sp2++ = *reg_sp2++;
5750 }
5751 // one less word to copy
5752 stack_depth--;
5753 }
5754 // copy to args
5755 for( t_CKUINT i = 0; i < stack_depth; i++ )
5756 *mem_sp2++ = *reg_sp2++;
5757 }
5758
5759 // call the function
5760 // (added 1.3.0.0 -- Chuck_DL_Api::instance())
5761 // (added 1.4.1.0 -- base_type)
5762 f( (Chuck_Type *)(*mem_sp), mem_sp+1, &retval, vm, shred, Chuck_DL_Api::instance() );
5763
5764 // push the return
5765 // 1.3.1.0: check type to use kind instead of size
5766 if( m_val == kindof_INT ) // ISSUE: 64-bit (fixed 1.3.1.0)
5767 {
5768 // push the return args
5769 push_( reg_sp, retval.v_uint );
5770
5771 // 1.5.0.0 (ge) | added -- ensure ref count
5772 if( m_func_ref && isobj(vm->env(), m_func_ref->def()->ret_type) )
5773 {
5774 // get return value as object reference
5775 Chuck_VM_Object * obj = (Chuck_VM_Object *)retval.v_uint;
5776 if( obj )
5777 {
5778 // always add reference to returned objects (should release outside)
5779 obj->add_ref();
5780 }
5781 }
5782 }
5783 else if( m_val == kindof_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
5784 {
5785 // push the return args
5786 t_CKFLOAT *& sp_double = (t_CKFLOAT *&)reg_sp;
5787 push_( sp_double, retval.v_float );
5788 }
5789 else if( m_val == kindof_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) | 1.5.1.7 (ge) complex -> vec2
5790 {
5791 // push the return args
5792 t_CKVEC2 *& sp_vec2 = (t_CKVEC2 *&)reg_sp;
5793 // in this context vec2 = complex = polar
5794 push_( sp_vec2, retval.v_vec2 );
5795 }
5796 else if( m_val == kindof_VEC3 ) // 1.3.5.3
5797 {
5798 // push the return args
5799 t_CKVEC3 *& sp_vec3 = (t_CKVEC3 *&)reg_sp;
5800 push_( sp_vec3, retval.v_vec3 );
5801 }
5802 else if( m_val == kindof_VEC4 ) // 1.3.5.3
5803 {
5804 // push the return args
5805 t_CKVEC4 *& sp_vec4 = (t_CKVEC4 *&)reg_sp;
5806 push_( sp_vec4, retval.v_vec4 );
5807 }
5808 else if( m_val == kindof_VOID ) { }
5809 else assert( FALSE );
5810
5811 // if we have a func def | 1.5.0.0 (ge) added
5812 if( m_func_ref != NULL )
5813 {
5814 // cleanup / release objects on the arg list
5815 // context: this should be done here for builtin/import functions
5816 // user-defined functions do their own arg list cleanup
5817 // note: this is done after pushing the return value, in case the
5818 // return value is one of these args being released;
5819 // e.g., functions that pass through args;
5820 // e.g., string Sndbuf.read(string)
5821 func_release_args( vm, m_func_ref->def()->arg_list, (t_CKBYTE *)(mem_sp+1) );
5822 }
5823
5824 // pop the stack pointer
5825 mem_sp -= push;
5826
5827 return;
5828
5829 error_overflow:
5830
5831 ck_handle_overflow( shred, vm, "too many nested/recursive function calls" );
5832 }
5833
5834
5835
5836
5837 //-----------------------------------------------------------------------------
5838 // for printing
5839 //-----------------------------------------------------------------------------
5840 const char * Chuck_Instr_Func_Call_Global::params() const
5841 {
5842 static char buffer[CK_PRINT_BUF_LENGTH];
5843 snprintf( buffer, CK_PRINT_BUF_LENGTH, "%s",
5844 m_func_ref ? m_func_ref->signature(FALSE,FALSE).c_str() : "[null]" );
5845 return buffer;
5846 }
5847
5848
5849
5850
5851 //-----------------------------------------------------------------------------
5852 // name: execute() | 1.5.1.5
5853 // desc: imported global function call with return
5854 //-----------------------------------------------------------------------------
5855 void Chuck_Instr_Func_Call_Global::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5856 {
5857 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
5858 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5859 Chuck_DL_Return retval;
5860
5861 // pop code and local depth
5862 pop_( reg_sp, 2 );
5863 // get the function to be called as code
5864 Chuck_VM_Code * func = (Chuck_VM_Code *)*(reg_sp);
5865 // get the function to be called
5866 f_gfun f = (f_gfun)func->native_func;
5867 // verify | 1.5.2.0
5868 assert( func->native_func_kind == ae_fp_gfun );
5869 // get the local stack depth - caller local variables
5870 t_CKUINT local_depth = *(reg_sp+1);
5871 // convert to number of int's (was: 4-byte words), extra partial word counts as additional word
5872 local_depth = ( local_depth / sz_INT ) + ( local_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5873 // get the stack depth of the callee function args
5874 t_CKUINT stack_depth = ( func->stack_depth / sz_INT ) + ( func->stack_depth & 0x3 ? 1 : 0 ); // ISSUE: 64-bit (fixed 1.3.1.0)
5875 // UNUSED: get the previous stack depth - caller function args
5876 // UNUSED: t_CKUINT prev_stack = ( *(mem_sp-1) >> 2 ) + ( *(mem_sp-1) & 0x3 ? 1 : 0 );
5877 // the amount to push in 4-byte words
5878 t_CKUINT push = local_depth;
5879 // push the mem stack past the current function variables and arguments
5880 mem_sp += push;
5881
5882 // detect overflow
5883 if( overflow_( shred->mem ) ) goto error_overflow;
5884
5885 // pass args
5886 if( stack_depth )
5887 {
5888 // pop the arguments for pass to callee function
5889 reg_sp -= stack_depth;
5890
5891 // make copies (without modifying actual stack pointers)
5892 t_CKUINT * reg_sp2 = reg_sp;
5893 t_CKUINT * mem_sp2 = mem_sp;
5894
5895 // detect would-be overflow | 1.5.1.5 (ge) added
5896 if( would_overflow_( mem_sp2+stack_depth, shred->mem ) ) goto error_overflow;
5897
5898 // copy to args
5899 for( t_CKUINT i = 0; i < stack_depth; i++ )
5900 *mem_sp2++ = *reg_sp2++;
5901 }
5902
5903 // call the function
5904 f( mem_sp, &retval, vm, shred, Chuck_DL_Api::instance() );
5905
5906 // push the return
5907 // 1.3.1.0: check type to use kind instead of size
5908 if( m_val == kindof_INT ) // ISSUE: 64-bit (fixed 1.3.1.0)
5909 {
5910 // push the return args
5911 push_( reg_sp, retval.v_uint );
5912
5913 // 1.5.0.0 (ge) | added -- ensure ref count
5914 if( m_func_ref && isobj(vm->env(), m_func_ref->def()->ret_type) )
5915 {
5916 // get return value as object reference
5917 Chuck_VM_Object * obj = (Chuck_VM_Object *)retval.v_uint;
5918 if( obj )
5919 {
5920 // always add reference to returned objects (should release outside)
5921 obj->add_ref();
5922 }
5923 }
5924 }
5925 else if( m_val == kindof_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
5926 {
5927 // push the return args
5928 t_CKFLOAT *& sp_double = (t_CKFLOAT *&)reg_sp;
5929 push_( sp_double, retval.v_float );
5930 }
5931 else if( m_val == kindof_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) | 1.5.1.7 (ge) complex -> vec2
5932 {
5933 // push the return args
5934 t_CKVEC2 *& sp_vec2 = (t_CKVEC2 *&)reg_sp;
5935 // in this context vec2 = complex = polar
5936 push_( sp_vec2, retval.v_vec2 );
5937 }
5938 else if( m_val == kindof_VEC3 ) // 1.3.5.3
5939 {
5940 // push the return args
5941 t_CKVEC3 *& sp_vec3 = (t_CKVEC3 *&)reg_sp;
5942 push_( sp_vec3, retval.v_vec3 );
5943 }
5944 else if( m_val == kindof_VEC4 ) // 1.3.5.3
5945 {
5946 // push the return args
5947 t_CKVEC4 *& sp_vec4 = (t_CKVEC4 *&)reg_sp;
5948 push_( sp_vec4, retval.v_vec4 );
5949 }
5950 else if( m_val == kindof_VOID ) { }
5951 else assert( FALSE );
5952
5953 // if we have a func def | 1.5.0.0 (ge) added
5954 if( m_func_ref != NULL )
5955 {
5956 // cleanup / release objects on the arg list
5957 // context: this should be done here for builtin/import functions
5958 // user-defined functions do their own arg list cleanup
5959 // note: this is done after pushing the return value, in case the
5960 // return value is one of these args being released;
5961 // e.g., functions that pass through args;
5962 // e.g., string Sndbuf.read(string)
5963 func_release_args( vm, m_func_ref->def()->arg_list, (t_CKBYTE *)(mem_sp) );
5964 }
5965
5966 // pop the stack pointer
5967 mem_sp -= push;
5968
5969 return;
5970
5971 error_overflow:
5972
5973 ck_handle_overflow( shred, vm, "too many nested/recursive function calls" );
5974 }
5975
5976
5977
5978
5979 //-----------------------------------------------------------------------------
5980 // name: execute()
5981 // desc: return from a function
5982 //-----------------------------------------------------------------------------
5983 void Chuck_Instr_Func_Return::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
5984 {
5985 t_CKUINT *& mem_sp = (t_CKUINT *&)shred->mem->sp;
5986 // UNUSED: t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
5987
5988 // pop pc
5989 pop_( mem_sp, 2 );
5990 shred->next_pc = *mem_sp;
5991 // pop the code
5992 pop_( mem_sp, 1 );
5993 Chuck_VM_Code * func = (Chuck_VM_Code *)*mem_sp;
5994 // pop the prev_stack
5995 pop_( mem_sp, 1 );
5996 // jump the prev stack
5997 mem_sp -= *(mem_sp);
5998
5999 // set the shred
6000 shred->code = func;
6001 shred->instr = func->instr;
6002 }
6003
6004
6005
6006
6007 //-----------------------------------------------------------------------------
6008 // name: Chuck_Instr_Stmt_Start()
6009 // desc: constructor
6010 //-----------------------------------------------------------------------------
6011 Chuck_Instr_Stmt_Start::Chuck_Instr_Stmt_Start( t_CKUINT numObjReleases )
6012 {
6013 m_nextOffset = 0;
6014 m_numObjReleases = numObjReleases;
6015 m_stackLevel = 0;
6016 }
6017
6018
6019
6020
6021 //-----------------------------------------------------------------------------
6022 // name: ~Chuck_Instr_Stmt_Start()
6023 // desc: destructor
6024 //-----------------------------------------------------------------------------
6025 Chuck_Instr_Stmt_Start::~Chuck_Instr_Stmt_Start()
6026 {
6027 // drain stack
6028 while( m_stack.size() )
6029 {
6030 CK_SAFE_DELETE_ARRAY( m_stack.back() );
6031 m_stack.pop_back();
6032 }
6033 }
6034
6035
6036
6037
6038 //-----------------------------------------------------------------------------
6039 // name: params()
6040 // desc: for instruction dumps
6041 //-----------------------------------------------------------------------------
6042 // for printing
6043 const char * Chuck_Instr_Stmt_Start::params() const
6044 {
6045 static char buffer[CK_PRINT_BUF_LENGTH];
6046 snprintf( buffer, CK_PRINT_BUF_LENGTH, "numObjReleases=%lu this=%p", (unsigned long)m_numObjReleases, this );
6047 return buffer;
6048 }
6049
6050
6051
6052
6053 //-----------------------------------------------------------------------------
6054 // name: execute()
6055 // desc: executed at the start of a statement (see header for details)
6056 //-----------------------------------------------------------------------------
6057 void Chuck_Instr_Stmt_Start::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6058 {
6059 // if nothing to push, no op
6060 if( !m_numObjReleases ) return;
6061
6062 // push level
6063 m_stackLevel++;
6064
6065 // see if using base cache
6066 if( m_stackLevel == 1 && m_stack.size() == 1 ) return;
6067
6068 // make new region
6069 t_CKUINT * region = new t_CKUINT[m_numObjReleases];
6070 // zero out region
6071 for( t_CKUINT i = 0; i < m_numObjReleases; i++ )
6072 {
6073 // zero out
6074 region[i] = 0;
6075 }
6076 // push onto stack
6077 m_stack.push_back( region );
6078 }
6079
6080
6081
6082
6083 //-----------------------------------------------------------------------------
6084 // name: nextOffset()
6085 // desc: returns next offset on success; 0 if we have exceeded numObjeReleases
6086 //-----------------------------------------------------------------------------
6087 t_CKBOOL Chuck_Instr_Stmt_Start::nextOffset( t_CKUINT & offset )
6088 {
6089 // clear offset
6090 offset = 0;
6091 // check
6092 if( m_nextOffset >= m_numObjReleases )
6093 {
6094 EM_exception(
6095 "(internal error) out of bounds in Stmt_Start.nextIndex(): nextOffset == %lu",
6096 m_nextOffset );
6097
6098 // return
6099 return FALSE;
6100 }
6101
6102 // copy into return variable, then increment
6103 offset = m_nextOffset++;
6104 // return
6105 return TRUE;
6106 }
6107
6108
6109
6110
6111 //-----------------------------------------------------------------------------
6112 // name: setObject()
6113 // desc: set object into data region of objects to release by stmt's end
6114 //-----------------------------------------------------------------------------
6115 t_CKBOOL Chuck_Instr_Stmt_Start::setObject( Chuck_VM_Object * object, t_CKUINT offset )
6116 {
6117 // check
6118 if( m_stackLevel == 0 || m_stack.size() == 0 )
6119 {
6120 EM_exception(
6121 "(internal error) region stack inconsistency in Stmt_Start.setObject(): level=%lu size=%lu",
6122 m_stackLevel, (t_CKUINT)m_stack.size() );
6123 // return
6124 return FALSE;
6125 }
6126
6127 // check
6128 if( offset >= m_numObjReleases )
6129 {
6130 EM_exception(
6131 "(internal error) offset out of bounds in Stmt_Start.setObject(): offset == %lu",
6132 offset );
6133 // return
6134 return FALSE;
6135 }
6136
6137 // region pointer
6138 t_CKUINT * region = m_stack.back();
6139 // pointer arithmetic
6140 t_CKUINT * pInt = region + offset;
6141
6142 // release if not NULL; what was previously there is no-longer accessible
6143 // NOTE this could happen in the case of a loop:
6144 // e.g., while( foo() ) { ... } // where foo() returns an object
6145 Chuck_VM_Object * outgoing = (Chuck_VM_Object *)(*pInt);
6146 CK_SAFE_RELEASE( outgoing );
6147
6148 // copy incoming object pointer
6149 *pInt = (t_CKUINT)object;
6150 // done
6151 return TRUE;
6152 }
6153
6154
6155
6156
6157 //-----------------------------------------------------------------------------
6158 // name: cleanupRefs()
6159 // desc: clean up references
6160 //-----------------------------------------------------------------------------
6161 t_CKBOOL Chuck_Instr_Stmt_Start::cleanupRefs( Chuck_VM_Shred * shred )
6162 {
6163 // if nothing to push, no op
6164 if( !m_numObjReleases ) return TRUE;
6165
6166 // check
6167 if( m_stackLevel == 0 || m_stack.size() == 0 )
6168 {
6169 EM_exception(
6170 "(internal error) region stack inconsistency in Stmt_Start.cleanupRefs(): level=%lu size=%lu on shred[id=%lu:%s]",
6171 m_stackLevel, (t_CKUINT)m_stack.size(), shred->xid, shred->name.c_str() );
6172 // return
6173 return FALSE;
6174 }
6175
6176 // cast pointer to data region as Object pointers
6177 t_CKUINT * pInt = m_stack.back();
6178
6179 // make room for all the object references to release
6180 for( t_CKUINT i = 0; i < m_numObjReleases; i++ )
6181 {
6182 // object pointer
6183 Chuck_VM_Object * object = (Chuck_VM_Object *)(*pInt);
6184 // release (could be NULL)
6185 CK_SAFE_RELEASE( object );
6186 // zero out the region
6187 *pInt = 0;
6188 // advance pointer
6189 pInt++;
6190 }
6191
6192 // decrement stack level
6193 m_stackLevel--;
6194 // pop stack unless we are level 1
6195 if( m_stack.size() > 1 )
6196 {
6197 // clean up
6198 CK_SAFE_DELETE_ARRAY( m_stack.back() );
6199 // pop
6200 m_stack.pop_back();
6201 }
6202
6203 return TRUE;
6204 }
6205
6206
6207
6208
6209
6210 //-----------------------------------------------------------------------------
6211 // name: params()
6212 // desc: for instruction dumps
6213 //-----------------------------------------------------------------------------
6214 const char * Chuck_Instr_Stmt_Remember_Object::params() const
6215 {
6216 static char buffer[CK_PRINT_BUF_LENGTH];
6217 snprintf( buffer, CK_PRINT_BUF_LENGTH, "offset=%lu start=%p addref:%s", (unsigned long)m_offset, m_stmtStart, m_addRef?"yes":"no" );
6218 return buffer;
6219 }
6220
6221
6222
6223
6224 //-----------------------------------------------------------------------------
6225 // name: execute()
6226 // desc: remember obj ref on reg stack for stmt-related cleanup
6227 //-----------------------------------------------------------------------------
6228 void Chuck_Instr_Stmt_Remember_Object::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6229 {
6230 // get stack pointer
6231 t_CKUINT * reg_sp = (t_CKUINT *)shred->reg->sp;
6232 Chuck_VM_Object * obj = (Chuck_VM_Object *)(*(reg_sp-1));
6233
6234 // add-ref for certain expressions (e.g., 'new Object;`)
6235 if( m_addRef ) CK_SAFE_ADD_REF( obj );
6236
6237 // check
6238 if( !m_stmtStart )
6239 {
6240 EM_exception(
6241 "(internal error) missing data region information in Stmt_Remember_Object instruction..." );
6242 goto error;
6243 }
6244
6245 // clear the objects returns by function calls in the statement
6246 if( !m_stmtStart->setObject( obj, m_offset ) )
6247 {
6248 EM_exception(
6249 "(internal error) cannot setObject() in Stmt_Remember_Object instruction..." );
6250 goto error;
6251 }
6252
6253 // done
6254 return;
6255
6256 error:
6257 // done
6258 shred->is_running = FALSE;
6259 shred->is_done = TRUE;
6260 }
6261
6262
6263
6264
6265 //-----------------------------------------------------------------------------
6266 // name: params()
6267 // desc: for instruction dumps
6268 //-----------------------------------------------------------------------------
6269 const char * Chuck_Instr_Stmt_Cleanup::params() const
6270 {
6271 static char buffer[CK_PRINT_BUF_LENGTH];
6272 snprintf( buffer, CK_PRINT_BUF_LENGTH, "numObjRelease=%lu start=%p", (unsigned long)(m_stmtStart ? m_stmtStart->m_numObjReleases : 0), m_stmtStart );
6273 return buffer;
6274 }
6275
6276
6277
6278
6279 //-----------------------------------------------------------------------------
6280 // name: execute()
6281 // desc: clean up after a statement
6282 //-----------------------------------------------------------------------------
6283 void Chuck_Instr_Stmt_Cleanup::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6284 {
6285 // check
6286 if( !m_stmtStart )
6287 {
6288 EM_exception(
6289 "(internal error) missing data region in Stmt_Cleanup instruction..." );
6290 goto error;
6291 }
6292
6293 // clean up references
6294 if( !m_stmtStart->cleanupRefs( shred ) )
6295 goto error;
6296
6297 // done
6298 return;
6299
6300 error:
6301 // done
6302 shred->is_running = FALSE;
6303 shred->is_done = TRUE;
6304 }
6305
6306
6307
6308
6309 //-----------------------------------------------------------------------------
6310 // name: execute()
6311 // desc: spork a shred from code
6312 //-----------------------------------------------------------------------------
6313 void Chuck_Instr_Spork::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6314 {
6315 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
6316 t_CKUINT this_ptr = 0;
6317 t_CKUINT type_ptr = 0;
6318
6319 // pop the stack
6320 pop_( reg_sp, 1 );
6321 // get the code
6322 Chuck_VM_Code * code = *(Chuck_VM_Code **)reg_sp;
6323 // spork it
6324 Chuck_VM_Shred * sh = vm->spork( code, shred, TRUE );
6325 // pop the stack
6326 pop_( reg_sp, 1 );
6327 // get the func
6328 Chuck_Func * func = (Chuck_Func *)(*reg_sp);
6329 // need this?
6330 if( func->is_member )
6331 {
6332 // pop the stack
6333 pop_( reg_sp, 1 );
6334 // get this
6335 this_ptr = *reg_sp;
6336 // add to shred so it's ref counted, and released when shred done (1.3.1.2)
6337 sh->add_parent_ref( (Chuck_Object *)this_ptr );
6338 }
6339 // need @type (for static functions) | 1.4.1.0 (ge) added
6340 else if( func->is_static )
6341 {
6342 // pop the stack
6343 pop_( reg_sp, 1 );
6344 // get type info
6345 type_ptr = *reg_sp;
6346 }
6347 // copy args
6348 if( m_val )
6349 {
6350 // ISSUE: 64-bit? (1.3.1.0: this should be OK as long as shred->reg->sp is t_CKBYTE *)
6351 pop_( shred->reg->sp, m_val );
6352 memcpy( sh->reg->sp, shred->reg->sp, m_val );
6353 sh->reg->sp += m_val;
6354 }
6355 // copy this, if need
6356 if( func->is_member )
6357 {
6358 push_( (t_CKUINT*&)sh->reg->sp, this_ptr );
6359 }
6360 // copy type info (for static functions) | 1.4.1.0 (ge) added
6361 else if( func->is_static )
6362 {
6363 push_( (t_CKUINT*&)sh->reg->sp, type_ptr );
6364 }
6365 // copy func
6366 push_( (t_CKUINT*&)sh->reg->sp, (t_CKUINT)func );
6367 // push the stack
6368 push_( reg_sp, (t_CKUINT)sh );
6369 }
6370
6371
6372
6373
6374 //-----------------------------------------------------------------------------
6375 // name: execute()
6376 // desc: ...
6377 //-----------------------------------------------------------------------------
6378 //void Chuck_Instr_Spork_Stmt::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6379 //{
6380 // t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
6381 //
6382 // // pop the stack
6383 // pop_( reg_sp, 1 );
6384 // // get the code
6385 // Chuck_VM_Code * code = *(Chuck_VM_Code **)reg_sp;
6386 // // spork it
6387 // Chuck_VM_Shred * sh = vm->spork( code, shred );
6388 //
6389 // if( code->need_this )
6390 // {
6391 // // pop the stack
6392 // pop_( reg_sp, 1 );
6393 // // copy this from local stack to top of new shred mem
6394 // *( ( t_CKUINT * ) sh->mem->sp ) = *reg_sp;
6395 // }
6396 //
6397 // // push the stack
6398 // push_( reg_sp, (t_CKUINT)sh );
6399 //}
6400
6401
6402
6403 #pragma mark === Time Advance ===
6404
6405 //-----------------------------------------------------------------------------
6406 // name: execute()
6407 // desc: advance to particular time (one TIME value on reg stack)
6408 //-----------------------------------------------------------------------------
6409 void Chuck_Instr_Time_Advance::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6410 {
6411 t_CKTIME *& sp = (t_CKTIME *&)shred->reg->sp;
6412
6413 // pop time value from reg stack
6414 pop_( sp, 1 );
6415
6416 // check for immediate mode exception | 1.5.1.5 (ge)
6417 if( shred->checkImmediatModeException(m_linepos) )
6418 {
6419 // do something!
6420 shred->is_running = FALSE;
6421 shred->is_done = TRUE;
6422 // bail out
6423 return;
6424 }
6425
6426 // check
6427 if( *sp < shred->now )
6428 {
6429 // we have a problem
6430 EM_exception(
6431 "DestTimeNegative: '%.6f' on line[%lu] in shred[id=%lu:%s]",
6432 *sp, m_linepos, shred->xid, shred->name.c_str() );
6433 // do something!
6434 shred->is_running = FALSE;
6435 shred->is_done = TRUE;
6436 // bail out
6437 return;
6438 }
6439
6440 // shredule the shred
6441 vm->shreduler()->shredule( shred, *sp );
6442 // suspend
6443 shred->is_running = FALSE;
6444 // increment towards per-shred garbage collection | 1.5.2.0 (ge)
6445 // NOTE 0-dur advance possible; but inc at least 1::samp
6446 // shred->gc_inc( ck_max((*sp)-shred->now,1) );
6447
6448 // track time advance
6449 CK_TRACK( Chuck_Stats::instance()->advance_time( shred, *sp ) );
6450
6451 // push time value on stack
6452 push_( sp, *sp );
6453 }
6454
6455
6456
6457
6458 //-----------------------------------------------------------------------------
6459 // name: execute()
6460 // desc: have current shred wait on event (expects one Event on reg stack)
6461 //-----------------------------------------------------------------------------
6462 void Chuck_Instr_Event_Wait::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6463 {
6464 // stack pointer
6465 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
6466 // pop word from reg stack
6467 pop_( sp, 1 );
6468 // cast to event
6469 Chuck_Event * event = (Chuck_Event *)(*sp);
6470
6471 // check for null
6472 if( !event ) goto null_pointer;
6473 // check for immediate mode exception | 1.5.1.5 (ge) added
6474 if( shred->checkImmediatModeException(m_linepos) ) goto done;
6475
6476 // wait
6477 event->wait( shred, vm );
6478
6479 // done
6480 return;
6481
6482 null_pointer:
6483 // we have a problem
6484 EM_exception(
6485 "NullPointer: (null Event wait) on line[%lu] in shred[id=%lu:%s]",
6486 m_linepos, shred->xid, shred->name.c_str() );
6487 goto done;
6488
6489 done:
6490 // do something!
6491 shred->is_running = FALSE;
6492 shred->is_done = TRUE;
6493 }
6494
6495
6496
6497 #pragma mark === Arrays ===
6498
6499
6500 //-----------------------------------------------------------------------------
6501 // name: Chuck_Instr_Array_Init_Literal()
6502 // desc: initialize array literal, e.g., [1,2,3]
6503 //-----------------------------------------------------------------------------
6504 Chuck_Instr_Array_Init_Literal::Chuck_Instr_Array_Init_Literal( Chuck_Env * env, Chuck_Type * t, t_CKINT length )
6505 {
6506 // set
6507 m_length = length;
6508 // copy
6509 m_type_ref = t;
6510 // add reference
6511 m_type_ref->add_ref();
6512 // type
6513 m_param_str = new char[72]; // 1.5.0.1 (ge) changed from 64 to 72
6514 // obj | REFACTOR-2017: added env
6515 m_is_obj = isobj( env, m_type_ref );
6516 // float | 1.4.2.0 (ge) added to differentiate between int and float arrays
6517 // -- in situations where they are the same size
6518 m_is_float = isa( t, env->ckt_float );
6519
6520 // copy type name
6521 const char * str = m_type_ref->c_name();
6522 t_CKUINT len = strlen( str );
6523 // copy
6524 if( len < 48 )
6525 strcpy( m_param_str, str );
6526 else
6527 {
6528 strncpy( m_param_str, str, 48 );
6529 strcpy( m_param_str + 48, "..." );
6530 }
6531
6532 // append length
6533 char buffer[16];
6534 snprintf( buffer, 16, "[%ld]", (long)m_length );
6535 strcat( m_param_str, buffer );
6536 }
6537
6538
6539
6540
6541 //-----------------------------------------------------------------------------
6542 // name: ~Chuck_Instr_Array_Init_Literal()
6543 // desc: ...
6544 //-----------------------------------------------------------------------------
6545 Chuck_Instr_Array_Init_Literal::~Chuck_Instr_Array_Init_Literal()
6546 {
6547 // delete
6548 CK_SAFE_DELETE_ARRAY( m_param_str );
6549 // release
6550 CK_SAFE_RELEASE( m_type_ref );
6551 }
6552
6553
6554
6555
6556 //-----------------------------------------------------------------------------
6557 // name: execute()
6558 // desc: ...
6559 //-----------------------------------------------------------------------------
6560 void Chuck_Instr_Array_Init_Literal::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6561 {
6562 // reg stack pointer
6563 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
6564
6565 // 1.5.4.4 (ge) added base_type logic for multidim arrays
6566 Chuck_Type * baseType = m_type_ref->array_type ? m_type_ref->array_type : m_type_ref;
6567 // amalgamating array type | 1.5.4.0 (ge, nick, andrew) added after a wild yak hunt
6568 // 1.5.4.4 (ge) updated to use `baseType` instead of `m_type_ref`
6569 Chuck_Type * arrayType = vm->env()->get_array_type( vm->env()->ckt_array, m_type_ref->array_depth+1, baseType );
6570
6571 // allocate the array
6572 // 1.4.2.0 (ge) | added: check for float explicitly
6573 if( m_type_ref->size == sz_INT && !m_is_float ) // ISSUE: 64-bit (fixed 1.3.1.0)
6574 {
6575 // TODO: look at size and treat Chuck_ArrayInt as ChuckArrayInt
6576 // pop the values
6577 pop_( reg_sp, m_length );
6578 // instantiate array
6579 Chuck_ArrayInt * array = new Chuck_ArrayInt( m_is_obj, m_length );
6580 // problem
6581 if( !array ) goto out_of_memory;
6582
6583 // initialize object
6584 // should it be this??? initialize_object( array, m_type_ref );
6585 // should it be this??? initialize_object( array, vm->env()->ckt_array, shred, vm );
6586 // neither! behold -- the amalgamated array type... | 1.5.4.0 (ge, nick, andrew)
6587 initialize_object( array, arrayType, shred, vm );
6588
6589 // set size
6590 array->set_size( m_length );
6591 // fill array
6592 for( t_CKINT i = 0; i < m_length; i++ )
6593 array->set( i, *(reg_sp + i) );
6594 // push the pointer
6595 push_( reg_sp, (t_CKUINT)array );
6596 }
6597 else if( m_type_ref->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
6598 {
6599 // pop the values
6600 pop_( reg_sp, m_length * (sz_FLOAT / sz_INT) ); // 1.3.1.0 added size division
6601 // instantiate array
6602 Chuck_ArrayFloat * array = new Chuck_ArrayFloat( m_length );
6603 // problem
6604 if( !array ) goto out_of_memory;
6605 // fill array
6606 t_CKFLOAT * sp = (t_CKFLOAT *)reg_sp;
6607 // intialize object
6608 initialize_object( array, arrayType, shred, vm );
6609 // set size
6610 array->set_size( m_length );
6611 // fill array
6612 for( t_CKINT i = 0; i < m_length; i++ )
6613 array->set( i, *(sp + i) );
6614 // push the pointer
6615 push_( reg_sp, (t_CKUINT)array );
6616 }
6617 else if( m_type_ref->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) | also should work for vec2 (noted 1.5.1.7)
6618 {
6619 // pop the values
6620 pop_( reg_sp, m_length * (sz_VEC2 / sz_INT) ); // 1.3.1.0 added size division
6621 // instantiate array
6622 Chuck_ArrayVec2 * array = new Chuck_ArrayVec2( m_length );
6623 // problem
6624 if( !array ) goto out_of_memory;
6625 // fill array
6626 t_CKVEC2 * sp = (t_CKVEC2 *)reg_sp;
6627 // intialize object
6628 initialize_object( array, arrayType, shred, vm );
6629 // differentiate between complex and polar | 1.5.1.0 (ge) added, used for sorting Array16s
6630 if( isa(m_type_ref, vm->env()->ckt_polar) ) array->m_isPolarType = TRUE;
6631 // set size
6632 array->set_size( m_length );
6633 // fill array
6634 for( t_CKINT i = 0; i < m_length; i++ )
6635 array->set( i, *(sp + i) );
6636 // push the pointer
6637 push_( reg_sp, (t_CKUINT)array );
6638 }
6639 else if( m_type_ref->size == sz_VEC3 ) // 1.3.5.3
6640 {
6641 // pop the values
6642 pop_( reg_sp, m_length * (sz_VEC3 / sz_INT) );
6643 // instantiate array
6644 Chuck_ArrayVec3 * array = new Chuck_ArrayVec3( m_length );
6645 // problem
6646 if( !array ) goto out_of_memory;
6647 // fill array
6648 t_CKVEC3 * sp = (t_CKVEC3 *)reg_sp;
6649 // intialize object
6650 initialize_object( array, arrayType, shred, vm );
6651 // set size
6652 array->set_size( m_length );
6653 // fill array
6654 for( t_CKINT i = 0; i < m_length; i++ )
6655 array->set( i, *(sp + i) );
6656 // push the pointer
6657 push_( reg_sp, (t_CKUINT)array );
6658 }
6659 else if( m_type_ref->size == sz_VEC4 ) // 1.3.5.3
6660 {
6661 // pop the values
6662 pop_( reg_sp, m_length * (sz_VEC4 / sz_INT) );
6663 // instantiate array
6664 Chuck_ArrayVec4 * array = new Chuck_ArrayVec4( m_length );
6665 // problem
6666 if( !array ) goto out_of_memory;
6667 // fill array
6668 t_CKVEC4 * sp = (t_CKVEC4 *)reg_sp;
6669 // intialize object
6670 initialize_object( array, arrayType, shred, vm );
6671 // set size
6672 array->set_size( m_length );
6673 // fill array
6674 for( t_CKINT i = 0; i < m_length; i++ )
6675 array->set( i, *(sp + i) );
6676 // push the pointer
6677 push_( reg_sp, (t_CKUINT)array );
6678 }
6679 else
6680 {
6681 // we have a problem
6682 EM_exception(
6683 "InvalidArrayTypeInfo: while initializing arrays on line[%lu]", m_linepos );
6684 EM_exception(
6685 "...(internal consistency compromised; halting..." );
6686 assert( FALSE );
6687 }
6688
6689 return;
6690
6691 out_of_memory:
6692
6693 // we have a problem
6694 EM_exception(
6695 "OutOfMemory: while initializing arrays on line[%lu]", m_linepos );
6696
6697 // do something!
6698 shred->is_running = FALSE;
6699 shred->is_done = TRUE;
6700 }
6701
6702
6703
6704
6705 //-----------------------------------------------------------------------------
6706 // name: Chuck_Instr_Array_Alloc()
6707 // desc: allocate a chuck array
6708 //-----------------------------------------------------------------------------
6709 Chuck_Instr_Array_Alloc::Chuck_Instr_Array_Alloc( Chuck_Env * env, t_CKUINT depth, Chuck_Type * contentType,
6710 t_CKUINT offset, t_CKBOOL is_ref, Chuck_Type * arrayType )
6711 {
6712 // set
6713 m_depth = depth;
6714
6715 // 1.5.0.0 (ge) now maintains two Chuck_Types: one for the array content;
6716 // one for the array itself (which has depth > 0); the latter is so that
6717 // array object can have a reference to the array type itself
6718
6719 // copy array content type, e.g., int
6720 m_type_ref_content = contentType;
6721 // remember
6722 CK_SAFE_ADD_REF( m_type_ref_content );
6723 // remember the type of the array itself, e.g., int[][]
6724 m_type_ref_array = arrayType;
6725 CK_SAFE_ADD_REF( m_type_ref_array );
6726
6727 // parameter string
6728 m_param_str = new char[64];
6729 // obj | REFACTOR-2017: added env
6730 m_is_obj = isobj( env, m_type_ref_content );
6731 // offset for pre constructor
6732 m_stack_offset = offset;
6733 // is object ref
6734 m_is_ref = is_ref;
6735 const char * str = m_type_ref_content->c_name();
6736 t_CKUINT len = strlen( str );
6737 // copy
6738 if( len < 64 )
6739 strcpy( m_param_str, str );
6740 else
6741 {
6742 strncpy( m_param_str, str, 60 );
6743 strcpy( m_param_str + 60, "..." );
6744 }
6745 }
6746
6747
6748
6749
6750 //-----------------------------------------------------------------------------
6751 // name: ~Chuck_Instr_Array_Alloc()
6752 // desc: ...
6753 //-----------------------------------------------------------------------------
6754 Chuck_Instr_Array_Alloc::~Chuck_Instr_Array_Alloc()
6755 {
6756 // delete | 1.5.0.0 (ge) convert to macro
6757 CK_SAFE_DELETE_ARRAY( m_param_str );
6758 // release | 1.5.0.0 (ge) added
6759 CK_SAFE_RELEASE(m_type_ref_content);
6760 CK_SAFE_RELEASE(m_type_ref_array);
6761 }
6762
6763
6764
6765
6766 //-----------------------------------------------------------------------------
6767 // name: do_alloc_array()
6768 // desc: 1.3.1.0 -- changed size to kind
6769 //-----------------------------------------------------------------------------
6770 Chuck_Object * do_alloc_array(
6771 Chuck_VM * vm, // REFACTOR-2017: added
6772 Chuck_VM_Shred * shred, // 1.5.1.5 added
6773 t_CKINT * capacity,
6774 const t_CKINT * top,
6775 t_CKUINT kind,
6776 t_CKBOOL is_obj,
6777 t_CKUINT * objs,
6778 t_CKINT & index,
6779 Chuck_Type * type)
6780 {
6781 // not top level
6782 Chuck_ArrayInt * theBase = NULL;
6783 Chuck_Object * next = NULL;
6784 Chuck_Type * typeNext = NULL;
6785 t_CKINT i = 0;
6786
6787 // capacity
6788 if( *capacity < 0 ) goto negative_array_size;
6789
6790 // see if top level
6791 if( capacity >= top )
6792 {
6793 // check size
6794 // 1.3.1.0: look at type to use kind instead of size
6795 if( kind == kindof_INT ) // ISSUE: 64-bit (fixed 1.3.1.0)
6796 {
6797 Chuck_ArrayInt * baseX = new Chuck_ArrayInt( is_obj, *capacity );
6798 if( !baseX ) goto out_of_memory;
6799
6800 // if object
6801 if( is_obj && objs )
6802 {
6803 // loop
6804 for( i = 0; i < *capacity; i++ )
6805 {
6806 // add to array for later allocation
6807 objs[index++] = baseX->addr(i);
6808 }
6809 }
6810
6811 // initialize object | 1.5.0.0 (ge) use array type instead of base t_array
6812 // for the object->type_ref to contain more specific information
6813 initialize_object( baseX, type, shred, vm );
6814
6815 // initialize_object( baseX, vm->env()->ckt_array );
6816 return baseX;
6817 }
6818 else if( kind == kindof_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
6819 {
6820 Chuck_ArrayFloat * baseX = new Chuck_ArrayFloat( *capacity );
6821 if( !baseX ) goto out_of_memory;
6822
6823 // initialize object | 1.5.0.0 (ge) use array type instead of base t_array
6824 // for the object->type_ref to contain more specific information
6825 initialize_object( baseX, type, shred, vm );
6826 // initialize_object( baseX, vm->env()->ckt_array );
6827 return baseX;
6828 }
6829 else if( kind == kindof_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0) | 1.5.1.7 (ge) complex -> vec2
6830 {
6831 Chuck_ArrayVec2 * baseX = new Chuck_ArrayVec2( *capacity );
6832 if( !baseX ) goto out_of_memory;
6833
6834 // check array type
6835 Chuck_Type * array_type = type->array_type;
6836 // differentiate between complex and polar | 1.5.1.0 (ge) added, used for sorting Array16s
6837 if( array_type && isa(array_type, vm->env()->ckt_polar) ) baseX->m_isPolarType = TRUE;
6838
6839 // initialize object | 1.5.0.0 (ge) use array type instead of base t_array
6840 // for the object->type_ref to contain more specific information
6841 initialize_object( baseX, type, shred, vm );
6842 // initialize_object( baseX, vm->env()->ckt_array );
6843 return baseX;
6844 }
6845 else if( kind == kindof_VEC3 ) // 1.3.5.3
6846 {
6847 Chuck_ArrayVec3 * baseX = new Chuck_ArrayVec3( *capacity );
6848 if( !baseX ) goto out_of_memory;
6849
6850 // initialize object | 1.5.0.0 (ge) use array type instead of base t_array
6851 // for the object->type_ref to contain more specific information
6852 initialize_object( baseX, type, shred, vm );
6853 // initialize_object( baseX, vm->env()->ckt_array );
6854 return baseX;
6855 }
6856 else if( kind == kindof_VEC4 ) // 1.3.5.3
6857 {
6858 Chuck_ArrayVec4* baseX = new Chuck_ArrayVec4( *capacity );
6859 if( !baseX ) goto out_of_memory;
6860
6861 // initialize object | 1.5.0.0 (ge) use array type instead of base t_array
6862 // for the object->type_ref to contain more specific information
6863 initialize_object( baseX, type, shred, vm );
6864 // initialize_object( baseX, vm->env()->ckt_array );
6865 return baseX;
6866 }
6867
6868 // shouldn't get here
6869 assert( FALSE );
6870 }
6871
6872 // not top level
6873 theBase = new Chuck_ArrayInt( TRUE, *capacity );
6874 if( !theBase ) goto out_of_memory;
6875
6876 // construct type for next array level | 1.5.0.0 (ge) added
6877 // TODO: look up in common array type pool before allocating
6878 // pass in NULL (context) as typeNext is meant to be temporary and
6879 // shouldn't be associated with context; also by this point,
6880 // vm->env()->context is likely the global context, and the
6881 // originating context (e.g., ck file) is already cleaned up | 1.5.1.1
6882 typeNext = type->copy( vm->env(), NULL );
6883 // check
6884 if( typeNext->array_depth == 0 ) goto internal_error_array_depth;
6885 // minus the depth
6886 typeNext->array_depth--;
6887 // add ref | 1.5.1.1
6888 CK_SAFE_ADD_REF( typeNext );
6889
6890 // allocate the next level
6891 for( i = 0; i < *capacity; i++ )
6892 {
6893 // the next | REFACTOR-2017: added vm
6894 next = do_alloc_array( vm, shred, capacity + 1, top, kind, is_obj, objs, index, typeNext );
6895 // error if NULL
6896 if( !next ) goto error;
6897 // set that, with ref count
6898 theBase->set( i, (t_CKUINT)next );
6899 }
6900
6901 // release | 1.5.1.1
6902 CK_SAFE_RELEASE( typeNext );
6903
6904 // initialize object | 1.5.0.0 (ge) use array type instead of base t_array
6905 // for the object->type_ref to contain more specific information
6906 initialize_object( theBase, type, shred, vm );
6907 // initialize_object( theBase, vm->env()->ckt_array );
6908 return theBase;
6909
6910 internal_error_array_depth:
6911 // we have a big problem
6912 EM_exception(
6913 "(internal error) multi-dimensional array depth mismatch while allocating arrays...");
6914 goto error;
6915
6916 out_of_memory:
6917 // we have a problem
6918 EM_exception(
6919 "OutOfMemory: while allocating arrays...");
6920 goto error;
6921
6922 negative_array_size:
6923 // we have a problem
6924 EM_exception(
6925 "NegativeArraySize: while allocating arrays...");
6926 goto error;
6927
6928 error:
6929 // base shouldn't have been ref counted
6930 CK_SAFE_DELETE( theBase );
6931 return NULL;
6932 }
6933
6934
6935
6936
6937 //-----------------------------------------------------------------------------
6938 // name: execute()
6939 // desc: ...
6940 //-----------------------------------------------------------------------------
6941 void Chuck_Instr_Array_Alloc::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
6942 {
6943 // reg stack pointer
6944 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
6945 // ref
6946 t_CKUINT ref = 0;
6947 // the total number of objects to allocate
6948 t_CKUINT num_obj = 0;
6949 // the index to pass to the arrays
6950 t_CKINT index = 0;
6951 // number
6952 t_CKFLOAT num = 1.0;
6953 // array
6954 t_CKUINT * obj_array = NULL;
6955 // size
6956 t_CKUINT obj_array_size = 0;
6957
6958 // if need instantiation
6959 if( m_is_obj && !m_is_ref )
6960 {
6961 t_CKINT * curr = (t_CKINT *)(reg_sp - m_depth);
6962 t_CKINT * top = (t_CKINT *)(reg_sp - 1);
6963
6964 num_obj = 1;
6965 num = 1.0;
6966 // product of all dims
6967 while( curr <= top )
6968 {
6969 num_obj *= *(curr);
6970
6971 // overflow
6972 num *= (t_CKFLOAT)(*(curr));
6973 if( num > (t_CKFLOAT)INT_MAX ) goto overflow;
6974
6975 curr++;
6976 }
6977
6978 // allocate array to hold elements, this array
6979 // is pushed on the reg stack, filled, and cleaned
6980 // during the array_post stage
6981 // ----
6982 // TODO: this scheme results in potential leak
6983 // if intermediate memory allocations fail
6984 // and the array instantiation is aborted
6985 if( num_obj > 0 )
6986 {
6987 obj_array = new t_CKUINT[num_obj];
6988 if( !obj_array ) goto out_of_memory;
6989 obj_array_size = num_obj;
6990 }
6991
6992 // check to see if we need to allocate
6993 // if( num_obj > shred->obj_array_size )
6994 // {
6995 // CK_SAFE_DELETE( shred->obj_array );
6996 // shred->obj_array_size = 0;
6997 // shred->obj_array = new t_CKUINT[num_obj];
6998 // if( !shred->obj_array ) goto out_of_memory;
6999 // shred->obj_array_size = num_obj;
7000 // }
7001 }
7002
7003 // recursively allocate | REFACTOR-2017: added env
7004 ref = (t_CKUINT)do_alloc_array( vm, shred,
7005 (t_CKINT *)(reg_sp - m_depth),
7006 (t_CKINT *)(reg_sp - 1),
7007 getkindof(vm->env(), m_type_ref_content), // 1.3.1.0: changed; was 'm_type_ref->size'
7008 m_is_obj, obj_array, index, m_type_ref_array // 1.5.0.0: added m_type_ref_array
7009 );
7010
7011 // pop the indices - this protects the contents of the stack
7012 // do_alloc_array writes stuff to the stack
7013 pop_( reg_sp, m_depth );
7014
7015 // make sure
7016 assert( index == (t_CKINT)num_obj );
7017
7018 // problem
7019 if( !ref ) goto error;
7020
7021 // push array
7022 push_( reg_sp, ref );
7023
7024 // if need to instantiate
7025 if( m_is_obj && !m_is_ref )
7026 {
7027 // push objects to instantiate
7028 push_( reg_sp, (t_CKUINT)obj_array );
7029 // push index to use
7030 push_( reg_sp, 0 );
7031 // push size
7032 push_( reg_sp, (t_CKUINT)num_obj );
7033 }
7034
7035 return;
7036
7037 overflow:
7038 // we have a problem
7039 EM_exception(
7040 "OverFlow: requested array size too big..." );
7041 goto error;
7042
7043 out_of_memory:
7044 // we have a problem
7045 EM_exception(
7046 "OutOfMemory: while allocating arrays..." );
7047 goto error;
7048
7049 error:
7050 EM_exception(
7051 "...on line[%lu] in shred[id=%lu:%s]",
7052 m_linepos, shred->xid, shred->name.c_str() );
7053
7054 // done
7055 shred->is_running = FALSE;
7056 shred->is_done = TRUE;
7057 }
7058
7059
7060
7061
7062 //-----------------------------------------------------------------------------
7063 // name: normalize_index()
7064 // desc: normalize index to allow for negative array indexing
7065 // 1.5.0.0 (nshaheed) added
7066 //-----------------------------------------------------------------------------
7067 static t_CKINT normalize_index( t_CKINT i, t_CKINT len )
7068 {
7069 // a normal array index
7070 if( i >= 0 ) return i;
7071 // start with len-1 and keep going down
7072 return len + i;
7073 }
7074
7075
7076
7077
7078 //-----------------------------------------------------------------------------
7079 // name: execute()
7080 // desc: ...
7081 //-----------------------------------------------------------------------------
7082 void Chuck_Instr_Array_Access::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7083 {
7084 // reg stack pointer
7085 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7086 // UNUSED: t_CKUINT *& reg_sp = sp;
7087 t_CKINT i = 0;
7088 t_CKINT ni = 0; // normalized
7089 t_CKUINT val = 0;
7090 t_CKFLOAT fval = 0;
7091 t_CKVEC2 v2;
7092 v2.x = 0;
7093 v2.y = 0;
7094 // vec2 should behave here as complex
7095 t_CKVEC3 v3;
7096 t_CKVEC4 v4;
7097
7098 // pop
7099 pop_( sp, 2 );
7100
7101 // check pointer
7102 if( !(*sp) ) goto null_pointer;
7103
7104 // 4 or 8 or 16
7105 // 1.3.1.0: look at type to use kind instead of size
7106 if( m_kind == kindof_INT ) // ISSUE: 64-bit (fixed 1.3.1.0)
7107 {
7108 // get array
7109 Chuck_ArrayInt * arr = (Chuck_ArrayInt *)(*sp);
7110 // get index
7111 i = (t_CKINT)(*(sp+1));
7112 // normalize index to allow for negative indexing | 1.5.0.0 (nshaheed) added
7113 ni = normalize_index( i, arr->size() );
7114 // check if writing
7115 if( m_emit_addr ) {
7116 // get the addr
7117 val = arr->addr( ni );
7118 // exception
7119 if( !val ) goto array_out_of_bound;
7120 // push the addr
7121 push_( sp, val );
7122 } else {
7123 // get the value
7124 if( !arr->get( ni, &val ) )
7125 goto array_out_of_bound;
7126 // push the value
7127 push_( sp, val );
7128 }
7129 }
7130 else if( m_kind == kindof_FLOAT ) // ISSUE: 64-bit (1.3.1.0)
7131 {
7132 // get array
7133 Chuck_ArrayFloat * arr = (Chuck_ArrayFloat *)(*sp);
7134 // get index
7135 i = (t_CKINT)(*(sp+1));
7136 // normalize index to allow for negative indexing
7137 ni = normalize_index( i, arr->size() );
7138 // check if writing
7139 if( m_emit_addr ) {
7140 // get the addr
7141 val = arr->addr( ni );
7142 // exception
7143 if( !val ) goto array_out_of_bound;
7144 // push the addr
7145 push_( sp, val );
7146 } else {
7147 // get the value
7148 if( !arr->get( ni, &fval ) )
7149 goto array_out_of_bound;
7150 // push the value
7151 push_( ((t_CKFLOAT *&)sp), fval );
7152 }
7153 }
7154 else if( m_kind == kindof_VEC2 ) // ISSUE: 64-bit | 1.5.1.7 (ge) complex -> vec2
7155 {
7156 // get array
7157 Chuck_ArrayVec2 * arr = (Chuck_ArrayVec2 *)(*sp);
7158 // get index
7159 i = (t_CKINT)(*(sp+1));
7160 // normalize index to allow for negative indexing
7161 ni = normalize_index( i, arr->size() );
7162 // check if writing
7163 if( m_emit_addr ) {
7164 // get the addr
7165 val = arr->addr( ni );
7166 // exception
7167 if( !val ) goto array_out_of_bound;
7168 // push the addr
7169 push_( sp, val );
7170 } else {
7171 // get the value
7172 if( !arr->get( ni, &v2 ) )
7173 goto array_out_of_bound;
7174 // push the value
7175 push_( ((t_CKVEC2 *&)sp), v2 );
7176 }
7177 }
7178 else if( m_kind == kindof_VEC3 ) // 1.3.5.3
7179 {
7180 // get array
7181 Chuck_ArrayVec3 * arr = (Chuck_ArrayVec3 *)(*sp);
7182 // get index
7183 i = (t_CKINT)(*(sp + 1));
7184 // normalize index to allow for negative indexing
7185 ni = normalize_index( i, arr->size() );
7186 // check if writing
7187 if( m_emit_addr ) {
7188 // get the addr
7189 val = arr->addr( ni );
7190 // exception
7191 if( !val ) goto array_out_of_bound;
7192 // push the addr
7193 push_( sp, val );
7194 } else {
7195 // get the value
7196 if( !arr->get( ni, &v3 ) )
7197 goto array_out_of_bound;
7198 // push the value
7199 push_( ((t_CKVEC3 *&)sp), v3 );
7200 }
7201 }
7202 else if( m_kind == kindof_VEC4 ) // 1.3.5.3
7203 {
7204 // get array
7205 Chuck_ArrayVec4 * arr = (Chuck_ArrayVec4 *)(*sp);
7206 // get index
7207 i = (t_CKINT)(*(sp+1));
7208 // normalize index to allow for negative indexing
7209 ni = normalize_index( i, arr->size() );
7210 // check if writing
7211 if( m_emit_addr ) {
7212 // get the addr
7213 val = arr->addr( ni );
7214 // exception
7215 if( !val ) goto array_out_of_bound;
7216 // push the addr
7217 push_( sp, val );
7218 } else {
7219 // get the value
7220 if( !arr->get( ni, &v4 ) )
7221 goto array_out_of_bound;
7222 // push the value
7223 push_( ((t_CKVEC4 *&)sp), v4 );
7224 }
7225 }
7226 else
7227 assert( FALSE );
7228
7229 return;
7230
7231 null_pointer:
7232 // we have a problem
7233 EM_exception(
7234 "NullPointer: (array access) on line[%lu] in shred[id=%lu:%s]",
7235 m_linepos, shred->xid, shred->name.c_str() ); // shred->pc removed
7236 goto done;
7237
7238 array_out_of_bound:
7239 // we have a problem
7240 EM_exception(
7241 "ArrayOutofBounds: on line[%lu] in shred[id=%lu:%s] index[%ld]",
7242 m_linepos, shred->xid, shred->name.c_str(), i ); // shred->pc removed
7243 // go to done
7244 goto done;
7245
7246 done:
7247 // do something!
7248 shred->is_running = FALSE;
7249 shred->is_done = TRUE;
7250 }
7251
7252
7253
7254
7255 //-----------------------------------------------------------------------------
7256 // name: execute()
7257 // desc: ...
7258 //-----------------------------------------------------------------------------
7259 void Chuck_Instr_Array_Map_Access::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7260 {
7261 // reg stack pointer
7262 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7263 Chuck_String * key = NULL;
7264 t_CKUINT val = 0;
7265 t_CKFLOAT fval = 0;
7266 t_CKVEC2 v2; v2.x = v2.y = 0;
7267 t_CKVEC3 v3;
7268 t_CKVEC4 v4;
7269
7270 // pop
7271 pop_( sp, 2 );
7272
7273 // check pointer
7274 if( !(*sp) ) goto null_pointer;
7275
7276 // 4 or 8 or 16
7277 // 1.3.1.0: look at type to use kind instead of size
7278 if( m_kind == kindof_INT ) // ISSUE: 64-bit (fixed 1.3.1.0)
7279 {
7280 // get array
7281 Chuck_ArrayInt * arr = (Chuck_ArrayInt *)(*sp);
7282 // get index
7283 key = (Chuck_String *)(*(sp+1));
7284 // check if writing
7285 if( m_emit_addr ) {
7286 // get the addr
7287 val = arr->addr( key->str() );
7288 // exception
7289 if( !val ) goto error;
7290 // push the addr
7291 push_( sp, val );
7292 } else {
7293 // get the value
7294 arr->get( key->str(), &val );
7295 // push the value
7296 push_( sp, val );
7297 }
7298 }
7299 else if( m_kind == kindof_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
7300 {
7301 // get array
7302 Chuck_ArrayFloat * arr = (Chuck_ArrayFloat *)(*sp);
7303 // get index
7304 key = (Chuck_String *)(*(sp+1));
7305 // check if writing
7306 if( m_emit_addr ) {
7307 // get the addr
7308 val = arr->addr( key->str() );
7309 // exception
7310 if( !val ) goto error;
7311 // push the addr
7312 push_( sp, val );
7313 } else {
7314 // get the value
7315 arr->get( key->str(), &fval );
7316 // push the value
7317 push_( ((t_CKFLOAT *&)sp), fval );
7318 }
7319 }
7320 else if( m_kind == kindof_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0)
7321 {
7322 // get array
7323 Chuck_ArrayVec2 * arr = (Chuck_ArrayVec2 *)(*sp);
7324 // get index
7325 key = (Chuck_String *)(*(sp+1));
7326 // check if writing
7327 if( m_emit_addr ) {
7328 // get the addr
7329 val = arr->addr( key->str() );
7330 // exception
7331 if( !val ) goto error;
7332 // push the addr
7333 push_( sp, val );
7334 } else {
7335 // get the value
7336 arr->get( key->str(), &v2 );
7337 // push the value
7338 push_( ((t_CKVEC2 *&)sp), v2 );
7339 }
7340 }
7341 else if( m_kind == kindof_VEC3 ) // 1.3.5.3
7342 {
7343 // get array
7344 Chuck_ArrayVec3 * arr = (Chuck_ArrayVec3 *)(*sp);
7345 // get index
7346 key = (Chuck_String *)(*(sp+1));
7347 // check if writing
7348 if( m_emit_addr ) {
7349 // get the addr
7350 val = arr->addr( key->str() );
7351 // exception
7352 if( !val ) goto error;
7353 // push the addr
7354 push_( sp, val );
7355 } else {
7356 // get the value
7357 arr->get( key->str(), &v3 );
7358 // push the value
7359 push_( ((t_CKVEC3 *&)sp), v3 );
7360 }
7361 }
7362 else if( m_kind == kindof_VEC4 ) // 1.3.5.3
7363 {
7364 // get array
7365 Chuck_ArrayVec4 * arr = (Chuck_ArrayVec4 *)(*sp);
7366 // get index
7367 key = (Chuck_String *)(*(sp+1));
7368 // check if writing
7369 if( m_emit_addr ) {
7370 // get the addr
7371 val = arr->addr( key->str() );
7372 // exception
7373 if( !val ) goto error;
7374 // push the addr
7375 push_( sp, val );
7376 } else {
7377 // get the value
7378 arr->get( key->str(), &v4 );
7379 // push the value
7380 push_( ((t_CKVEC4 *&)sp), v4 );
7381 }
7382 }
7383 else
7384 assert( FALSE );
7385
7386 return;
7387
7388 null_pointer:
7389 // we have a problem
7390 EM_exception(
7391 "NullPointer: (map access) on line[%lu] in shred[id=%lu:%s]",
7392 m_linepos, shred->xid, shred->name.c_str() ); // shred->pc
7393 goto done;
7394
7395 error:
7396 // we have a problem
7397 EM_exception(
7398 "InternalArrayMapError: on line[%lu] in shred[id=%lu:%s], index=[%s]",
7399 m_linepos, shred->xid, shred->name.c_str(), key->str().c_str() ); // shred->pc
7400 goto done;
7401
7402 done:
7403 // do something!
7404 shred->is_running = FALSE;
7405 shred->is_done = TRUE;
7406 }
7407
7408
7409
7410
7411 //-----------------------------------------------------------------------------
7412 // name: execute()
7413 // desc: ...
7414 //-----------------------------------------------------------------------------
7415 void Chuck_Instr_Array_Access_Multi::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7416 {
7417 // reg stack pointer
7418 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7419 t_CKINT i = 0;
7420 t_CKUINT val = 0, j;
7421 t_CKFLOAT fval = 0;
7422 t_CKINT * ptr = NULL;
7423 t_CKUINT index = 0;
7424 t_CKVEC2 v2; v2.x = v2.y = 0;
7425 t_CKVEC3 v3;
7426 t_CKVEC4 v4;
7427
7428 // pop all indices then array
7429 pop_( sp, m_depth + 1 );
7430
7431 // get array
7432 Chuck_ArrayInt * base = (Chuck_ArrayInt *)(*sp);
7433 ptr = (t_CKINT *)(sp+1);
7434
7435 // check for null
7436 if( !base ) goto null_pointer;
7437
7438 // make sure
7439 assert( m_depth > 1 );
7440 // loop through indices
7441 for( j = 0; j < (m_depth-1); j++ )
7442 {
7443 // get index
7444 i = *ptr++;
7445 // check if index is string (added 1.3.1.0 -- thanks Robin Haberkorn!)
7446 if( j < m_indexIsAssociative.size() && m_indexIsAssociative[j] )
7447 {
7448 // get index
7449 Chuck_String * key = (Chuck_String *)(i);
7450 // get the array
7451 if( !base->get( key->str(), &val ) )
7452 goto array_out_of_bound;
7453 }
7454 else
7455 {
7456 // get the array
7457 if( !base->get( i, &val ) )
7458 goto array_out_of_bound;
7459 }
7460
7461 // set the array
7462 base = (Chuck_ArrayInt *)val;
7463 // check for null
7464 if( !base )
7465 {
7466 // error
7467 index = j + 1;
7468 goto null_pointer;
7469 }
7470 }
7471
7472 // 4 or 8 or 16
7473 // 1.3.1.0: look at type and use kind instead of size
7474 if( m_kind == kindof_INT ) // ISSUE: 64-bit (fixed 1.3.1.0)
7475 {
7476 // get arry
7477 Chuck_ArrayInt * arr = base;
7478 // get index
7479 i = (t_CKINT)(*ptr);
7480 // check if writing
7481 if( m_emit_addr ) {
7482 // get the addr
7483 val = arr->addr( i );
7484 // exception
7485 if( !val ) goto array_out_of_bound;
7486 // push the addr
7487 push_( sp, val );
7488 } else {
7489 // get the value
7490 if( !arr->get( i, &val ) )
7491 goto array_out_of_bound;
7492 // push the value
7493 push_( sp, val );
7494 }
7495 }
7496 else if( m_kind == kindof_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
7497 {
7498 // get array
7499 Chuck_ArrayFloat * arr = (Chuck_ArrayFloat *)(base);
7500 // get index
7501 i = (t_CKINT)(*ptr);
7502 // check if writing
7503 if( m_emit_addr ) {
7504 // get the addr
7505 val = arr->addr( i );
7506 // exception
7507 if( !val ) goto array_out_of_bound;
7508 // push the addr
7509 push_( sp, val );
7510 } else {
7511 // get the value
7512 if( !arr->get( i, &fval ) )
7513 goto array_out_of_bound;
7514 // push the value
7515 push_( ((t_CKFLOAT *&)sp), fval );
7516 }
7517 }
7518 else if( m_kind == kindof_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0)
7519 {
7520 // get array
7521 Chuck_ArrayVec2 * arr = (Chuck_ArrayVec2 *)(base);
7522 // get index
7523 i = (t_CKINT)(*ptr);
7524 // check if writing
7525 if( m_emit_addr ) {
7526 // get the addr
7527 val = arr->addr( i );
7528 // exception
7529 if( !val ) goto array_out_of_bound;
7530 // push the addr
7531 push_( sp, val );
7532 } else {
7533 // get the value
7534 if( !arr->get( i, &v2 ) )
7535 goto array_out_of_bound;
7536 // push the value
7537 push_( ((t_CKVEC2 *&)sp), v2 );
7538 }
7539 }
7540 else if( m_kind == kindof_VEC3 ) // 1.3.5.3
7541 {
7542 // get array
7543 Chuck_ArrayVec3 * arr = (Chuck_ArrayVec3 *)(base);
7544 // get index
7545 i = (t_CKINT)(*ptr);
7546 // check if writing
7547 if( m_emit_addr ) {
7548 // get the addr
7549 val = arr->addr( i );
7550 // exception
7551 if( !val ) goto array_out_of_bound;
7552 // push the addr
7553 push_( sp, val );
7554 } else {
7555 // get the value
7556 if( !arr->get( i, &v3 ) )
7557 goto array_out_of_bound;
7558 // push the value
7559 push_( ((t_CKVEC3 *&)sp), v3 );
7560 }
7561 }
7562 else if( m_kind == kindof_VEC4 ) // 1.3.5.3
7563 {
7564 // get array
7565 Chuck_ArrayVec4 * arr = (Chuck_ArrayVec4 *)(base);
7566 // get index
7567 i = (t_CKINT)(*ptr);
7568 // check if writing
7569 if( m_emit_addr ) {
7570 // get the addr
7571 val = arr->addr( i );
7572 // exception
7573 if( !val ) goto array_out_of_bound;
7574 // push the addr
7575 push_( sp, val );
7576 } else {
7577 // get the value
7578 if( !arr->get( i, &v4 ) )
7579 goto array_out_of_bound;
7580 // push the value
7581 push_( ((t_CKVEC4 *&)sp), v4 );
7582 }
7583 }
7584 else
7585 assert( FALSE );
7586
7587 return;
7588
7589 null_pointer:
7590 // we have a problem
7591 EM_exception(
7592 "NullPointer: (array access) on line[%lu] in shred[id=%lu:%s]",
7593 m_linepos, shred->xid, shred->name.c_str() ); // shred->pc
7594 EM_exception(
7595 "...(array dimension where exception occurred: %lu of %lu)", index+1, m_depth );
7596 goto done;
7597
7598 array_out_of_bound:
7599 // we have a problem
7600 EM_exception(
7601 "ArrayOutofBounds: on line[%lu] in shred[id=%lu:%s] index[%ld]",
7602 m_linepos, shred->xid, shred->name.c_str(), i ); // shred->pc
7603 // go to done
7604 goto done;
7605
7606 done:
7607 // do something!
7608 shred->is_running = FALSE;
7609 shred->is_done = TRUE;
7610 }
7611
7612
7613
7614
7615 //-----------------------------------------------------------------------------
7616 // name: execute()
7617 // desc: ...
7618 //-----------------------------------------------------------------------------
7619 void Chuck_Instr_Array_Prepend::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7620 {
7621 }
7622
7623
7624
7625
7626 //-----------------------------------------------------------------------------
7627 // name: execute()
7628 // desc: ...
7629 //-----------------------------------------------------------------------------
7630 void Chuck_Instr_Array_Append::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7631 {
7632 // reg stack pointer
7633 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7634 // t_CKINT i = 0;
7635 t_CKUINT val = 0;
7636 t_CKFLOAT fval = 0;
7637 t_CKVEC2 v2; v2.x = v2.y = 0;
7638 t_CKVEC3 v3;
7639 t_CKVEC4 v4;
7640
7641 // how much to pop (added 1.3.1.0)
7642 t_CKUINT howmuch = 0;
7643 // check kind
7644 if( m_val == kindof_INT ) howmuch = 1;
7645 else if( m_val == kindof_FLOAT ) howmuch = sz_FLOAT / sz_INT;
7646 else if( m_val == kindof_VEC2 ) howmuch = sz_VEC2 / sz_INT;
7647 else if( m_val == kindof_VEC3 ) howmuch = sz_VEC3 / sz_INT;
7648 else if( m_val == kindof_VEC4 ) howmuch = sz_VEC4 / sz_INT;
7649 // pop (1.3.1.0: use howmuch instead of m_val/4)
7650 pop_( sp, 1 + howmuch ); // ISSUE: 64-bit (fixed 1.3.1.0)
7651
7652 // check pointer
7653 if( !(*sp) ) goto null_pointer;
7654
7655 // 4 or 8 or 16
7656 // 1.3.1.0: changed to look at type (instead of size)
7657 if( m_val == kindof_INT ) // ISSUE: 64-bit (fixed 1.3.1.0)
7658 {
7659 // get array
7660 Chuck_ArrayInt * arr = (Chuck_ArrayInt *)(*sp);
7661 // get value
7662 val = (t_CKINT)(*(sp+1));
7663 // append
7664 arr->push_back( val );
7665 }
7666 else if( m_val == kindof_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
7667 {
7668 // get array
7669 Chuck_ArrayFloat * arr = (Chuck_ArrayFloat *)(*sp);
7670 // get value
7671 fval = (*(t_CKFLOAT *)(sp+1));
7672 // append
7673 arr->push_back( fval );
7674 }
7675 else if( m_val == kindof_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0)
7676 {
7677 // get array
7678 Chuck_ArrayVec2 * arr = (Chuck_ArrayVec2 *)(*sp);
7679 // get value
7680 v2 = (*(t_CKVEC2 *)(sp+1));
7681 // append
7682 arr->push_back( v2 );
7683 }
7684 else if( m_val == kindof_VEC3 ) // 1.3.5.3
7685 {
7686 // get array
7687 Chuck_ArrayVec3 * arr = (Chuck_ArrayVec3 *)(*sp);
7688 // get value
7689 v3 = (*(t_CKVEC3 *)(sp+1));
7690 // append
7691 arr->push_back( v3 );
7692 }
7693 else if( m_val == kindof_VEC4 ) // 1.3.5.3
7694 {
7695 // get array
7696 Chuck_ArrayVec4 * arr = (Chuck_ArrayVec4 *)(*sp);
7697 // get value
7698 v4 = (*(t_CKVEC4 *)(sp+1));
7699 // append
7700 arr->push_back( v4 );
7701 }
7702 else
7703 assert( FALSE );
7704
7705 // push array back on stack
7706 push_( sp, (*sp) );
7707
7708 return;
7709
7710 null_pointer:
7711 // we have a problem
7712 EM_exception(
7713 "NullPointer: (array append) on line[%lu] in shred[id=%lu:%s]",
7714 m_linepos, shred->xid, shred->name.c_str() );
7715 goto done;
7716
7717 done:
7718 // do something!
7719 shred->is_running = FALSE;
7720 shred->is_done = TRUE;
7721 }
7722
7723
7724
7725 #pragma mark === Dot Access ===
7726
7727 //-----------------------------------------------------------------------------
7728 // name: execute()
7729 // desc: ...
7730 //-----------------------------------------------------------------------------
7731 void Chuck_Instr_Dot_Member_Data::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7732 {
7733 // register stack pointer
7734 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7735 // the pointer
7736 t_CKUINT data;
7737
7738 // pop the object pointer
7739 pop_( sp, 1 );
7740 // get the object pointer
7741 Chuck_Object * obj = (Chuck_Object *)(*sp);
7742 // check
7743 if( !obj ) goto error;
7744 // calculate the data pointer
7745 data = (t_CKUINT)(obj->data + m_offset);
7746
7747 // emit addr or value
7748 if( m_emit_addr )
7749 {
7750 // push the address
7751 push_( sp, data );
7752 }
7753 else
7754 {
7755 // 4 or 8 or 16
7756 // 1.3.1.0: check type to use kind instead of size
7757 if( m_kind == kindof_INT ) { push_( sp, *((t_CKINT *)data) ); } // ISSUE: 64-bit (fixed 1.3.1.0)
7758 else if( m_kind == kindof_FLOAT ) { push_float( sp, *((t_CKFLOAT *)data) ); } // ISSUE: 64-bit (fixed 1.3.1.0)
7759 else if( m_kind == kindof_VEC2 ) { push_vec2( sp, *((t_CKVEC2 *)data) ); } // ISSUE: 64-bit (fixed 1.3.1.0) | in this context, vec2 = complex = polar
7760 else if( m_kind == kindof_VEC3 ) { push_vec3( sp, *((t_CKVEC3 *)data) ); } // 1.3.5.3
7761 else if( m_kind == kindof_VEC4 ) { push_vec4( sp, *((t_CKVEC4 *)data) ); } // 1.3.5.3
7762 else assert( FALSE );
7763 }
7764
7765 return;
7766
7767 error:
7768 // we have a problem
7769 EM_exception(
7770 "NullPointer: on line[%lu] in shred[id=%lu:%s]",
7771 m_linepos, shred->xid, shred->name.c_str() );
7772
7773 // do something!
7774 shred->is_running = FALSE;
7775 shred->is_done = TRUE;
7776 }
7777
7778
7779
7780
7781 //-----------------------------------------------------------------------------
7782 // name: execute()
7783 // desc: ...
7784 //-----------------------------------------------------------------------------
7785 void Chuck_Instr_Dot_Member_Func::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7786 {
7787 // register stack pointer
7788 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7789 // the pointer
7790 t_CKUINT data;
7791
7792 // pop the object pointer
7793 pop_( sp, 1 );
7794 // get the object pointer
7795 Chuck_Object * obj = (Chuck_Object *)(*sp);
7796 // check
7797 if( !obj ) goto error;
7798 // make sure we are in range
7799 assert( m_offset < obj->vtable->funcs.size() );
7800 // calculate the data pointer
7801 data = (t_CKUINT)(obj->vtable->funcs[m_offset]);
7802
7803 // push the address
7804 push_( sp, data );
7805
7806 return;
7807
7808 error:
7809 // we have a problem
7810 EM_exception(
7811 "NullPointer: on line[%lu] in shred[id=%lu:%s]",
7812 m_linepos, shred->xid, shred->name.c_str() );
7813
7814 // do something!
7815 shred->is_running = FALSE;
7816 shred->is_done = TRUE;
7817 }
7818
7819
7820
7821
7822 //-----------------------------------------------------------------------------
7823 // name: execute()
7824 // desc: call super class member function | 1.5.5.6 (niccolo) added
7825 //-----------------------------------------------------------------------------
7826 void Chuck_Instr_Dot_Member_Func_Super::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7827 {
7828 // register stack pointer
7829 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7830 // the pointer
7831 t_CKUINT data;
7832
7833 // pop the object pointer
7834 pop_( sp, 1 );
7835 // get the object pointer
7836 Chuck_Object * obj = (Chuck_Object *)(*sp);
7837 // check
7838 if( !obj ) goto error;
7839 // make sure we are in range
7840 assert( m_offset < obj->vtable->funcs.size() );
7841 // calculate the data pointer
7842 data = (t_CKUINT)get_func( obj );
7843
7844 // push the address
7845 push_( sp, data );
7846
7847 return;
7848
7849 error:
7850 // we have a problem
7851 EM_exception(
7852 "NullPointer: on line[%lu] in shred[id=%lu:%s]",
7853 m_linepos, shred->xid, shred->name.c_str() );
7854
7855 // do something!
7856 shred->is_running = FALSE;
7857 shred->is_done = TRUE;
7858 }
7859
7860
7861
7862
7863 //-----------------------------------------------------------------------------
7864 // name: get_func() | 1.5.5.6 (niccolo) added
7865 // desc: recursively, move up until the function from the correct type is found
7866 //-----------------------------------------------------------------------------
7867 Chuck_Func * Chuck_Instr_Dot_Member_Func_Super::get_func( Chuck_Object * obj ) const
7868 {
7869 // current func and type
7870 Chuck_Func * func = obj->vtable->funcs[m_offset];
7871 // move up until the type matches, or we it can no longer go up
7872 while( !equals( func->value_ref->owner_class, m_type) && func->up )
7873 {
7874 // super class function
7875 func = func->up->owner->obj_v_table.funcs[m_offset];
7876 }
7877 return func;
7878 }
7879
7880
7881
7882
7883 //-----------------------------------------------------------------------------
7884 // name: execute()
7885 // desc: primitive func, 1.3.5.3
7886 //-----------------------------------------------------------------------------
7887 void Chuck_Instr_Dot_Primitive_Func::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7888 {
7889 // register stack pointer
7890 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7891
7892 // pop the primitive pointer
7893 pop_( sp, 1 );
7894
7895 // push the function address address
7896 push_( sp, m_native_func );
7897 }
7898
7899
7900
7901
7902 //-----------------------------------------------------------------------------
7903 // name: execute()
7904 // desc: ...
7905 //-----------------------------------------------------------------------------
7906 void Chuck_Instr_Dot_Static_Data::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7907 {
7908 // register stack pointer
7909 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7910 // the pointer
7911 t_CKUINT data;
7912
7913 // pop the type pointer
7914 pop_( sp, 1 );
7915 // get the object pointer
7916 Chuck_Type * t_class = (Chuck_Type *)(*sp);
7917 // make sure
7918 assert( (m_offset + m_size) <= t_class->nspc->static_data_size );
7919 // calculate the data pointer
7920 data = (t_CKUINT)(t_class->nspc->static_data + m_offset);
7921
7922 // emit addr or value
7923 if( m_emit_addr )
7924 {
7925 // push the address
7926 push_( sp, data );
7927 }
7928 else
7929 {
7930 // 4 or 8 or 16
7931 // 1.3.1.0: check type to use kind instead of size
7932 if( m_kind == kindof_INT ) { push_( sp, *((t_CKUINT *)data) ); } // ISSUE: 64-bit (fixed 1.3.1.0)
7933 else if( m_kind == kindof_FLOAT ) { push_float( sp, *((t_CKFLOAT *)data) ); } // ISSUE: 64-bit (fixed 1.3.1.0)
7934 else if( m_kind == kindof_VEC2 ) { push_vec2( sp, *((t_CKVEC2 *)data) ); } // ISSUE: 64-bit (fixed 1.3.1.0) | in this context, vec2 = complex = polar
7935 else if( m_kind == kindof_VEC3 ) { push_vec3( sp, *((t_CKVEC3 *)data) ); } // 1.3.5.3
7936 else if( m_kind == kindof_VEC4 ) { push_vec4( sp, *((t_CKVEC4 *)data) ); } // 1.3.5.3
7937 else assert( FALSE );
7938 }
7939 }
7940
7941
7942
7943
7944 //-----------------------------------------------------------------------------
7945 // name: execute()
7946 // desc: ...
7947 //-----------------------------------------------------------------------------
7948 void Chuck_Instr_Dot_Static_Import_Data::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7949 {
7950 // register stack pointer
7951 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7952
7953 // emit addr or value
7954 if( m_emit_addr )
7955 {
7956 // push the address
7957 push_( sp, (t_CKUINT)m_addr );
7958 }
7959 else
7960 {
7961 // 4 or 8 or 16
7962 // 1.3.1.0: check type to use kind instead of size
7963 if( m_kind == kindof_INT ) { push_( sp, *((t_CKUINT *)m_addr) ); } // ISSUE: 64-bit (fixed 1.3.1.0)
7964 else if( m_kind == kindof_FLOAT ) { push_float( sp, *((t_CKFLOAT *)m_addr) ); } // ISSUE: 64-bit (fixed 1.3.1.0)
7965 else if( m_kind == kindof_VEC2 ) { push_vec2( sp, *((t_CKVEC2 *)m_addr) ); } // ISSUE: 64-bit (fixed 1.3.1.0) | in this context, vec2 = complex = polar
7966 else if( m_kind == kindof_VEC3 ) { push_vec3( sp, *((t_CKVEC3 *)m_addr) ); } // 1.3.5.3
7967 else if( m_kind == kindof_VEC4 ) { push_vec4( sp, *((t_CKVEC4 *)m_addr) ); } // 1.3.5.3
7968 else assert( FALSE );
7969 }
7970 }
7971
7972
7973
7974
7975 //-----------------------------------------------------------------------------
7976 // name: execute()
7977 // desc: ...
7978 //-----------------------------------------------------------------------------
7979 void Chuck_Instr_Dot_Static_Func::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
7980 {
7981 // register stack pointer
7982 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
7983
7984 // 1.4.1.0 (ge): leave the base type on the operand stack
7985 // commented out: pop the type pointer
7986 // 1.5.4.3 (ge): uncommented, remove base pointer, consistent with
7987 // other dot-member function emission; this helps cleaning up the
7988 // stack, depending on whether this is part of a function value emission
7989 // only, or going to be used as a function call #2024-func-call-update
7990 pop_( sp, 1 );
7991
7992 // push the address
7993 push_( sp, (t_CKUINT)(m_func) );
7994 }
7995
7996
7997
7998
7999 //-----------------------------------------------------------------------------
8000 // name: execute()
8001 // desc: access the first field of a complex/polar/vec3/vec4 value
8002 //-----------------------------------------------------------------------------
8003 void Chuck_Instr_Dot_Cmp_First::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8004 {
8005 // reg contains pointer to complex elsewhere
8006 if( m_is_mem )
8007 {
8008 // pointer to var (regardless of value type)
8009 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8010 // pop
8011 pop_( sp, 1 );
8012 // push var addr or value?
8013 if( m_emit_addr ) {
8014 // push addr (NOTE this works for complex/polar/vec2/vec3/vec4)
8015 push_( sp, (t_CKUINT)(&((*(t_CKCOMPLEX **)sp)->re)) );
8016 } else {
8017 // push value
8018 push_float( sp, (*(t_CKCOMPLEX **)sp)->re );
8019 }
8020 }
8021 else
8022 {
8023 // make sure
8024 assert( m_emit_addr == FALSE );
8025
8026 // which kind of datatype?
8027 switch( m_kind )
8028 {
8029 // get corresponding value, pop that value, push field
8030 case kindof_VEC2:
8031 {
8032 t_CKVEC2 *& sp = (t_CKVEC2 *&)shred->reg->sp;
8033 pop_( sp, 1 ); push_float( sp, sp->x );
8034 }
8035 break;
8036
8037 case kindof_VEC3:
8038 {
8039 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
8040 pop_( sp, 1 ); push_float( sp, sp->x );
8041 }
8042 break;
8043
8044 case kindof_VEC4:
8045 {
8046 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
8047 pop_( sp, 1 ); push_float( sp, sp->x );
8048 }
8049 break;
8050
8051 // shouldn't get here
8052 default: assert( FALSE ); break;
8053 }
8054 }
8055 }
8056
8057
8058
8059
8060 //-----------------------------------------------------------------------------
8061 // name: execute()
8062 // desc: access the second field of a complex/polar/vec2/vec3/vec4 value
8063 //-----------------------------------------------------------------------------
8064 void Chuck_Instr_Dot_Cmp_Second::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8065 {
8066 // reg contains pointer to complex elsewhere
8067 if( m_is_mem )
8068 {
8069 // pointer to var (regardless of value type)
8070 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8071 // pop
8072 pop_( sp, 1 );
8073 // emit as var address or as value?
8074 if( m_emit_addr ) {
8075 // push addr (works for complex/polar/vec2/vec3/vec4)
8076 push_( sp, (t_CKUINT)(&((*(t_CKCOMPLEX **)sp)->im)) );
8077 } else {
8078 // push value
8079 push_float( sp, (*(t_CKCOMPLEX **)sp)->im );
8080 }
8081 }
8082 else
8083 {
8084 // make sure
8085 assert( m_emit_addr == FALSE );
8086
8087 // which kind of datatype?
8088 switch( m_kind )
8089 {
8090 // get corresponding value, pop that value, push field
8091 case kindof_VEC2:
8092 {
8093 t_CKVEC2 *& sp = (t_CKVEC2 *&)shred->reg->sp;
8094 pop_( sp, 1 ); push_float( sp, sp->y );
8095 }
8096 break;
8097
8098 case kindof_VEC3:
8099 {
8100 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
8101 pop_( sp, 1 ); push_float( sp, sp->y );
8102 }
8103 break;
8104
8105 case kindof_VEC4:
8106 {
8107 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
8108 pop_( sp, 1 ); push_float( sp, sp->y );
8109 }
8110 break;
8111
8112 // shouldn't get here
8113 default: assert( FALSE ); break;
8114 }
8115 }
8116 }
8117
8118
8119
8120
8121 //-----------------------------------------------------------------------------
8122 // name: execute()
8123 // desc: access the third field of a vec3/vec4 value
8124 //-----------------------------------------------------------------------------
8125 void Chuck_Instr_Dot_Cmp_Third::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8126 {
8127 // reg contains pointer to complex elsewhere
8128 if( m_is_mem )
8129 {
8130 // pointer
8131 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8132 // pop
8133 pop_( sp, 1 );
8134 // var addr or value?
8135 if( m_emit_addr ) {
8136 // push addr
8137 push_( sp, (t_CKUINT)(&((*(t_CKVEC3 **)sp)->z)) );
8138 } else {
8139 // push value
8140 push_float( sp, (*(t_CKVEC3 **)sp)->z );
8141 }
8142 }
8143 else
8144 {
8145 // make sure
8146 assert( m_emit_addr == FALSE );
8147
8148 // which kind of datatype?
8149 switch( m_kind )
8150 {
8151 // get corresponding value, pop that value, push field
8152 case kindof_VEC3:
8153 {
8154 t_CKVEC3 *& sp = (t_CKVEC3 *&)shred->reg->sp;
8155 pop_( sp, 1 ); push_float( sp, sp->z );
8156 }
8157 break;
8158
8159 case kindof_VEC4:
8160 {
8161 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
8162 pop_( sp, 1 ); push_float( sp, sp->z );
8163 }
8164 break;
8165
8166 // shouldn't get here
8167 default: assert( FALSE ); break;
8168 }
8169 }
8170 }
8171
8172
8173
8174
8175 //-----------------------------------------------------------------------------
8176 // name: execute()
8177 // desc: access the fourth field of a vec4 value
8178 //-----------------------------------------------------------------------------
8179 void Chuck_Instr_Dot_Cmp_Fourth::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8180 {
8181 // reg contains pointer to complex elsewhere
8182 if( m_is_mem )
8183 {
8184 // pointer
8185 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8186 // pop
8187 pop_( sp, 1 );
8188 // push var addr or value?
8189 if( m_emit_addr ) {
8190 // push addr
8191 push_( sp, (t_CKUINT)(&((*(t_CKVEC4 **)sp)->w)) );
8192 } else {
8193 // push value
8194 push_float( sp, (*(t_CKVEC4 **)sp)->w );
8195 }
8196 }
8197 else
8198 {
8199 // make sure...
8200 assert( m_emit_addr == FALSE );
8201 assert( m_kind == kindof_VEC4 );
8202
8203 // get vec4 value, pop that value, push field
8204 t_CKVEC4 *& sp = (t_CKVEC4 *&)shred->reg->sp;
8205 pop_( sp, 1 );
8206 push_float( sp, sp->w );
8207 }
8208 }
8209
8210
8211
8212
8213 #pragma mark === Casting ===
8214
8215
8216 //-----------------------------------------------------------------------------
8217 // name: execute()
8218 // desc: ...
8219 //-----------------------------------------------------------------------------
8220 void Chuck_Instr_Cast_double2int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8221 {
8222 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8223 t_CKINT *& sp_int = (t_CKINT *&)sp;
8224 pop_( sp, 1 );
8225 push_( sp_int, (t_CKINT)(*sp) );
8226 }
8227
8228
8229
8230
8231 //-----------------------------------------------------------------------------
8232 // name: execute()
8233 // desc: ...
8234 //-----------------------------------------------------------------------------
8235 void Chuck_Instr_Cast_int2double::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8236 {
8237 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
8238 t_CKFLOAT *& sp_double = (t_CKFLOAT *&)sp;
8239 pop_( sp, 1 );
8240 push_( sp_double, (t_CKFLOAT)(*sp) );
8241 }
8242
8243
8244
8245
8246 //-----------------------------------------------------------------------------
8247 // name: execute()
8248 // desc: ...
8249 //-----------------------------------------------------------------------------
8250 void Chuck_Instr_Cast_int2complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8251 {
8252 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
8253 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
8254 pop_( sp, 1 );
8255 // push re and im
8256 push_( sp_float, (t_CKFLOAT)(*sp) );
8257 push_( sp_float, (t_CKFLOAT)0 );
8258 }
8259
8260
8261
8262
8263 //-----------------------------------------------------------------------------
8264 // name: execute()
8265 // desc: ...
8266 //-----------------------------------------------------------------------------
8267 void Chuck_Instr_Cast_int2polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8268 {
8269 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
8270 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
8271 pop_( sp, 1 );
8272 // push re and im
8273 push_( sp_float, (t_CKFLOAT)(*sp) );
8274 push_( sp_float, (t_CKFLOAT)0 );
8275 }
8276
8277
8278
8279
8280 //-----------------------------------------------------------------------------
8281 // name: execute()
8282 // desc: ...
8283 //-----------------------------------------------------------------------------
8284 void Chuck_Instr_Cast_double2complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8285 {
8286 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8287 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
8288 // leave on stack and push 0
8289 push_( sp_float, (t_CKFLOAT)0 );
8290 }
8291
8292
8293
8294
8295 //-----------------------------------------------------------------------------
8296 // name: execute()
8297 // desc: ...
8298 //-----------------------------------------------------------------------------
8299 void Chuck_Instr_Cast_double2polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8300 {
8301 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8302 t_CKFLOAT *& sp_float = (t_CKFLOAT *&)sp;
8303 // leave on stack and push 0
8304 push_( sp_float, (t_CKFLOAT)0 );
8305 }
8306
8307
8308
8309
8310 //-----------------------------------------------------------------------------
8311 // name: execute()
8312 // desc: ...
8313 //-----------------------------------------------------------------------------
8314 void Chuck_Instr_Cast_complex2polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8315 {
8316 t_CKCOMPLEX * sp = (t_CKCOMPLEX *)shred->reg->sp;
8317 // find it
8318 sp--;
8319 t_CKPOLAR * sp_polar = (t_CKPOLAR *)sp;
8320 t_CKFLOAT modulus, phase;
8321 // leave on stack
8322 modulus = ::sqrt( sp->re*sp->re + sp->im*sp->im );
8323 phase = ::atan2( sp->im, sp->re );
8324 sp_polar->modulus = modulus;
8325 sp_polar->phase = phase;
8326 }
8327
8328
8329
8330
8331 //-----------------------------------------------------------------------------
8332 // name: execute()
8333 // desc: ...
8334 //-----------------------------------------------------------------------------
8335 void Chuck_Instr_Cast_polar2complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8336 {
8337 t_CKPOLAR * sp = (t_CKPOLAR *)shred->reg->sp;
8338 // find it
8339 sp--;
8340 t_CKCOMPLEX * sp_complex = (t_CKCOMPLEX *)sp;
8341 t_CKFLOAT re, im;
8342 // leave on stack
8343 re = sp->modulus * ::cos( sp->phase );
8344 im = sp->modulus * ::sin( sp->phase );
8345 sp_complex->re = re;
8346 sp_complex->im = im;
8347 }
8348
8349
8350
8351
8352 //-----------------------------------------------------------------------------
8353 // name: execute()
8354 // desc: cast vec2 to vec3 on reg stack
8355 //-----------------------------------------------------------------------------
8356 void Chuck_Instr_Cast_vec2tovec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8357 {
8358 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8359 // zero pad for z
8360 push_( sp, 0 );
8361 }
8362
8363
8364
8365
8366 //-----------------------------------------------------------------------------
8367 // name: execute()
8368 // desc: cast vec2 to vec4 on reg stack
8369 //-----------------------------------------------------------------------------
8370 void Chuck_Instr_Cast_vec2tovec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8371 {
8372 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8373 // zero pad for z, w
8374 push_( sp, 0 );
8375 push_( sp, 0 );
8376 }
8377
8378
8379
8380
8381 //-----------------------------------------------------------------------------
8382 // name: execute()
8383 // desc: cast vec3 to vec4 on reg stack
8384 //-----------------------------------------------------------------------------
8385 void Chuck_Instr_Cast_vec3tovec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8386 {
8387 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8388 // zero pad it
8389 push_( sp, 0 );
8390 }
8391
8392
8393
8394
8395 //-----------------------------------------------------------------------------
8396 // name: execute()
8397 // desc: cast vec3 to vec2 on reg stack
8398 //-----------------------------------------------------------------------------
8399 void Chuck_Instr_Cast_vec3tovec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8400 {
8401 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8402 // remove z component
8403 pop_( sp, 1 );
8404 }
8405
8406
8407
8408
8409 //-----------------------------------------------------------------------------
8410 // name: execute()
8411 // desc: cast vec4 to vec2 on reg stack
8412 //-----------------------------------------------------------------------------
8413 void Chuck_Instr_Cast_vec4tovec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8414 {
8415 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8416 // remove z and w components
8417 pop_( sp, 2 );
8418 }
8419
8420
8421
8422
8423 //-----------------------------------------------------------------------------
8424 // name: execute()
8425 // desc: cast vec4 to vec3 on reg stack
8426 //-----------------------------------------------------------------------------
8427 void Chuck_Instr_Cast_vec4tovec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8428 {
8429 t_CKFLOAT *& sp = (t_CKFLOAT *&)shred->reg->sp;
8430 // remove w component
8431 pop_( sp, 1 );
8432 }
8433
8434
8435
8436
8437 //-----------------------------------------------------------------------------
8438 // name: execute()
8439 // desc: "cast" Object to a string, using the Object's method .toString()
8440 //-----------------------------------------------------------------------------
8441 void Chuck_Instr_Cast_object2string::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8442 {
8443 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8444 // pop it
8445 pop_( sp, 1 );
8446 // object
8447 Chuck_Object * obj = (Chuck_Object *)(*sp);
8448 // return
8449 Chuck_DL_Return RETURN;
8450 // get toString from it (added 1.3.0.0 -- Chuck_DL_Api::instance())
8451 object_toString( obj, NULL, &RETURN, vm, shred, Chuck_DL_Api::instance() );
8452 // get pointer
8453 Chuck_String * str = RETURN.v_string;
8454 // set it
8455 push_( sp, (t_CKUINT)str );
8456 }
8457
8458
8459
8460
8461 //-----------------------------------------------------------------------------
8462 // name: params()
8463 // desc: params for printing
8464 //-----------------------------------------------------------------------------
8465 const char * Chuck_Instr_Cast_Runtime_Verify::params() const
8466 {
8467 static char buffer[CK_PRINT_BUF_LENGTH];
8468 snprintf( buffer, CK_PRINT_BUF_LENGTH, "%s $ %s", m_from->c_name(), m_to->c_name() );
8469 return buffer;
8470 }
8471
8472
8473
8474
8475 //-----------------------------------------------------------------------------
8476 // name: execute()
8477 // desc: type cast runtime verification
8478 //-----------------------------------------------------------------------------
8479 void Chuck_Instr_Cast_Runtime_Verify::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8480 {
8481 // stack pointer
8482 t_CKUINT * sp = (t_CKUINT *)shred->reg->sp;
8483 // cast to object
8484 Chuck_Object * obj = (Chuck_Object *)(*(sp-1));
8485
8486 // NULL is ok
8487 if( !obj ) return;
8488
8489 // actual type of object
8490 Chuck_Type * actualType = obj->type_ref;
8491 // check against to type to cast to
8492 if( !isa(actualType,m_to) ) goto invalid_cast;
8493
8494 // got here? ok
8495 return;
8496
8497 invalid_cast:
8498 // we have a problem
8499 EM_exception( "RuntimeCastIncompatible: in shred[id=%lu:%s]...",
8500 shred->xid, shred->name.c_str() );
8501 // print code context
8502 if( m_codeWithFormat != "" ) CK_FPRINTF_STDERR( m_codeWithFormat.c_str(), TC::orange(actualType->c_name(),true).c_str(), TC::orange(m_to->c_name(),true).c_str() );
8503 // done
8504 goto done;
8505
8506 done:
8507 // do something!
8508 shred->is_running = FALSE;
8509 shred->is_done = TRUE;
8510 }
8511
8512
8513
8514
8515 //-----------------------------------------------------------------------------
8516 // name: execute()
8517 // desc: various string ops
8518 // (no longer used; string ops now handled more properly by op overloads)
8519 //-----------------------------------------------------------------------------
8520 //void Chuck_Instr_Op_string::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8521 //{
8522 // t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8523 // Chuck_String * lhs = NULL;
8524 // Chuck_String * rhs = NULL;
8525 //
8526 // // pop
8527 // pop_( sp, 2 );
8528 // // get the string references
8529 // lhs = (Chuck_String *)*sp;
8530 // rhs = (Chuck_String *)*(sp + 1);
8531 // // neither should be null
8532 // if( !lhs || !rhs ) goto null_pointer;
8533 //
8534 // // look
8535 // switch( m_val )
8536 // {
8537 // case ae_op_eq:
8538 // push_( sp, lhs->str() == rhs->str() );
8539 // break;
8540 //
8541 // case ae_op_neq:
8542 // push_( sp, lhs->str() != rhs->str() );
8543 // break;
8544 //
8545 // case ae_op_lt:
8546 // push_( sp, lhs->str() < rhs->str() );
8547 // break;
8548 //
8549 // case ae_op_le:
8550 // push_( sp, lhs->str() <= rhs->str() );
8551 // break;
8552 //
8553 // case ae_op_gt:
8554 // push_( sp, lhs->str() > rhs->str() );
8555 // break;
8556 //
8557 // case ae_op_ge:
8558 // push_( sp, lhs->str() >= rhs->str() );
8559 // break;
8560 //
8561 // default:
8562 // goto invalid_op;
8563 // break;
8564 // }
8565 //
8566 // return;
8567 //
8568 //null_pointer:
8569 // // we have a problem
8570 // EM_exception(
8571 // "NullPointer: (string op) on line[%lu] in shred[id=%lu:%s]",
8572 // m_linepos, shred->xid, shred->name.c_str() );
8573 // goto done;
8574 //
8575 //invalid_op:
8576 // // we have a problem
8577 // EM_exception(
8578 // "InvalidStringOp: '%s' on line[%lu] in shred[id=%lu:%s]",
8579 // op2str((ae_Operator)m_val), m_linepos, shred->xid, shred->name.c_str() );
8580 // goto done;
8581 //
8582 //done:
8583 // // do something!
8584 // shred->is_running = FALSE;
8585 // shred->is_done = TRUE;
8586 //}
8587
8588
8589
8590 #pragma mark === Builtins ===
8591
8592
8593 //-----------------------------------------------------------------------------
8594 // name: execute()
8595 // desc: ...
8596 //-----------------------------------------------------------------------------
8597 void Chuck_Instr_ADC::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8598 {
8599 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
8600 push_( reg_sp, (t_CKUINT)vm->m_adc );
8601 }
8602
8603
8604
8605
8606 //-----------------------------------------------------------------------------
8607 // name: execute()
8608 // desc: ...
8609 //-----------------------------------------------------------------------------
8610 void Chuck_Instr_DAC::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8611 {
8612 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
8613 push_( reg_sp, (t_CKUINT)vm->m_dac );
8614 }
8615
8616
8617
8618
8619 //-----------------------------------------------------------------------------
8620 // name: execute()
8621 // desc: ...
8622 //-----------------------------------------------------------------------------
8623 void Chuck_Instr_Bunghole::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8624 {
8625 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
8626 push_( reg_sp, (t_CKUINT)vm->m_bunghole );
8627 }
8628
8629
8630
8631
8632 //-----------------------------------------------------------------------------
8633 // name: execute()
8634 // desc: ...
8635 //-----------------------------------------------------------------------------
8636 void Chuck_Instr_Chout::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8637 {
8638 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
8639 push_( reg_sp, (t_CKUINT) vm->chout() );
8640 }
8641
8642
8643
8644
8645 //-----------------------------------------------------------------------------
8646 // name: execute()
8647 // desc: ...
8648 //-----------------------------------------------------------------------------
8649 void Chuck_Instr_Cherr::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8650 {
8651 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
8652 push_( reg_sp, (t_CKUINT) vm->cherr() );
8653 }
8654
8655
8656 #pragma mark === UGens ===
8657
8658 //-----------------------------------------------------------------------------
8659 // name: Chuck_Instr_UGen_Link()
8660 // desc: ...
8661 //-----------------------------------------------------------------------------
8662 Chuck_Instr_UGen_Link::Chuck_Instr_UGen_Link( t_CKBOOL isUpChuck )
8663 {
8664 m_isUpChuck = isUpChuck;
8665 }
8666
8667
8668
8669
8670 //-----------------------------------------------------------------------------
8671 // name: execute()
8672 // desc: link one UGen to another; except two UGen pointers on reg stack
8673 //-----------------------------------------------------------------------------
8674 void Chuck_Instr_UGen_Link::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8675 {
8676 Chuck_UGen **& sp = (Chuck_UGen **&)shred->reg->sp;
8677
8678 // pop
8679 pop_( sp, 2 );
8680 // check for null
8681 if( !*(sp+1) || !(*sp) ) goto null_pointer;
8682
8683 // go for it
8684 (*(sp + 1))->add( *sp, m_isUpChuck );
8685 // push the second
8686 push_( sp, *(sp + 1) );
8687
8688 return;
8689
8690 null_pointer:
8691 // we have a problem
8692 EM_exception(
8693 "NullPointer: (UGen link) on line[%lu] in shred[id=%lu:%s]",
8694 m_linepos, shred->xid, shred->name.c_str() );
8695
8696 // do something!
8697 shred->is_running = FALSE;
8698 shred->is_done = TRUE;
8699 }
8700
8701
8702
8703 //-----------------------------------------------------------------------------
8704 // name: execute()
8705 // desc: linking array of UGens
8706 //-----------------------------------------------------------------------------
8707 void Chuck_Instr_UGen_Array_Link::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8708 {
8709 Chuck_Object **& sp = (Chuck_Object **&)shred->reg->sp;
8710 Chuck_Object * src_obj = NULL, * dst_obj = NULL;
8711 t_CKINT num_out = 1, num_in = 1;
8712
8713 // pop
8714 pop_( sp, 2 );
8715 // check for null
8716 if( !*(sp+1) || !(*sp) ) goto null_pointer;
8717
8718 src_obj = *sp;
8719 dst_obj = (*(sp + 1));
8720
8721 // go for it
8722 // 1.5.2.2 (ge) semantic update: default num_out and num_in to 1,
8723 // only update if actually an array; let ugen->add() sort out channels
8724 // 1.5.2.3 (pr-lab, ge, nick, eito, terry) un-break case of array => non-mono ugen
8725 num_out = ugen_generic_num_out( src_obj, m_srcIsArray );
8726 num_in = ugen_generic_num_in( dst_obj, m_dstIsArray );
8727
8728 // check for different combos, similar to ugen->add() | 1.5.2.2 (ge)
8729 if( num_out == 1 && num_in == 1 )
8730 {
8731 Chuck_UGen * dst_ugen = ugen_generic_get_dst( dst_obj, 0, m_dstIsArray );
8732 Chuck_UGen * src_ugen = ugen_generic_get_src( src_obj, 0, m_srcIsArray );
8733 if( dst_ugen == NULL || src_ugen == NULL ) goto null_pointer;
8734 dst_ugen->add( src_ugen, m_isUpChuck );
8735 }
8736 else if( num_out == 1 && num_in > 1 )
8737 {
8738 Chuck_UGen * src_ugen = ugen_generic_get_src( src_obj, 0, m_srcIsArray );
8739 for( t_CKINT i = 0; i < num_in; i++ )
8740 {
8741 Chuck_UGen * dst_ugen = ugen_generic_get_dst( dst_obj, i, m_dstIsArray );
8742 if( dst_ugen == NULL || src_ugen == NULL ) goto null_pointer;
8743 dst_ugen->add( src_ugen, m_isUpChuck );
8744 }
8745 }
8746 else if( num_out > 1 && num_in == 1 )
8747 {
8748 Chuck_UGen * dst_ugen = ugen_generic_get_dst( dst_obj, 0, m_dstIsArray );
8749 for( t_CKINT i = 0; i < num_out; i++ )
8750 {
8751 Chuck_UGen * src_ugen = ugen_generic_get_src( src_obj, i, m_srcIsArray );
8752 if( dst_ugen == NULL || src_ugen == NULL ) goto null_pointer;
8753 dst_ugen->add( src_ugen, m_isUpChuck );
8754 }
8755 }
8756 else if( num_out > 1 && num_in > 1 )
8757 {
8758 // find greater
8759 t_CKINT greater = ck_max( num_out, num_in );
8760 // map one to one, up to greater (lesser should modulo)
8761 for( t_CKINT i = 0; i < greater; i++ )
8762 {
8763 Chuck_UGen * src_ugen = ugen_generic_get_src( src_obj, i, m_srcIsArray );
8764 Chuck_UGen * dst_ugen = ugen_generic_get_dst( dst_obj, i, m_dstIsArray );
8765 if( dst_ugen == NULL || src_ugen == NULL ) goto null_pointer;
8766 dst_ugen->add( src_ugen, m_isUpChuck );
8767 }
8768 }
8769
8770 // push the second
8771 push_( sp, *(sp + 1) );
8772
8773 return;
8774
8775 null_pointer:
8776 // we have a problem
8777 EM_exception(
8778 "NullPointer: (UGen array link) on line[%lu] in shred[id=%lu:%s]",
8779 m_linepos, shred->xid, shred->name.c_str() );
8780
8781 // do something!
8782 shred->is_running = FALSE;
8783 shred->is_done = TRUE;
8784 }
8785
8786
8787
8788
8789
8790 //-----------------------------------------------------------------------------
8791 // name: execute()
8792 // desc: ...
8793 //-----------------------------------------------------------------------------
8794 void Chuck_Instr_UGen_UnLink::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8795 {
8796 Chuck_UGen **& sp = (Chuck_UGen **&)shred->reg->sp;
8797
8798 pop_( sp, 2 );
8799 (*(sp+1))->remove( *sp );
8800 push_( sp, *(sp + 1) );
8801 }
8802
8803
8804
8805 /*
8806 //-----------------------------------------------------------------------------
8807 // name: execute()
8808 // desc: ...
8809 //-----------------------------------------------------------------------------
8810 void Chuck_Instr_UGen_Ctrl::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8811 {
8812 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8813
8814 pop_( sp, 4 );
8815 Chuck_UGen * ugen = (Chuck_UGen *)*(sp+1);
8816 f_ctrl ctrl = (f_ctrl)*(sp+2);
8817 f_cget cget = (f_cget)*(sp+3);
8818 // set now
8819 ugen->now = shred->now;
8820 // call ctrl
8821 ctrl( ugen, (void *)sp );
8822 if( cget ) cget( ugen, (void *)sp );
8823 // push the new value
8824 push_( sp, *sp);
8825 }
8826
8827
8828
8829
8830 //-----------------------------------------------------------------------------
8831 // name: execute()
8832 // desc: ...
8833 //-----------------------------------------------------------------------------
8834 void Chuck_Instr_UGen_CGet::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8835 {
8836 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8837
8838 pop_( sp, 2 );
8839 Chuck_UGen * ugen = (Chuck_UGen *)*(sp);
8840 f_cget cget = (f_cget)*(sp+1);
8841 // set now
8842 ugen->now = shred->now;
8843 // call cget
8844 cget( ugen, (void *)sp );
8845 // push the new value
8846 push_( sp, *sp);
8847 }
8848
8849
8850
8851
8852 //-----------------------------------------------------------------------------
8853 // name: execute()
8854 // desc: ...
8855 //-----------------------------------------------------------------------------
8856 void Chuck_Instr_UGen_Ctrl2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8857 {
8858 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8859
8860 pop_( sp, 4 );
8861 Chuck_UGen * ugen = (Chuck_UGen *)*(sp+1);
8862 f_ctrl ctrl = (f_ctrl)*(sp+2);
8863 f_cget cget = (f_cget)*(sp+3);
8864 // set now
8865 ugen->now = shred->now;
8866 // call ctrl
8867 pop_( sp, 1 );
8868 ctrl( ugen, (void *)sp );
8869 if( cget ) cget( ugen, (void *)sp );
8870 // push the new value
8871 ((t_CKFLOAT *&)shred->reg->sp)++;
8872 }
8873
8874
8875
8876
8877 //-----------------------------------------------------------------------------
8878 // name: execute()
8879 // desc: ...
8880 //-----------------------------------------------------------------------------
8881 void Chuck_Instr_UGen_CGet2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8882 {
8883 t_CKUINT *& sp = (t_CKUINT *&)shred->reg->sp;
8884
8885 pop_( sp, 2 );
8886 Chuck_UGen * ugen = (Chuck_UGen *)*(sp);
8887 f_cget cget = (f_cget)*(sp+1);
8888 // set now
8889 ugen->now = shred->now;
8890 // call cget
8891 cget( ugen, (void *)sp );
8892 // push the new value
8893 ((t_CKFLOAT *&)shred->reg->sp)++;
8894 }
8895 */
8896
8897
8898
8899 //-----------------------------------------------------------------------------
8900 // name: execute()
8901 // desc: ...
8902 //-----------------------------------------------------------------------------
8903 void Chuck_Instr_UGen_PMsg::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8904 {
8905 Chuck_UGen **& sp = (Chuck_UGen **&)shred->reg->sp;
8906
8907 pop_( sp, 2 );
8908
8909 // (*(sp + 1))->pmsg( shred->now, *sp );
8910
8911 push_( sp, *(sp + 1) );
8912 }
8913
8914
8915
8916
8917 //-----------------------------------------------------------------------------
8918 // name: execute()
8919 // desc: ...
8920 //-----------------------------------------------------------------------------
8921 void Chuck_Instr_Init_Loop_Counter::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8922 {
8923 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
8924
8925 // pop the value
8926 pop_( sp, 1 );
8927
8928 // allocate counter
8929 t_CKUINT * p = shred->pushLoopCounter();
8930
8931 // copy it
8932 *p = (t_CKUINT)(*sp >= 0 ? *sp : -*sp);
8933 }
8934
8935
8936
8937
8938 //-----------------------------------------------------------------------------
8939 // name: execute()
8940 // desc: decrement top loop counter for shred
8941 //-----------------------------------------------------------------------------
8942 void Chuck_Instr_Dec_Loop_Counter::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8943 {
8944 // get topmost
8945 t_CKUINT * p = shred->currentLoopCounter();
8946
8947 // check
8948 if( p == NULL ) goto error;
8949
8950 // decrement
8951 (*p)--;
8952
8953 // done
8954 return;
8955
8956 error:
8957 // we have a problem
8958 EM_exception(
8959 "LoopCounterError: on line[%lu] in shred[id=%lu:%s]",
8960 m_linepos, shred->xid, shred->name.c_str() );
8961 goto done;
8962
8963 done:
8964 // do something!
8965 shred->is_running = FALSE;
8966 shred->is_done = TRUE;
8967 }
8968
8969
8970
8971
8972 //-----------------------------------------------------------------------------
8973 // name: execute()
8974 // desc: get top loop counter for shred
8975 //-----------------------------------------------------------------------------
8976 void Chuck_Instr_Reg_Push_Loop_Counter_Deref::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
8977 {
8978 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
8979
8980 // get topmost
8981 t_CKUINT * p = shred->currentLoopCounter();
8982
8983 // check
8984 if( p == NULL ) goto error;
8985
8986 // push
8987 push_( reg_sp, *p );
8988
8989 // done
8990 return;
8991
8992 error:
8993 // we have a problem
8994 EM_exception(
8995 "LoopCounterError: on line[%lu] in shred[id=%lu:%s]",
8996 m_linepos, shred->xid, shred->name.c_str() );
8997 goto done;
8998
8999 done:
9000 // do something!
9001 shred->is_running = FALSE;
9002 shred->is_done = TRUE;
9003 }
9004
9005
9006
9007
9008 //-----------------------------------------------------------------------------
9009 // name: execute()
9010 // desc: ...
9011 //-----------------------------------------------------------------------------
9012 void Chuck_Instr_Pop_Loop_Counter::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9013 {
9014 // pop counter
9015 shred->popLoopCounter();
9016 }
9017
9018
9019
9020
9021 //-----------------------------------------------------------------------------
9022 // name: struct Chuck_Instr_ForEach_Inc_And_Branch
9023 // desc: for( VAR : ARRAY ) increment VAR, test against ARRAY size; branch
9024 //
9025 // pre-condition: expects three items on reg stack (see below)
9026 // post-condition: leaves no values on reg stack
9027 //-----------------------------------------------------------------------------
9028 void Chuck_Instr_ForEach_Inc_And_Branch::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9029 {
9030 t_CKUINT *& reg_sp = (t_CKUINT *&)shred->reg->sp;
9031
9032 // --------
9033 // output of VAR alloc (e.g., int or float or other) -- but always an UINT
9034 // ARRAY to iterate over
9035 // implicit loop counter
9036 // --------
9037 // ^ assume three things on the reg stack
9038
9039 // compute stack offset
9040 // t_CKUINT offset = (m_dataSize + sz_INT + sz_INT) / sz_INT;
9041 t_CKUINT offset = (sz_INT + sz_INT + sz_INT) / sz_INT;
9042
9043 // pop
9044 pop_( reg_sp, offset );
9045 // set
9046 t_CKUINT *& sp = reg_sp;
9047
9048 // get var address
9049 t_CKUINT * pVar = (t_CKUINT *)(*sp);
9050 // get array pointer
9051 Chuck_Array * array = (Chuck_Array *)(*(sp+1));
9052 // loop counter pointer
9053 t_CKINT ** pCounter = (t_CKINT **)(sp+2);
9054 // counter
9055 t_CKINT * counter = *(pCounter);
9056
9057 // branch if either NULL array, or counter reached array size
9058 if( !array || val_(counter) >= array->size() )
9059 {
9060 shred->next_pc = m_jmp;
9061 }
9062 else
9063 {
9064 // set pVar to current element
9065 switch( m_dataKind )
9066 {
9067 case kindof_INT:
9068 {
9069 // cast to specific array type
9070 Chuck_ArrayInt * arr = (Chuck_ArrayInt *)array;
9071 // is object RELEASE
9072 if( arr->contains_objects() && *pVar)
9073 {
9074 // release ref, since for-each loops don't auto-release every iteration
9075 // (only at the end or if return encountered)
9076 ((Chuck_VM_Object *)(*pVar))->release();
9077 }
9078 // get element
9079 arr->get( *counter, pVar );
9080 // is object ADD_REF
9081 if( arr->contains_objects() && *pVar)
9082 {
9083 // add ref, as this will be cleaned up at end of scope, hopefully
9084 ((Chuck_VM_Object *)(*pVar))->add_ref();
9085 }
9086 break;
9087 }
9088 case kindof_FLOAT:
9089 {
9090 // cast to specific array type
9091 Chuck_ArrayFloat * arr = (Chuck_ArrayFloat *) array;
9092 // get element
9093 arr->get( *counter, (t_CKFLOAT *)pVar );
9094 break;
9095 }
9096 case kindof_VEC2:
9097 {
9098 // cast to specific array type
9099 Chuck_ArrayVec2 * arr = (Chuck_ArrayVec2 *) array;
9100 // get element
9101 arr->get( *counter, (t_CKVEC2 *)pVar );
9102 break;
9103 }
9104 case kindof_VEC3:
9105 {
9106 // cast to specific array type
9107 Chuck_ArrayVec3 * arr = (Chuck_ArrayVec3 *) array;
9108 // get element
9109 arr->get( *counter, (t_CKVEC3 *)pVar );
9110 break;
9111 }
9112 case kindof_VEC4:
9113 {
9114 // cast to specific array type
9115 Chuck_ArrayVec4 * arr = (Chuck_ArrayVec4 *) array;
9116 // get element
9117 arr->get( *counter, (t_CKVEC4 *)pVar );
9118 break;
9119 }
9120 default:
9121 {
9122 // shouldn't get here
9123 assert( FALSE );
9124 break;
9125 }
9126 }
9127
9128 // increment counter
9129 (*(*pCounter))++;
9130 }
9131 }
9132
9133
9134
9135
9136 #pragma mark === IO ===
9137
9138
9139
9140
9141 //-----------------------------------------------------------------------------
9142 // name: execute()
9143 // desc: ...
9144 //-----------------------------------------------------------------------------
9145 void Chuck_Instr_IO_in_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9146 {
9147 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9148
9149 // pop the value
9150 pop_( sp, 2 );
9151
9152 // ISSUE: 64-bit?
9153 // the IO
9154 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9155
9156 // check it
9157 if( *(ppIO) == NULL ) goto null_pointer;
9158
9159 // read into the variable
9160 **(t_CKINT **)(sp+1) = (*ppIO)->readInt( Chuck_IO::INT32 );
9161
9162 // push the IO
9163 push_( sp, (t_CKINT)(*(ppIO)) );
9164
9165 return;
9166
9167 null_pointer:
9168 // we have a problem
9169 EM_exception(
9170 "NullPointer: (IO input int) on line[%lu] in shred[id=%lu:%s]",
9171 m_linepos, shred->xid, shred->name.c_str() );
9172 goto done;
9173
9174 done:
9175 // do something!
9176 shred->is_running = FALSE;
9177 shred->is_done = TRUE;
9178 }
9179
9180
9181
9182
9183 //-----------------------------------------------------------------------------
9184 // name: execute()
9185 // desc: ...
9186 //-----------------------------------------------------------------------------
9187 void Chuck_Instr_IO_in_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9188 {
9189 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9190
9191 // issue: 64-bit
9192 // pop the value (fixed 1.3.0.0 -- changed from 3 to 2, note it's a float POINTER)
9193 pop_( sp, 2 );
9194
9195 // the IO
9196 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9197
9198 // check it
9199 if( *(ppIO) == NULL ) goto null_pointer;
9200
9201 // read into the variable
9202 **(t_CKFLOAT **)(sp+1) = (*ppIO)->readFloat();
9203
9204 // push the IO
9205 push_( sp, (t_CKINT)(*(ppIO)) );
9206
9207 return;
9208
9209 null_pointer:
9210 // we have a problem
9211 EM_exception(
9212 "NullPointer: (IO input float) on line[%lu] in shred[id=%lu:%s]",
9213 m_linepos, shred->xid, shred->name.c_str() );
9214 goto done;
9215
9216 done:
9217 // do something!
9218 shred->is_running = FALSE;
9219 shred->is_done = TRUE;
9220 }
9221
9222
9223
9224
9225 //-----------------------------------------------------------------------------
9226 // name: execute()
9227 // desc: ...
9228 //-----------------------------------------------------------------------------
9229 void Chuck_Instr_IO_in_string::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9230 {
9231 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9232
9233 // pop the value
9234 pop_( sp, 2 );
9235
9236 // issue: 64-bit
9237 // the IO
9238 Chuck_IO ** ppIO = (Chuck_IO **)sp;
9239 // the string
9240 Chuck_String ** ppStr = (Chuck_String **)(sp+1);
9241 std::string s; // = (*ppStr)->str();
9242
9243 // CK_FPRINTF_STDERR( "ppIO: 0x%08x\n", ppIO );
9244 // CK_FPRINTF_STDERR( "ppStr: 0x%08x\n", ppStr );
9245 // CK_FPRINTF_STDERR( "*ppStr: 0x%08x\n", *ppStr );
9246
9247 // check it
9248 if( *(ppIO) == NULL || *(ppStr) == NULL ) goto null_pointer;
9249
9250 // read into the variable
9251 (*ppIO)->readString( s );
9252 (*ppStr)->set( s );
9253
9254 // push the IO
9255 push_( sp, (t_CKINT)(*(ppIO)) );
9256
9257 return;
9258
9259 null_pointer:
9260 // we have a problem
9261 EM_exception(
9262 "NullPointer: (IO input string) on line[%lu] in shred[id=%lu:%s]",
9263 m_linepos, shred->xid, shred->name.c_str() );
9264 goto done;
9265
9266 done:
9267 // do something!
9268 shred->is_running = FALSE;
9269 shred->is_done = TRUE;
9270 }
9271
9272
9273
9274
9275 //-----------------------------------------------------------------------------
9276 // name: execute()
9277 // desc: insert int value into output stream
9278 //-----------------------------------------------------------------------------
9279 void Chuck_Instr_IO_out_int::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9280 {
9281 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9282
9283 // pop the value
9284 pop_( sp, 2 );
9285
9286 // issue: 64-bit
9287 // the IO
9288 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9289
9290 // check it
9291 if( *(ppIO) == NULL ) goto null_pointer;
9292
9293 // write the value
9294 (*ppIO)->write( *(sp+1) );
9295
9296 // push the IO
9297 push_( sp, (t_CKINT)(*(ppIO)) );
9298
9299 return;
9300
9301 null_pointer:
9302 // we have a problem
9303 EM_exception(
9304 "NullPointer: (IO output int) on line[%lu] in shred[id=%lu:%s]",
9305 m_linepos, shred->xid, shred->name.c_str() );
9306 goto done;
9307
9308 done:
9309 // do something!
9310 shred->is_running = FALSE;
9311 shred->is_done = TRUE;
9312 }
9313
9314
9315
9316
9317 //-----------------------------------------------------------------------------
9318 // name: execute()
9319 // desc: insert float value into output stream
9320 //-----------------------------------------------------------------------------
9321 void Chuck_Instr_IO_out_float::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9322 {
9323 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9324
9325 // pop the value
9326 pop_( sp, 1 + (sz_FLOAT / sz_INT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
9327
9328 // ISSUE: 64-bit
9329 // the IO
9330 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9331
9332 // check it
9333 if( *(ppIO) == NULL ) goto null_pointer;
9334
9335 // write the value
9336 (*ppIO)->write( *((t_CKFLOAT *)(sp+1)) );
9337
9338 // push the IO
9339 push_( sp, (t_CKINT)(*(ppIO)) );
9340
9341 return;
9342
9343 null_pointer:
9344 // we have a problem
9345 EM_exception(
9346 "NullPointer: (IO output float) on line[%lu] in shred[id=%lu:%s]",
9347 m_linepos, shred->xid, shred->name.c_str() );
9348 goto done;
9349
9350 done:
9351 // do something!
9352 shred->is_running = FALSE;
9353 shred->is_done = TRUE;
9354 }
9355
9356
9357
9358
9359 //-----------------------------------------------------------------------------
9360 // name: execute() | 1.5.1.0
9361 // desc: insert complex value into output stream
9362 //-----------------------------------------------------------------------------
9363 void Chuck_Instr_IO_out_complex::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9364 {
9365 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9366
9367 // pop the value
9368 pop_( sp, 1 + (sz_COMPLEX / sz_INT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
9369
9370 // ISSUE: 64-bit
9371 // the IO
9372 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9373
9374 // check it
9375 if( *(ppIO) == NULL ) goto null_pointer;
9376
9377 // write the value
9378 (*ppIO)->write( *((t_CKCOMPLEX *)(sp+1)) );
9379
9380 // push the IO
9381 push_( sp, (t_CKINT)(*(ppIO)) );
9382
9383 return;
9384
9385 null_pointer:
9386 // we have a problem
9387 EM_exception(
9388 "NullPointer: (IO output complex) on line[%lu] in shred[id=%lu:%s]",
9389 m_linepos, shred->xid, shred->name.c_str() );
9390 goto done;
9391
9392 done:
9393 // do something!
9394 shred->is_running = FALSE;
9395 shred->is_done = TRUE;
9396 }
9397
9398
9399
9400
9401 //-----------------------------------------------------------------------------
9402 // name: execute() | 1.5.1.0
9403 // desc: insert complex value into output stream
9404 //-----------------------------------------------------------------------------
9405 void Chuck_Instr_IO_out_polar::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9406 {
9407 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9408
9409 // pop the value
9410 pop_( sp, 1 + (sz_POLAR / sz_INT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
9411
9412 // ISSUE: 64-bit
9413 // the IO
9414 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9415
9416 // check it
9417 if( *(ppIO) == NULL ) goto null_pointer;
9418
9419 // write the value
9420 (*ppIO)->write( *((t_CKPOLAR *)(sp+1)) );
9421
9422 // push the IO
9423 push_( sp, (t_CKINT)(*(ppIO)) );
9424
9425 return;
9426
9427 null_pointer:
9428 // we have a problem
9429 EM_exception(
9430 "NullPointer: (IO output polar) on line[%lu] in shred[id=%lu:%s]",
9431 m_linepos, shred->xid, shred->name.c_str() );
9432 goto done;
9433
9434 done:
9435 // do something!
9436 shred->is_running = FALSE;
9437 shred->is_done = TRUE;
9438 }
9439
9440
9441
9442
9443 //-----------------------------------------------------------------------------
9444 // name: execute() | 1.5.1.7
9445 // desc: insert vec2 value into output stream
9446 //-----------------------------------------------------------------------------
9447 void Chuck_Instr_IO_out_vec2::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9448 {
9449 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9450
9451 // pop the value
9452 pop_( sp, 1 + (sz_VEC2 / sz_INT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
9453
9454 // ISSUE: 64-bit
9455 // the IO
9456 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9457
9458 // check it
9459 if( *(ppIO) == NULL ) goto null_pointer;
9460
9461 // write the value
9462 (*ppIO)->write( *((t_CKVEC2 *)(sp+1)) );
9463
9464 // push the IO
9465 push_( sp, (t_CKINT)(*(ppIO)) );
9466
9467 return;
9468
9469 null_pointer:
9470 // we have a problem
9471 EM_exception(
9472 "NullPointer: (IO output vec2) on line[%lu] in shred[id=%lu:%s]",
9473 m_linepos, shred->xid, shred->name.c_str() );
9474 goto done;
9475
9476 done:
9477 // do something!
9478 shred->is_running = FALSE;
9479 shred->is_done = TRUE;
9480 }
9481
9482
9483
9484
9485 //-----------------------------------------------------------------------------
9486 // name: execute() | 1.5.1.0
9487 // desc: insert vec3 value into output stream
9488 //-----------------------------------------------------------------------------
9489 void Chuck_Instr_IO_out_vec3::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9490 {
9491 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9492
9493 // pop the value
9494 pop_( sp, 1 + (sz_VEC3 / sz_INT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
9495
9496 // ISSUE: 64-bit
9497 // the IO
9498 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9499
9500 // check it
9501 if( *(ppIO) == NULL ) goto null_pointer;
9502
9503 // write the value
9504 (*ppIO)->write( *((t_CKVEC3 *)(sp+1)) );
9505
9506 // push the IO
9507 push_( sp, (t_CKINT)(*(ppIO)) );
9508
9509 return;
9510
9511 null_pointer:
9512 // we have a problem
9513 EM_exception(
9514 "NullPointer: (IO output vec3) on line[%lu] in shred[id=%lu:%s]",
9515 m_linepos, shred->xid, shred->name.c_str() );
9516 goto done;
9517
9518 done:
9519 // do something!
9520 shred->is_running = FALSE;
9521 shred->is_done = TRUE;
9522 }
9523
9524
9525
9526
9527 //-----------------------------------------------------------------------------
9528 // name: execute() | 1.5.1.0
9529 // desc: insert vec4 value into output stream
9530 //-----------------------------------------------------------------------------
9531 void Chuck_Instr_IO_out_vec4::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9532 {
9533 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9534
9535 // pop the value
9536 pop_( sp, 1 + (sz_VEC4 / sz_INT) ); // ISSUE: 64-bit (fixed 1.3.1.0)
9537
9538 // ISSUE: 64-bit
9539 // the IO
9540 Chuck_IO **& ppIO = (Chuck_IO **&)sp;
9541
9542 // check it
9543 if( *(ppIO) == NULL ) goto null_pointer;
9544
9545 // write the value
9546 (*ppIO)->write( *((t_CKVEC4 *)(sp+1)) );
9547
9548 // push the IO
9549 push_( sp, (t_CKINT)(*(ppIO)) );
9550
9551 return;
9552
9553 null_pointer:
9554 // we have a problem
9555 EM_exception(
9556 "NullPointer: (IO output vec4) on line[%lu] in shred[id=%lu:%s]",
9557 m_linepos, shred->xid, shred->name.c_str() );
9558 goto done;
9559
9560 done:
9561 // do something!
9562 shred->is_running = FALSE;
9563 shred->is_done = TRUE;
9564 }
9565
9566
9567
9568
9569 //-----------------------------------------------------------------------------
9570 // name: execute()
9571 // desc: insert string into output stream
9572 //-----------------------------------------------------------------------------
9573 void Chuck_Instr_IO_out_string::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9574 {
9575 t_CKINT *& sp = (t_CKINT *&)shred->reg->sp;
9576
9577 // pop the value
9578 pop_( sp, 2 );
9579
9580 // ISSUE: 64-bit
9581 // the IO
9582 Chuck_IO ** ppIO = (Chuck_IO **)sp;
9583 // the string
9584 Chuck_String ** ppStr = (Chuck_String **)(sp+1);
9585
9586 // check it
9587 if( *(ppIO) == NULL || *(ppStr) == NULL ) goto null_pointer;
9588
9589 // write the variable
9590 (*ppIO)->write( (*ppStr)->str().c_str() );
9591
9592 // push the IO
9593 push_( sp, (t_CKINT)(*(ppIO)) );
9594
9595 return;
9596
9597 null_pointer:
9598 // we have a problem
9599 EM_exception(
9600 "NullPointer: (IO output string) on line[%lu] in shred[id=%lu:%s]",
9601 m_linepos, shred->xid, shred->name.c_str() );
9602 goto done;
9603
9604 done:
9605 // do something!
9606 shred->is_running = FALSE;
9607 shred->is_done = TRUE;
9608 }
9609
9610
9611
9612
9613 // hack
9614 Chuck_Instr_Hack::Chuck_Instr_Hack( Chuck_Type * type )
9615 {
9616 this->m_type_ref = type;
9617 // this->m_type_ref->add_ref();
9618 }
9619
9620 Chuck_Instr_Hack::~Chuck_Instr_Hack()
9621 {
9622 // this->m_type_ref->release();
9623 }
9624
9625 void Chuck_Instr_Hack::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9626 {
9627 // look at the type (1.3.1.0: added iskindofint)
9628 if( m_type_ref->size == sz_INT && iskindofint(vm->env(), m_type_ref) ) // ISSUE: 64-bit (fixed 1.3.1.0)
9629 {
9630 t_CKINT * sp = (t_CKINT *)shred->reg->sp;
9631 Chuck_Object * obj = ((Chuck_Object *)*(sp-1));
9632 if( !isa( m_type_ref, vm->env()->ckt_string ) || *(sp-1) == 0 )
9633 {
9634 // print it
9635 if( isa( m_type_ref, vm->env()->ckt_object ) )
9636 {
9637 if( obj == NULL )
9638 {
9639 CK_FPRINTF_STDERR( "null :(%s)\n", m_type_ref->c_name() );
9640 }
9641 else
9642 {
9643 CK_FPRINTF_STDERR( "0x%lx :(%s|refcount=%d)\n", *(sp-1), m_type_ref->c_name(), obj->m_ref_count );
9644 }
9645 }
9646 else
9647 CK_FPRINTF_STDERR( "%ld :(%s)\n", *(sp-1), m_type_ref->c_name() );
9648 }
9649 else
9650 {
9651 CK_FPRINTF_STDERR( "\"%s\" :(%s)\n", ((Chuck_String *)obj)->str().c_str(), m_type_ref->c_name() );
9652 }
9653 }
9654 else if( m_type_ref->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
9655 {
9656 t_CKFLOAT * sp = (t_CKFLOAT *)shred->reg->sp;
9657 // print it
9658 CK_FPRINTF_STDERR( "%f :(%s)\n", *(sp-1), m_type_ref->c_name() );
9659 }
9660 else if( m_type_ref->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0)
9661 {
9662 if( m_type_ref->xid == te_complex )
9663 {
9664 t_CKFLOAT * sp = (t_CKFLOAT *)shred->reg->sp;
9665 // print it
9666 CK_FPRINTF_STDERR( "#(%.4f,%.4f) :(%s)\n", *(sp-2), *(sp-1), m_type_ref->c_name() );
9667 }
9668 else if( m_type_ref->xid == te_polar )
9669 {
9670 t_CKFLOAT * sp = (t_CKFLOAT *)shred->reg->sp;
9671 // print it
9672 CK_FPRINTF_STDERR( "%%(%.4f,%.4f*pi) :(%s)\n", *(sp-2), *(sp-1)/CK_ONE_PI, m_type_ref->c_name() );
9673 }
9674 else if( m_type_ref->xid == te_vec2 ) // 1.5.1.7
9675 {
9676 t_CKFLOAT * sp = (t_CKFLOAT *)shred->reg->sp;
9677 // print it
9678 CK_FPRINTF_STDERR( "@(%.4f,%.4f) :(%s)\n", *(sp-2), *(sp-1), m_type_ref->c_name() );
9679 }
9680 else
9681 {
9682 CK_FPRINTF_STDERR( "[chuck]: internal error printing 16-word primitive...\n" );
9683 }
9684 }
9685 else if( m_type_ref->size == sz_VEC3 ) // 1.3.5.3
9686 {
9687 if( m_type_ref->xid == te_vec3 )
9688 {
9689 t_CKFLOAT * sp = (t_CKFLOAT *)shred->reg->sp;
9690 // print it
9691 CK_FPRINTF_STDERR( "@(%.4f,%.4f,%.4f) :(%s)\n", *(sp-3), *(sp-2), *(sp-1), m_type_ref->c_name() );
9692 }
9693 else
9694 {
9695 CK_FPRINTF_STDERR( "[chuck]: internal error printing 24-word primitive...\n" );
9696 }
9697 }
9698 else if( m_type_ref->size == sz_VEC4 ) // 1.3.5.3
9699 {
9700 if( m_type_ref->xid == te_vec4 )
9701 {
9702 t_CKFLOAT * sp = (t_CKFLOAT *)shred->reg->sp;
9703 // print it
9704 CK_FPRINTF_STDERR( "@(%.4f,%.4f,%.4f,%.4f) :(%s)\n", *(sp-4), *(sp-3), *(sp-2), *(sp-1), m_type_ref->c_name() );
9705 }
9706 else
9707 {
9708 CK_FPRINTF_STDERR( "[chuck]: internal error printing 32-word primitive...\n" );
9709 }
9710 }
9711 else if( m_type_ref->size == 0 )
9712 {
9713 CK_FPRINTF_STDERR( "... :(%s)\n", m_type_ref->c_name() );
9714 }
9715 else
9716 assert( FALSE );
9717
9718 // flush
9719 CK_FFLUSH_STDERR();
9720 }
9721
9722 const char * Chuck_Instr_Hack::params() const
9723 {
9724 static char buffer[CK_PRINT_BUF_LENGTH];
9725 snprintf( buffer, CK_PRINT_BUF_LENGTH, "(%s)", m_type_ref->c_name() );
9726 return buffer;
9727 }
9728
9729
9730
9731
9732 // gack
9733 Chuck_Instr_Gack::Chuck_Instr_Gack( const std::vector<Chuck_Type *> & types )
9734 {
9735 m_type_refs = types;
9736
9737 // add refs
9738 }
9739
9740 Chuck_Instr_Gack::~Chuck_Instr_Gack()
9741 {
9742 // release refs
9743 }
9744
9745 void Chuck_Instr_Gack::execute( Chuck_VM * vm, Chuck_VM_Shred * shred )
9746 {
9747 t_CKBYTE * the_sp = (t_CKBYTE *)shred->reg->sp;
9748
9749 if( m_type_refs.size() == 1 )
9750 {
9751 Chuck_Instr_Hack hack( m_type_refs[0] );
9752 hack.execute( vm, shred );
9753 return;
9754 }
9755
9756 // loop over types
9757 t_CKUINT i;
9758
9759 // find the start of the expression
9760 for( i = 0; i < m_type_refs.size(); i++ )
9761 {
9762 Chuck_Type * type = m_type_refs[i];
9763 the_sp -= type->size;
9764 }
9765
9766 // print
9767 for( i = 0; i < m_type_refs.size(); i++ )
9768 {
9769 Chuck_Type * type = m_type_refs[i];
9770
9771 // look at the type (1.3.1.0: added is kindofint)
9772 if( type->size == sz_INT && iskindofint(vm->env(), type) ) // ISSUE: 64-bit (fixed 1.3.1.0)
9773 {
9774 t_CKINT * sp = (t_CKINT *)the_sp;
9775 if( !isa( type, vm->env()->ckt_string ) || *(sp) == 0 )
9776 {
9777 if( isa( type, vm->env()->ckt_object ) )
9778 {
9779 Chuck_Object * obj = ((Chuck_Object *)(*sp));
9780 // print it
9781 if( *(sp) == 0 )
9782 CK_FPRINTF_STDERR( "null " );
9783 else
9784 CK_FPRINTF_STDERR( "0x%lx :(%s|refcount=%d) ", *(sp), type->c_name(), obj->m_ref_count );
9785 // CK_FPRINTF_STDERR( "0x%lx (refcount=%d) ", *(sp), obj->m_ref_count );
9786 }
9787 else
9788 {
9789 // print it
9790 CK_FPRINTF_STDERR( "%ld ", *(sp) );
9791 }
9792 }
9793 else
9794 {
9795 Chuck_String * str = ((Chuck_String *)*(sp));
9796 CK_FPRINTF_STDERR( "%s ", str->str().c_str() );
9797 }
9798
9799 the_sp += sz_INT; // ISSUE: 64-bit (fixed 1.3.1.0)
9800 }
9801 else if( type->size == sz_FLOAT ) // ISSUE: 64-bit (fixed 1.3.1.0)
9802 {
9803 t_CKFLOAT * sp = (t_CKFLOAT *)the_sp;
9804 // print it
9805 CK_FPRINTF_STDERR( "%f ", *(sp) );
9806
9807 the_sp += sz_FLOAT; // ISSUE: 64-bit (fixed 1.3.1.0)
9808 }
9809 else if( type->size == sz_VEC2 ) // ISSUE: 64-bit (fixed 1.3.1.0)
9810 {
9811 t_CKFLOAT * sp = (t_CKFLOAT *)the_sp;
9812 if( type->xid == te_complex )
9813 // print it
9814 CK_FPRINTF_STDERR( "#(%.4f,%.4f) ", *(sp), *(sp+1) );
9815 else if( type->xid == te_polar )
9816 // print it
9817 CK_FPRINTF_STDERR( "%%(%.4f,%.4f*pi) ", *(sp), *(sp+1)/CK_ONE_PI );
9818 else if( type->xid == te_vec2 ) // 1.5.1.7
9819 // print it
9820 CK_FPRINTF_STDERR( "@(%.4f,%.4f) ", *(sp), *(sp+1) );
9821
9822 the_sp += sz_VEC2; // ISSUE: 64-bit (fixed 1.3.1.0)
9823 }
9824 else if( type->size == sz_VEC3 ) // 1.3.5.3
9825 {
9826 t_CKFLOAT * sp = (t_CKFLOAT *)the_sp;
9827 if( type->xid == te_vec3 )
9828 // print it
9829 CK_FPRINTF_STDERR( "#(%.4f,%.4f,%.4f) ", *(sp), *(sp+1), *(sp+2) );
9830
9831 the_sp += sz_VEC3; // ISSUE: 64-bit (fixed 1.3.1.0)
9832 }
9833 else if( type->size == sz_VEC4 ) // 1.3.5.3
9834 {
9835 t_CKFLOAT * sp = (t_CKFLOAT *)the_sp;
9836 if( type->xid == te_vec4 )
9837 // print it
9838 CK_FPRINTF_STDERR( "#(%.4f,%.4f,%.4f,%.4f) ", *(sp), *(sp+1), *(sp+2), *(sp+3) );
9839
9840 the_sp += sz_VEC4; // ISSUE: 64-bit (fixed 1.3.1.0)
9841 }
9842 else if( type->size == 0 )
9843 {
9844 CK_FPRINTF_STDERR( "..." );
9845 }
9846 else
9847 assert( FALSE );
9848 }
9849
9850 CK_FPRINTF_STDERR( "\n" );
9851
9852 // flush
9853 CK_FFLUSH_STDERR();
9854 }
9855
9856 const char * Chuck_Instr_Gack::params() const
9857 {
9858 static char buffer[CK_PRINT_BUF_LENGTH];
9859 snprintf( buffer, CK_PRINT_BUF_LENGTH, "( many types )" );
9860 return buffer;
9861 }
9862
9863
9864 // actions to throw exception
9865 void ck_throw_exception( Chuck_VM_Shred * shred, const char * name )
9866 {
9867 // we have a problem
9868 EM_exception(
9869 "%s: on line[%lu] in shred[id=%lu:%s]",
9870 name, shred->instr[shred->pc]->m_linepos, shred->xid, shred->name.c_str() ); //, shred->pc );
9871 // do something!
9872 shred->is_running = FALSE;
9873 shred->is_done = TRUE;
9874 }
9875
9876
9877 // actions to throw exception with int
9878 void ck_throw_exception( Chuck_VM_Shred * shred, const char * name, t_CKINT desc )
9879 {
9880 // we have a problem
9881 EM_exception(
9882 "%s: '%li' on line[%lu] in shred[id=%lu:%s]",
9883 name, desc, shred->instr[shred->pc]->m_linepos, shred->xid, shred->name.c_str() ); //, shred->pc );
9884 // do something!
9885 shred->is_running = FALSE;
9886 shred->is_done = TRUE;
9887 }
9888
9889
9890 // actions to throw exception with float
9891 void ck_throw_exception( Chuck_VM_Shred * shred, const char * name, t_CKFLOAT desc )
9892 {
9893 // we have a problem
9894 EM_exception(
9895 "%s: '%f' on line[%lu] in shred[id=%lu:%s]",
9896 name, desc, shred->instr[shred->pc]->m_linepos, shred->xid, shred->name.c_str() ); //, shred->pc );
9897 // do something!
9898 shred->is_running = FALSE;
9899 shred->is_done = TRUE;
9900 }
9901
9902 // actions to throw exception with string desc
9903 void ck_throw_exception( Chuck_VM_Shred * shred, const char * name, const char * desc )
9904 {
9905 // we have a problem
9906 EM_exception(
9907 "%s: %s on line[%lu] in shred[id=%lu:%s]",
9908 name, desc, shred->instr[shred->pc]->m_linepos, shred->xid, shred->name.c_str() ); //, shred->pc );
9909 // do something!
9910 shred->is_running = FALSE;
9911 shred->is_done = TRUE;
9912 }