File: vm_run.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (1976 lines) | stat: -rw-r--r-- 52,761 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
/*
   FALCON - The Falcon Programming Language.
   FILE: vm_run.cpp

   Implementation of virtual machine - main loop
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: 2004-09-08

   -------------------------------------------------------------------
   (C) Copyright 2004: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

/** \file
   Virtual machine main loop.

   The main loop of the virtual machine deserves a space on its own.
*/

#include <falcon/vm.h>
#include <falcon/pcodes.h>
#include <falcon/vmcontext.h>
#include <falcon/sys.h>
#include <falcon/core_ext.h>
#include <falcon/sequence.h>

#include <falcon/coreobject.h>
#include <falcon/lineardict.h>
#include <falcon/string.h>
#include <falcon/cclass.h>
#include <falcon/carray.h>
#include <falcon/coredict.h>
#include <falcon/corefunc.h>
#include <falcon/error.h>
#include <falcon/stream.h>
#include <falcon/membuf.h>
#include <falcon/vmmsg.h>
#include <falcon/vmevent.h>
#include <falcon/rangeseq.h>
#include <falcon/generatorseq.h>
#include <falcon/garbagepointer.h>

#include <math.h>
#include <errno.h>
#include <string.h>

namespace Falcon {


Item *VMachine::getOpcodeParam( register uint32 bc_pos )
{
   Item *ret;

   // Load the operator ?
   switch( m_currentContext->code()[ m_currentContext->pc() + bc_pos ]  )
   {
      case P_PARAM_INT32:
         m_imm[bc_pos].setInteger( *reinterpret_cast<int32 *>( m_currentContext->code() + m_currentContext->pc_next() ) );
         m_currentContext->pc_next() += sizeof( int32 );
      return m_imm + bc_pos;

      case P_PARAM_INT64:
         m_imm[bc_pos].setInteger( loadInt64( m_currentContext->code() + m_currentContext->pc_next()  ) );
         m_currentContext->pc_next() += sizeof( int64 );
      return m_imm + bc_pos;

      case P_PARAM_STRID:
         {
            String *temp = currentLiveModule()->getString( *reinterpret_cast<int32 *>( m_currentContext->code() + m_currentContext->pc_next() ) );
            //m_imm[bc_pos].setString( temp, const_cast<LiveModule*>(currentLiveModule()) );
            m_imm[bc_pos].setString( temp );
            m_currentContext->pc_next() += sizeof( int32 );
         }
      return m_imm + bc_pos;

      case P_PARAM_LBIND:
         m_imm[bc_pos].setLBind( currentLiveModule()->getString(
            *reinterpret_cast<int32 *>( m_currentContext->code() + m_currentContext->pc_next() ) ) );
         m_currentContext->pc_next() += sizeof( int32 );
      return m_imm + bc_pos;

      case P_PARAM_NUM:
         m_imm[bc_pos].setNumeric( loadNum( m_currentContext->code() + m_currentContext->pc_next() ) );
         m_currentContext->pc_next() += sizeof( numeric );
      return m_imm + bc_pos;

      case P_PARAM_NIL:
         m_imm[bc_pos].setNil();
      return m_imm + bc_pos;

      case P_PARAM_UNB:
         m_imm[bc_pos].setUnbound();
      return m_imm + bc_pos;

      case P_PARAM_GLOBID:
      {
         register int32 id = *reinterpret_cast< int32 * >( m_currentContext->code() + m_currentContext->pc_next() );
         m_currentContext->pc_next()+=sizeof( int32 );
         return &moduleItem( id );
      }
      break;

      case P_PARAM_LOCID:
         ret = local( *reinterpret_cast< int32 * >( m_currentContext->code() + m_currentContext->pc_next() ) );
         m_currentContext->pc_next()+=sizeof(int32);
      return ret;

      case P_PARAM_PARID:
         ret = param( *reinterpret_cast< int32 * >( m_currentContext->code() + m_currentContext->pc_next() ) );
         m_currentContext->pc_next()+=sizeof(int32);
      return ret;

      case P_PARAM_TRUE:
         m_imm[bc_pos].setBoolean( true );
      return m_imm + bc_pos;

      case P_PARAM_FALSE:
         m_imm[bc_pos].setBoolean( false );
      return m_imm + bc_pos;

      case P_PARAM_NTD32: m_currentContext->pc_next() += sizeof(int32); return 0;
      case P_PARAM_NTD64: m_currentContext->pc_next() += sizeof(int64); return 0;
      case P_PARAM_REGA: return &regA();
      case P_PARAM_REGB: return &regB();
      case P_PARAM_REGS1: return &self();
      case P_PARAM_REGL1: return &latch();
      case P_PARAM_REGL2: return &latcher();      
      case P_PARAM_FSELF: return &m_currentContext->fself();
   }

   // we should not be here.
   fassert( false );
   return 0;
}


void VMachine::run()
{
   tOpcodeHandler *ops = m_opHandlers;

   // declare this as the running machine
   setCurrent();

   while( ! m_break )
   {
      try {
         // move m_currentContext->pc_next() to the end of instruction and beginning of parameters.
         // in case of opcode in the call_request range, this will move next pc to return.
         m_currentContext->pc_next() = m_currentContext->pc() + sizeof( uint32 );

         // external call required?
         if ( m_currentContext->pc() >= i_pc_call_request )
         {

            switch( m_currentContext->pc() )
            {
               case i_pc_call_external_ctor_return:
                  regA() = self();
                  //fallthrough
               case i_pc_call_external_return:
                  callReturn();
               break;

               // the request was just to ignore opcode
               case i_pc_redo_request:
                  if ( m_opCount )
                     m_opCount--; // prevent hitting oplimit
               break;

               default:
                  regA().setNil();
                  currentSymbol()->getExtFuncDef()->call( this );
            }
         }
         else
         {
            ops[ m_currentContext->code()[ m_currentContext->pc() ] ]( this );
         }

         m_opCount ++;

         //=========================
         // Executes periodic checks
         //

         if ( m_opCount >= m_opNextCheck )
         {
            // By default, if nothing else happens, we should do a check no sooner than this.
            m_opNextCheck += FALCON_VM_DFAULT_CHECK_LOOPS;

            // manage periodic callbacks
            periodicChecks();
         }

         // Jump to next isntruction.
         m_currentContext->pc() = m_currentContext->pc_next();
      }
      // catches explicitly raised items.
      catch( Item& raised )
      {
         handleRaisedItem( raised );
      }
      // errors thrown by C extensions;
      // they may get encapsulated in Error() instances and handled by the script
      // via handleRaised
      catch( Error *err )
      {
         handleRaisedError( err );
      }
   } // end while -- VM LOOP

   m_break = false;
}

/****************************************************
   Op code handlers
*****************************************************/

// 0
void opcodeHandler_END( register VMachine *vm )
{
   vm->regA().setNil();
   vm->terminateCurrentContext();
}


// 1
void opcodeHandler_NOP( register VMachine *vm )
{
}

// 2
void opcodeHandler_PSHN( register VMachine *vm )
{
   vm->stack().append( Item() );
}

// 3
void opcodeHandler_RET( register VMachine *vm )
{
   vm->retnil();
   vm->callReturn();
}

// 4
void opcodeHandler_RETA( register VMachine *vm )
{
   vm->callReturn();

   //? Check this -- I don't think is anymore necessary
   if( vm->regA().type() == FLC_ITEM_REFERENCE )
      vm->regA().copy( vm->regA().asReference()->origin() );
}

// 5
void opcodeHandler_PTRY( register VMachine *vm )
{
   register int32 target = vm->getNextNTD32();
   while( target > 0 )
   {
      vm->popTry( false );
      --target;
   }
}


// 6
void opcodeHandler_LNIL( register VMachine *vm )
{
   register Item *op1 = vm->getOpcodeParam( 1 )->dereference();
   op1->setNil();
}

// 7
void opcodeHandler_RETV(register VMachine *vm)
{
   vm->regA() = *vm->getOpcodeParam( 1 )->dereference();
   vm->callReturn();
}

// 8
void opcodeHandler_BOOL( register VMachine *vm )
{
   vm->regA().setBoolean( vm->getOpcodeParam( 1 )->dereference()->isTrue() );
}

// 9
void opcodeHandler_JMP( register VMachine *vm )
{
   vm->m_currentContext->pc_next() = vm->getNextNTD32();
}

// 0A
void opcodeHandler_GENA( register VMachine *vm )
{
   register uint32 size = (uint32) vm->getNextNTD32();
   CoreArray *array = new CoreArray( size );
   vm->regA().setArray( array );

   // copy the m-topmost items in the stack into the array
   if( size > 0 )
   {
      array->items().copyOnto( vm->stack(), vm->stack().length() - size,  size );
      vm->currentFrame()->pop( size );
   }
}

// 0B
void opcodeHandler_GEND( register VMachine *vm )
{
   register uint32 length = (uint32) vm->getNextNTD32();
   LinearDict *dict = new LinearDict( length );

   // copy the m-topmost items in the stack into the array
   uint32 len =  vm->stack().length();
   uint32 base = len - ( length * 2 );
   for ( uint32 i = base ; i < len; i += 2 ) {
      // insert may modify the stack (if using special "compare" functions)
      Item i1 = vm->stackItem(i);
      Item i2 = vm->stackItem(i+1);
      dict->put( i1, i2 );
      fassert( vm->stack().length() == len );
   }
   vm->stack().resize( base );
   vm->regA().setDict( new CoreDict(dict) );
}


// 0C
void opcodeHandler_PUSH( register VMachine *vm )
{
   /** \TODO Raise a stack overflow error on VM stack boundary limit. */
   Item *data = vm->getOpcodeParam( 1 )->dereference();
   if ( data->isFutureBind() )
   {
      // with this marker, the next call operation will search its parameters.
      // Let's consider this a temporary (but legitimate) hack.
      vm->regBind().flags( 0xF0 );
   }
   vm->stack().append( *data );
}

// 0D
void opcodeHandler_PSHR( register VMachine *vm )
{
   Item *referenced = vm->getOpcodeParam( 1 );
   if ( ! referenced->isReference() )
   {
      GarbageItem *ref = new GarbageItem( *referenced );
      referenced->setReference( ref );
   }

   vm->stack().append( *referenced );
}


// 0E
void opcodeHandler_POP( register VMachine *vm )
{
   if ( vm->stack().length() == 0 ) {
      vm->raiseHardError( e_stackuf, "POP", __LINE__ );
      return;
   }
   //  --- WARNING: do not dereference!
   vm->getOpcodeParam( 1 )->copy( vm->stack().back() );
   vm->stack().resize( vm->stack().length() - 1);
}


// 0F
void opcodeHandler_INC( register VMachine *vm )
{
   Item *operand =  vm->getOpcodeParam( 1 );
   operand->inc(vm->regA());
}

// 10
void opcodeHandler_DEC( register VMachine *vm )
{
   Item *operand =  vm->getOpcodeParam( 1 );
   operand->dec(vm->regA());
}


// 11
void opcodeHandler_NEG( register VMachine *vm )
{
   Item *operand = vm->getOpcodeParam( 1 );
   operand->neg( vm->regA() );
}


// 12
void opcodeHandler_NOT( register VMachine *vm )
{
   vm->regA().setInteger( vm->getOpcodeParam( 1 )->dereference()->isTrue() ? 0 : 1 );
}


//13
void opcodeHandler_TRAL( register VMachine *vm )
{
   if ( vm->stack().length() < 1 ) {
      vm->raiseHardError( e_stackuf, "TRAL", __LINE__ );
   }

   uint32 loop_last = vm->getNextNTD32();

   const Item &top = vm->stack().back();
   fassert( top.isGCPointer() );

   Iterator* iter = dyncast<Iterator*>(top.asGCPointer());
   // is this the last element?
   if (  ! iter->hasNext() )
   {
      if( iter->hasCurrent() )
         iter->next(); // position past last element

      vm->jump( loop_last );
   }
}



//14
void opcodeHandler_IPOP( register VMachine *vm )
{
   register uint32 amount = (uint32) vm->getNextNTD32();
   if ( vm->stack().length() < amount ) {
      vm->raiseHardError( e_stackuf, "IPOP", __LINE__ );
      return;
   }

   vm->stack().resize( vm->stack().length() - amount );
}

//15
void opcodeHandler_XPOP( register VMachine *vm )
{
   Item *operand = vm->getOpcodeParam( 1 )->dereference();
   // use copy constructor.
   Item itm( *operand );
   operand->copy( vm->stack()[vm->stack().length() - 1] );
   vm->stack()[vm->stack().length() - 1].copy( itm );
}

//16
void opcodeHandler_GEOR( register VMachine *vm )
{
   vm->regA().setRange(
      new CoreRange( (int32) vm->getOpcodeParam( 1 )->dereference()->forceIntegerEx() ) );
}

//17
void opcodeHandler_TRY( register VMachine *vm )
{
   vm->pushTry( vm->getNextNTD32() );
}

//18
void opcodeHandler_JTRY( register VMachine *vm )
{
   register int32 target = vm->getNextNTD32();

   vm->popTry( false );  // underflows are checked here
   vm->m_currentContext->pc_next() = target;
}

//19
void opcodeHandler_RIS( register VMachine *vm )
{
   // todo - when this will be in the main loop,
   // we may use directly handleRaisedEvent()
   throw *vm->getOpcodeParam( 1 )->dereference();
}

//1A
void opcodeHandler_BNOT( register VMachine *vm )
{
   register Item *operand = vm->getOpcodeParam( 1 )->dereference();
   if ( operand->type() == FLC_ITEM_INT ) {
      vm->regA().setInteger( ~operand->asInteger() );
   }
   else
      throw new TypeError( ErrorParam( e_bitwise_op ).extra("BNOT").origin( e_orig_vm ) );
}

//1B
void opcodeHandler_NOTS( register VMachine *vm )
{
   register Item *operand1 = vm->getOpcodeParam( 1 )->dereference();

   if ( operand1->isOrdinal() )
      operand1->setInteger( ~operand1->forceInteger() );
   else
      throw new TypeError( ErrorParam( e_bitwise_op ).extra("NOTS").origin( e_orig_vm ) );
}

//1c
void opcodeHandler_PEEK( register VMachine *vm )
{
   register Item *operand = vm->getOpcodeParam( 1 )->dereference();

   if ( vm->stack().length() == 0 ) {
      vm->raiseHardError( e_stackuf, "PEEK", __LINE__ );
      return;
   }
   *operand = vm->stack().back();
}

// 1D
void opcodeHandler_FORK( register VMachine *vm )
{
   uint32 pSize = (uint32) vm->getNextNTD32();
   uint32 pJump = (uint32) vm->getNextNTD32();

   // create the coroutine
   vm->putAtSleep( vm->coPrepare( pSize ) );

   // fork
   vm->m_currentContext->pc_next() = pJump;
}

// 1D - Missing

// 1E
void opcodeHandler_LD( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   /*
   if( operand1->isLBind() && operand1->asLBind()->getCharAt(0) != '.' )
   {
      vm->bindItem( *operand1->asLBind(), *operand2 );
      vm->regA() =  *operand2;
   }
   else
   {
      switch( operand2->type() )
      {
         case FLC_ITEM_STRING:
            operand1->setString( new CoreString( *operand2->asString() ) );
            operand1->flags( operand2->flags() );
            break;

         case FLC_ITEM_LBIND:
            vm->unbindItem( *operand2->asLBind(), *operand1 );
            break;

         default:
            operand1->copy( *operand2 );
      }

      vm->regA() =  *operand1;
   }
   */

   if ( operand2->isString() )
   {
      operand1->setString( new CoreString( *operand2->asString() ) );
      operand1->flags( operand2->flags() );
   }
   else
      operand1->copy( *operand2 );

   vm->regA() =  *operand1;
}

// 1F
void opcodeHandler_LDRF( register VMachine *vm )
{
   // don't dereference
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );

   if( operand2 == &vm->m_imm[2] )
      operand1->setNil(); // breaks the reference
   else {
      // we don't use memPool::referenceItem for performace reasons
      GarbageItem *gitem;
      if ( operand2->isReference() )
      {
         gitem = operand2->asReference();
      }
      else
      {
         gitem = new GarbageItem( *operand2 );
         operand2->setReference( gitem );
      }

      operand1->setReference( gitem );
   }
}

// 20
void opcodeHandler_ADD( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 );
   Item *operand2 = vm->getOpcodeParam( 2 );

   Item target; // neutralize auto-ops
   operand1->add( *operand2, target );
   vm->regA() = target;
}

// 21
void opcodeHandler_SUB( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );

   Item target; // neutralize auto-ops
   operand1->sub( *operand2, target );
   vm->regA() = target;
}

// 22
void opcodeHandler_MUL( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   operand1->mul( *operand2, vm->regA() );
}

// 23
void opcodeHandler_DIV( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   operand1->div( *operand2, vm->regA() );
}


//24
void opcodeHandler_MOD( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   operand1->mod( *operand2, vm->regA() );
}

// 25
void opcodeHandler_POW( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   operand1->pow( *operand2, vm->regA() );
}

// 26
void opcodeHandler_ADDS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 );

   // TODO: S-operators
   operand1->add( *operand2, *operand1 );
   vm->regA() = *operand1;
}

//27
void opcodeHandler_SUBS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 );

   // TODO: S-operators
   operand1->sub( *operand2, *operand1 );
   vm->regA() = *operand1;
}


//28

void opcodeHandler_MULS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 );

   // TODO: S-operators
   operand1->mul( *operand2, *operand1 );
   vm->regA() = *operand1;
}

//29
void opcodeHandler_DIVS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 );

   // TODO: S-operators
   operand1->div( *operand2, *operand1 );
   vm->regA() = *operand1;
}


//2A
void opcodeHandler_MODS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 );

   // TODO: S-operators
   operand1->mod( *operand2, *operand1 );
   vm->regA() = *operand1;
}

//2B
void opcodeHandler_BAND( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( operand1->isOrdinal() && operand2->isOrdinal() ) {
      vm->regA().setInteger( operand1->forceInteger() & operand2->forceInteger() );
   }
   else
      throw new TypeError( ErrorParam( e_invop ).extra("BAND").origin( e_orig_vm ) );
}

//2C
void opcodeHandler_BOR( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( operand1->isOrdinal() && operand2->isOrdinal() ) {
      vm->regA().setInteger( operand1->forceInteger() | operand2->forceInteger() );
   }
   else
      throw new TypeError( ErrorParam( e_invop ).extra("BOR").origin( e_orig_vm ) );
}

//2D
void opcodeHandler_BXOR( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( operand1->isOrdinal() && operand2->isOrdinal() ) {
      vm->regA().setInteger( operand1->forceInteger() ^ operand2->forceInteger() );
   }
   else
      throw new TypeError( ErrorParam( e_invop ).extra("BXOR").origin( e_orig_vm ) );
}

//2E
void opcodeHandler_ANDS( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( operand1->isOrdinal() && operand2->isOrdinal() ) {
      operand1->setInteger( operand1->forceInteger() & operand2->forceInteger() );
   }
   else
      throw new TypeError( ErrorParam( e_invop ).extra("ANDS").origin( e_orig_vm ) );
}

//2F
void opcodeHandler_ORS( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( operand1->type() == FLC_ITEM_INT && operand2->type() == FLC_ITEM_INT ) {
      operand1->setInteger( operand1->asInteger() | operand2->asInteger() );
   }
   else
      throw new TypeError( ErrorParam( e_invop ).extra("ORS").origin( e_orig_vm ) );
}

//30
void opcodeHandler_XORS( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( operand1->isOrdinal() && operand2->isOrdinal() ) {
      operand1->setInteger( operand1->forceInteger() ^ operand2->forceInteger() );
   }
   else
      throw new TypeError( ErrorParam( e_invop ).extra("XORS").origin( e_orig_vm ) );
}

//31
void opcodeHandler_GENR( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();
   Item *operand3 =  vm->getOpcodeParam( 3 )->dereference();


   int64 secondOp = operand2->forceIntegerEx();

   int64 step;
   if ( operand3->isNil() )
   {
      step = vm->stack()[ vm->stack().length() - 1].forceIntegerEx();
      vm->stack().resize( vm->stack().length() - 1);
   }
   else {
      step = operand3->forceIntegerEx();
   }


   int64 firstOp = operand1->forceIntegerEx();

   if( operand2->isOob() && firstOp <= secondOp )
      secondOp++;

   vm->regA().setRange( new CoreRange(
      firstOp,
      secondOp,
      step   ) );

}

//32
void opcodeHandler_EQ( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->regA().setBoolean( *operand1 == *operand2 );
}

//33
void opcodeHandler_NEQ( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->regA().setBoolean( *operand1 != *operand2 );
}

//34
void opcodeHandler_GT( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->regA().setBoolean( *operand1 > *operand2 );
}

//35
void opcodeHandler_GE( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->regA().setBoolean( *operand1 >= *operand2 );
}

//36
void opcodeHandler_LT( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->regA().setBoolean( *operand1 < *operand2 );
}

//37
void opcodeHandler_LE( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->regA().setBoolean( *operand1 <= *operand2 );
}

//38
void opcodeHandler_IFT( register VMachine *vm )
{
   uint32 pNext = (uint32) vm->getNextNTD32();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if( operand2->isTrue() )
      vm->m_currentContext->pc_next() = pNext;
}

//39
void opcodeHandler_IFF( register VMachine *vm )
{
   uint32 pNext = (uint32) vm->getNextNTD32();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if( ! operand2->isTrue() )
      vm->m_currentContext->pc_next() = pNext;
}

//3A
void opcodeHandler_CALL( register VMachine *vm )
{
   uint32 pNext = (uint32) vm->getNextNTD32();
   Item *operand2 = vm->getOpcodeParam( 2 )->dereference();

   operand2->readyFrame( vm, pNext );
}

//3B
void opcodeHandler_INST( register VMachine *vm )
{
   uint32 pNext = (uint32) vm->getNextNTD32();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if( operand2->type() != FLC_ITEM_CLASS )
   {
      throw new TypeError( ErrorParam( e_invop ).extra("INST").origin( e_orig_vm ) );
   }

   Item method = operand2->asClass()->constructor();
   if ( ! method.isNil() )
   {
      // set self in the function method.
      method.methodize( vm->self() );
      method.readyFrame( vm, pNext );
   }
}

//3C
void opcodeHandler_ONCE( register VMachine *vm )
{
   uint32 pNext = (uint32) vm->getNextNTD32();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   const Symbol *call = 0;

   if ( operand2->isFunction() )
   {
      call = operand2->asFunction()->symbol();
   }
   else if ( operand2->isMethod() && operand2->asMethodFunc()->isFunc() )
   {
      call = static_cast<CoreFunc*>(operand2->asMethodFunc())->symbol();
   }

   if ( call != 0 && call->isFunction() )
   {
      // we suppose we're in the same module as the function things we are...
      register uint32 itemId = call->getFuncDef()->onceItemId();
      if ( vm->moduleItem( itemId ).isNil() )
         vm->moduleItem( itemId ).setInteger( 1 );
      else
         vm->m_currentContext->pc_next() = pNext;
      return;
   }

   vm->raiseHardError( e_invop, "ONCE", __LINE__ );
}

//3D
void opcodeHandler_LDV( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->latch() = *operand1;
   vm->latcher() = *operand2;

   operand1->getIndex( *operand2, vm->regA() );
}

//3E
void opcodeHandler_LDP( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();
   vm->latch() = *operand1;
   vm->latcher() = *operand2;

   if( operand2->isString() )
   {
      String *property = operand2->asString();
      operand1->getProperty( *property, vm->regA() );
   }
   else
      throw
         new AccessError( ErrorParam( e_prop_acc ).origin( e_orig_vm ) .
            extra( operand2->isString() ? *operand2->asString() : "?" ) );
}


// 3F
void opcodeHandler_TRAN( register VMachine *vm )
{
   if ( vm->stack().length() < 1 ) {
      vm->raiseHardError( e_stackuf, "TRAN", __LINE__ );
   }

   uint32 loop_begin = vm->getNextNTD32();
   uint32 varCount = vm->getNextNTD32();
   const Item &top = vm->stack().back();
   fassert( top.isGCPointer() );

   Iterator* iter = dyncast<Iterator*>(top.asGCPointer());
   if( iter->isValid() && iter->next() )
   {
      // Ok, we proceeded to the next item; prepare the vars
      vm->expandTRAV( varCount, *iter );
      vm->currentContext()->pc_next() = loop_begin;
   }
   else
   {
      vm->currentContext()->pc_next() += sizeof(uint32) * 2 * varCount;
   }
}

//40
void opcodeHandler_LDAS( register VMachine *vm )
{
   uint32 size = (uint32) vm->getNextNTD32();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if( operand2->isArray() )
   {
      CoreArray* arr = operand2->asArray();
      if( arr->length() != size )
      {
         throw
            new AccessError( ErrorParam( e_unpack_size )
               .extra( "LDAS" )
               .origin( e_orig_vm ) );
      }
      else {
         uint32 oldpos = vm->stack().length();
         vm->stack().resize( oldpos + size );
         void* mem = &vm->stack()[ oldpos ];;
         memcpy( mem, arr->items().elements(), size * sizeof(Item) );
     }
   }
   else {
      throw
            new AccessError( ErrorParam( e_invop )
               .extra( "LDAS" )
               .origin( e_orig_vm ) );
   }
}

//41
void opcodeHandler_SWCH( register VMachine *vm )
{
   uint32 pNext = (uint32) vm->getNextNTD32();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();
   uint64 sw_count = (uint64) vm->getNextNTD64();

   byte *tableBase =  vm->m_currentContext->code() + vm->m_currentContext->pc_next();

   uint16 sw_int = (int16) (sw_count >> 48);
   uint16 sw_rng = (int16) (sw_count >> 32);
   uint16 sw_str = (int16) (sw_count >> 16);
   uint16 sw_obj = (int16) sw_count;

   //determine the value type to be checked
   switch( operand2->type() )
   {
      case FLC_ITEM_NIL:
         if ( *reinterpret_cast<uint32 *>( tableBase ) != 0xFFFFFFFF ) {
            vm->m_currentContext->pc_next() = *reinterpret_cast<uint32 *>( tableBase );
            return;
         }
      break;

      case FLC_ITEM_INT:
         if ( sw_int > 0 &&
               vm->seekInteger( operand2->asInteger(), tableBase + sizeof(uint32), sw_int, vm->m_currentContext->pc_next() ) )
            return;
         if ( sw_rng > 0 &&
               vm->seekInRange( operand2->asInteger(),
                     tableBase + sizeof(uint32) + sw_int * (sizeof(uint64) + sizeof(uint32) ),
                     sw_rng, vm->m_currentContext->pc_next() ) )
            return;
      break;

      case FLC_ITEM_NUM:
         if ( sw_int > 0 &&
               vm->seekInteger( operand2->forceInteger(), tableBase + sizeof(uint32), sw_int, vm->m_currentContext->pc_next() ) )
            return;
         if ( sw_rng > 0 &&
               vm->seekInRange( operand2->forceInteger(),
                     tableBase + sizeof(uint32) + sw_int * (sizeof(uint64) + sizeof(uint32) ),
                     sw_rng, vm->m_currentContext->pc_next() ) )
            return;
      break;

      case FLC_ITEM_STRING:
         if ( sw_str > 0 &&
               vm->seekString( operand2->asString(),
                  tableBase + sizeof(uint32) +
                     sw_int * (sizeof(uint64) + sizeof(uint32) )+
                     sw_rng * (sizeof(uint64) + sizeof(uint32) ),
                     sw_str,
                     vm->m_currentContext->pc_next() ) )
            return;
      break;
   }

   // always tries with the objects, if given and had not success up to here.
   if ( sw_obj > 0 &&
         vm->seekItem( operand2,
            tableBase + sizeof(uint32) +
               sw_int * (sizeof(uint64) + sizeof(uint32) ) +
               sw_rng * (sizeof(uint64) + sizeof(uint32) ) +
               sw_str * (sizeof(uint32) + sizeof(uint32) ),
               sw_obj,
               vm->m_currentContext->pc_next() ) )
            return;

   // in case of failure...
   vm->m_currentContext->pc_next() = pNext;
}


//46
void opcodeHandler_IN( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   bool result = false;

   switch( operand2->type() )
   {
      case FLC_ITEM_STRING:
         if( operand1->type() == FLC_ITEM_STRING )
            result = operand2->asString()->find( *operand1->asString() ) != csh::npos;
      break;

      case FLC_ITEM_ARRAY:
      {
         Item *elements =  operand2->asArray()->items().elements();
         for( uint32 pos = 0; pos <operand2->asArray()->length(); pos++ )
         {
            if( elements[pos] == *operand1 ) {
               result = true;
               break;
            }
         }
      }
      break;

      case FLC_ITEM_DICT:
      {
         result = operand2->asDict()->find( *operand1 ) != 0;
      }
      break;

      case FLC_ITEM_OBJECT:
      {
         // is the object embedding a dictionary interface?
         Sequence* seq = operand2->asObjectSafe()->getSequence();
         if( seq != 0 && seq->isDictionary() )
         {
            result = static_cast<ItemDict*>(seq)->find(*operand1) != 0 ;
         }
         else
         {
            if( operand1->type() == FLC_ITEM_STRING )
               result = operand2->asObjectSafe()->hasProperty( *operand1->asString() );
         }
      }
      break;

      case FLC_ITEM_CLASS:
      {
         if( operand1->type() == FLC_ITEM_STRING )
         {
            uint32 pos;
            result = operand2->asClass()->properties().findKey( *operand1->asString(), pos ) != 0;
         }
      }
      break;
   }

   vm->regA().setBoolean( result );
}

//47
void opcodeHandler_NOIN( register VMachine *vm )
{
   // do not decode operands; IN will do it
   opcodeHandler_IN( vm );
   vm->regA().setBoolean( ! vm->regA().asBoolean() );
}

//48
void opcodeHandler_PROV( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( operand2->type() != FLC_ITEM_STRING ) {
      vm->raiseHardError( e_invop, "PROV", __LINE__  ); // hard error, as the string should be created by the compiler
      return;
   }

   bool result;
   uint32 pos;
   switch ( operand1->type() ) {
      case FLC_ITEM_CLASS:
         result = operand1->asClass()->properties().findKey( *operand2->asString(), pos ) != 0;
      break;

      case FLC_ITEM_OBJECT:
         result = operand1->asObjectSafe()->hasProperty( *operand2->asString() );
      break;

      case FLC_ITEM_ARRAY:
         result = operand1->asArray()->getProperty( *operand2->asString() )!=0;
      break;
      case FLC_ITEM_DICT:
         result = operand1->asDict()->isBlessed() &&
                  operand1->asDict()->find( *operand2->asString() )!=0;
      break;
      default:
         result = false;
   }

   vm->regA().setBoolean( result );
}



//49
void opcodeHandler_STVS( register VMachine *vm )
{
   if(  vm->stack().empty() )
   {
      vm->raiseHardError( e_stackuf, "STVS", __LINE__  );
      return;
   }

   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   Item *origin = &vm->stack()[vm->stack().length() - 1];

   operand1->setIndex( *operand2, *origin );
   vm->regA() = *origin;
   vm->stack().resize( vm->stack().length() - 1);

}

//4A
void opcodeHandler_STPS( register VMachine *vm )
{
   if( vm->stack().empty() )
   {
      vm->raiseHardError( e_stackuf, "STPS", __LINE__  );
      return;
   }

   Item *target =  vm->getOpcodeParam( 1 );
   Item *method = vm->getOpcodeParam( 2 )->dereference();

   if ( method->isString() )
   {
      target->setProperty( *method->asString(), vm->stack()[vm->stack().length() - 1] );
      vm->regA() = vm->stack()[vm->stack().length() - 1];
      vm->stack().resize( vm->stack().length() - 1 );
   }
   else
   {
      vm->stack().resize( vm->stack().length() - 1 );
      throw new TypeError( ErrorParam( e_prop_acc ).extra("STPS").origin( e_orig_vm ) );
   }
}

//4B
void opcodeHandler_AND( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   vm->regA().setBoolean( operand1->isTrue() && operand2->isTrue() );
}

//4C
void opcodeHandler_OR( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   vm->regA().setBoolean( operand1->isTrue() || operand2->isTrue() );
}

//50
void opcodeHandler_STV( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 );
   Item *operand2 = vm->getOpcodeParam( 2 );
   Item *origItm = vm->getOpcodeParam( 3 );
   Item *origin = origItm->dereference();

   operand1->setIndex( *operand2, *origin );

   if( origItm != &vm->regB() )
         vm->regA() = *origin;
}

//51
void opcodeHandler_STP( register VMachine *vm )
{
   Item *target = vm->getOpcodeParam( 1 );
   Item *method = vm->getOpcodeParam( 2 )->dereference();
   Item *sourcend = vm->getOpcodeParam( 3 );
   Item *source = sourcend->dereference();


   if ( method->isString() )
   {
      target->setProperty( *method->asString(), *source );

      // when B is the source, the right value is already in A.
      if( sourcend != &vm->regB() )
         vm->regA() = *source;
      return;
   }

   throw new TypeError( ErrorParam( e_invop ).extra("STP").origin( e_orig_vm ) );

/**
   \todo check this code
   This code originally allowed to load a method of an object into a method
   of another object. This is considered now too danjerous, as external
   methods can rely on how the object is built. So, it's fine to set a
   function into a method, but it's not fine to extract a method or
   to exchange methods between classes.

   An alternative could be that to force methods to check for user
   data signature, but that was exactly what I wanted to avoid when
   putting user data in objects.

   Giancarlo Niccolai

   \code

            if( source->type() == FLC_ITEM_METHOD ) {
               // will instantiate by value
               if( target->asObject()->setProperty( vm->m_op2->asString(), source->asMethodFunction() ) )
                  return;
            }
            else if ( source->type() == FLC_ITEM_EXTMETHOD )
            {
               Item itm;
               itm.setExtFunc(source->asMethodExtFunc());
               if( target->asObject()->setProperty( vm->m_op2->asString(), itm ) )
                  return;
            }
            else {
               if( target->asObject()->setProperty( vm->m_op2->asString(), *source ) )
                  return;
            }
         }
   \endcode
*/
}

//52
void opcodeHandler_LDVT( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->latch() = *operand1;
   vm->latcher() = *operand2;

   operand1->getIndex( *operand2, *vm->getOpcodeParam( 3 ) );
}

//53
void opcodeHandler_LDPT( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();
   vm->latch() = *operand1;
   vm->latcher() = *operand2;

   if( operand2->isString() )
   {
      String *property = operand2->asString();
      operand1->getProperty( *property, *vm->getOpcodeParam( 3 ) );
   }
   else
      throw
         new AccessError( ErrorParam( e_prop_acc ).origin( e_orig_vm ) .
            extra( operand2->isString() ? *operand2->asString() : "?" ) );
}

//54
void opcodeHandler_STVR( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 );
   Item *operand2 = vm->getOpcodeParam( 2 );

   // do not deref op3
   Item *origin = vm->getOpcodeParam( 3 );

   //STV counts as an assignment, we must copy to expression value.
   if( origin != &vm->regA())
      vm->regA() = *origin;

   GarbageItem *gitem;
   if( ! origin->isReference() )
   {
      gitem = new GarbageItem( *origin );
      origin->setReference( gitem );
   }

   operand1->setIndex( *operand2, *origin );
}

//55
void opcodeHandler_STPR( register VMachine *vm )
{
   Item *target = vm->getOpcodeParam( 1 );
   Item *method = vm->getOpcodeParam( 2 )->dereference();

   Item *source = vm->getOpcodeParam( 3 );
   vm->regA() = *source;

   if ( method->isString() )
   {
      target->setProperty( *method->asString(), *source );
      return;
   }

   throw new TypeError( ErrorParam( e_prop_acc ).extra("STPR").origin( e_orig_vm ) );
}

//56
void opcodeHandler_TRAV( register VMachine *vm )
{

   // get the jump label.
   uint32 wayout = (uint32) vm->getNextNTD32();
   // get the number of variables in the trav loop
   uint32 varcount = (uint32) vm->getNextNTD32();
   Item *source = vm->getOpcodeParam( 3 )->dereference();

   // our sequence:
   Sequence *seq = 0;

   switch( source->type() )
   {
   case FLC_ITEM_ARRAY:
      seq = &source->asArray()->items();
      break;

   case FLC_ITEM_DICT:
      seq = &source->asDict()->items();
      break;

   case FLC_ITEM_OBJECT:
      seq = source->asObjectSafe()->getSequence();
      // is the object a direct sequence?
      if( seq == 0 )
      {
         // is it a callable entity?
         if ( source->asObjectSafe()->hasProperty( OVERRIDE_OP_CALL ))
         {
            seq = new GeneratorSeq( vm, *source );
            GarbagePointer* ptr = new GarbagePointer( seq );
            seq->owner( ptr );
         }
         else
            // we have mess on the stack, but it doesn't matter here.
            throw new TypeError( ErrorParam( e_invop ).extra("TRAV").origin( e_orig_vm ) );
      }
      break;


   case FLC_ITEM_RANGE:
      // optimize: avoid creating a bit of stuff if not needed
      if( source->asRangeIsOpen() ||
          (source->asRangeEnd() == source->asRangeStart() && source->asRangeStep() == 0) ||
          ( source->asRangeStep() > 0 && source->asRangeStart() > source->asRangeEnd() ) ||
          ( source->asRangeStep() < 0 && source->asRangeStart() < source->asRangeEnd() )
         )
      {
         vm->jump( wayout );
         return;
      }

      // ok, the range can generate a sequence. Hence...
      try {
         seq = new RangeSeq( *source->asRange() );
         // also, save the sequence in a garbage sensible item.
         // we don't record anywhere this item, but it will be marked
         // as long as we have the iterator active in the stack.
         GarbagePointer* ptr = new GarbagePointer( seq );
         seq->owner( ptr );
         seq->gcMark( vm->generation() ); // will mark our pointer
      }
      catch( ... ) {
         fassert( false ); // must not throw
      }
      break;

   case FLC_ITEM_NIL:
      // jump out
      vm->jump( wayout );
      return;


   default:
      if( source->isCallable() )
      {
         seq = new GeneratorSeq( vm, *source );
         GarbagePointer* ptr = new GarbagePointer( seq );
         seq->owner( ptr );
      }
      else
      {
         throw new TypeError( ErrorParam( e_invop ).extra("TRAV").origin( e_orig_vm ) );
      }
   }

   // empty sequence?
   if ( seq == 0 || seq->empty() )
   {
      vm->jump(wayout);
      return;
   }

   // create the iterator and push it.
   Item iterItem;
   Iterator* current = new Iterator( seq );
   iterItem.setGCPointer( current );
   vm->pushParam( iterItem );

   // the source is safe through back-marking of the iterator on its sequence,
   // and of the sequence on its owner.

   // now distribute the current value of the element on the variables.
   vm->expandTRAV( varcount, *current );
}


//57
void opcodeHandler_INCP( register VMachine *vm )
{
   Item *operand =  vm->getOpcodeParam( 1 )->dereference();
   Item temp;
   operand->incpost( temp );

   vm->regB() = *operand;
   vm->regA() = temp;
}

//58
void opcodeHandler_DECP( register VMachine *vm )
{
   Item *operand =  vm->getOpcodeParam( 1 )->dereference();
   Item temp;
   operand->decpost( temp );

   vm->regB() = *operand;
   vm->regA() = temp;
}

//59
void opcodeHandler_SHL( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 )->dereference();

   if( operand1->isInteger() && operand2->isInteger() )
      vm->regA().setInteger( operand1->asInteger() << operand2->asInteger() );
   else
      throw
         new TypeError( ErrorParam( e_invop ).origin( e_orig_vm ).extra("SHL") );

}

//5A
void opcodeHandler_SHR( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 )->dereference();

   if( operand1->isInteger() && operand2->isInteger() )
      vm->regA().setInteger( operand1->asInteger() >> operand2->asInteger() );
   else
      throw
         new TypeError( ErrorParam( e_invop ).origin( e_orig_vm ).extra("SHR") );
}

//5B
void opcodeHandler_SHLS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 )->dereference();

   if( operand1->isInteger() && operand2->isInteger() )
      operand1->setInteger( operand1->asInteger() << operand2->asInteger() );
   else
      throw
         new TypeError( ErrorParam( e_invop ).origin( e_orig_vm ).extra("SHLS") );
}

//5C
void opcodeHandler_SHRS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 )->dereference();

   if( operand1->isInteger() && operand2->isInteger() )
      operand1->setInteger( operand1->asInteger() >> operand2->asInteger() );
   else
      throw
         new TypeError( ErrorParam( e_invop ).origin( e_orig_vm ).extra("SHRS") );
}

//5D
void opcodeHandler_CLOS( register VMachine *vm )
{
   uint32 size = (uint32) vm->getNextNTD32();
   Item *tgt = vm->getOpcodeParam( 2 )->dereference();
   Item *src = vm->getOpcodeParam( 3 )->dereference();

   fassert( src->isFunction() );
   *tgt = new CoreFunc( *src->asFunction() );

   if( size > 0 )
   {
      if ( size > vm->stack().length() )
      {
         throw
            new CodeError( ErrorParam( e_stackuf ).origin( e_orig_vm ).extra("CLOS") );
      }

      ItemArray* closure = new ItemArray( size );
      Item *data = closure->elements();
      int32 base = vm->stack().length() - size;
      memcpy( data, &vm->stack()[ base ], sizeof(Item)*size );
      closure->length( size );
      vm->stack().resize( base );

      tgt->asFunction()->closure( closure );
   }
}

// 5D
void opcodeHandler_PSHL( register VMachine *vm )
{
   vm->stack().append( *vm->getOpcodeParam( 1 ) );
}


// 5E
void opcodeHandler_POWS( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 );

   // TODO: S-operators
   operand1->pow( *operand2, *operand1 );
   vm->regA() = *operand1;
}

//5F
void opcodeHandler_LSB( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   Item *operand2 = vm->getOpcodeParam( 2 )->dereference();

   switch( operand1->type() << 8 | operand2->type() )
   {
      case FLC_ITEM_STRING << 8 | FLC_ITEM_INT:
      case FLC_ITEM_STRING << 8 | FLC_ITEM_NUM:
      {
         int32 pos = (int32) operand2->forceInteger();
         String *cs = operand1->asString();
         if ( cs->checkPosBound( pos ) )
            vm->retval( (int64) cs->getCharAt( pos ) );
         else
            throw
               new AccessError( ErrorParam( e_arracc ).origin( e_orig_vm ).extra( "LSB" ) );
      }
      return;
   }

   throw
      new TypeError( ErrorParam( e_invop ).origin( e_orig_vm ).extra( "LSB" ) );
}

// 60
void opcodeHandler_EVAL( register VMachine *vm )
{
   // We know the first operand must be a string
   Item *operand1 =  vm->getOpcodeParam( 1 )->dereference();

   if ( operand1->isArray() )
   {
      CoreArray *arr = operand1->asArray();
      if ( arr->length() > 0 ) {
         // fake as if we were called by a function
         // This will cause functionalEval to produce a correct return frame
         // in case it needs sub functional evals.
         vm->createFrame(0);
         uint32 savePC = vm->m_currentContext->pc_next();
         vm->m_currentContext->pc_next() = VMachine::i_pc_call_external_return;
         if( ! vm->functionalEval( *operand1 ) )
         {
            // it wasn't necessary; reset pc to the correct value
            vm->callReturn();
            vm->m_currentContext->pc_next() = savePC;
         }
         // ok here we're ready either to jump where required by functionalEval or
         // to go on as usual
         return;
      }
   }
   else if ( operand1->isCallable() ) {
      vm->callFrame( *operand1, 0 );
      return;
   }

   // by default, just copy the operand
   vm->regA() = *operand1;
}

//61
void opcodeHandler_SELE( register VMachine *vm )
{
   uint32 pNext = (uint32) vm->getNextNTD32();
   Item *real_op2 = vm->getOpcodeParam( 2 );
   Item *op2 = real_op2->dereference();
   uint64 sw_count = (uint64) vm->getNextNTD64();

   byte *tableBase = vm->m_currentContext->code() + vm->m_currentContext->pc_next();

   // SELE B has a special semantic used as a TRY/CATCH helper
   bool hasDefault = *reinterpret_cast<uint32 *>( tableBase )  == 0;
   bool reRaise = real_op2 == &vm->regB();

   // In select we use only integers and object fields.
   uint16 sw_int = (int16) (sw_count >> 48);
   uint16 sw_obj = (int16) sw_count;

   //determine the value type to be checked
   int type = op2->type();
   if ( sw_int > 0 )
   {
      if ( type == FLC_ITEM_INT )
         type = FLC_ITEM_NUM;

      if ( vm->seekInteger( type, tableBase + sizeof(uint32), sw_int, vm->m_currentContext->pc_next() ) )
         return;
   }

   if (sw_obj > 0 )
   {
      // we have to search the symbol of the class of our object
      // always tries with the objects, if given and had not success up to here.
      if ( vm->seekItemClass( op2,
               tableBase + sizeof(uint32) +
                  sw_int * (sizeof(uint64) + sizeof(uint32) ),
                  sw_obj,
                  vm->m_currentContext->pc_next() ) )
               return;
   }

   if ( reRaise && hasDefault ) // in case of re-raise
   {
      throw vm->regB();
   }

   // in case of failure go to default or past sele...
   vm->m_currentContext->pc_next() = pNext;
}

//62
void opcodeHandler_INDI( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();

   if ( ! operand1->isString() )
   {
      throw new TypeError( ErrorParam( e_invop ).extra("INDI").origin( e_orig_vm ) );
   }

   if ( ! vm->findLocalVariable( *operand1->asString(), vm->regA() ) )
   {
       throw
            new ParamError( ErrorParam( e_param_indir_code ).origin( e_orig_vm ).extra( "INDI" ) );
   }
}

//63
void opcodeHandler_STEX( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();

   if ( ! operand1->isString() )
   {
      throw new TypeError( ErrorParam( e_invop ).extra("STEX").origin( e_orig_vm ) );
   }

   CoreString *target = new CoreString;
   String *src = operand1->asString();

   VMachine::returnCode retval = vm->expandString( *src, *target );
   switch( retval )
   {
      case VMachine::return_ok:
         vm->regA() = target;
      break;

      case VMachine::return_error_parse_fmt:
         throw
            new TypeError( ErrorParam( e_fmt_convert ).origin( e_orig_vm ).extra( "STEX-format" ) );
      break;

      case VMachine::return_error_string:
         throw
            new ParamError( ErrorParam( e_param_strexp_code ).origin( e_orig_vm ).extra( "STEX-string" ) );
      break;

      case VMachine::return_error_parse:
         throw
               new ParamError( ErrorParam( e_param_indir_code ).origin( e_orig_vm ).extra( "STEX-parse" ) );
      break;

      default:  // warning no-op
         return;
   }
}

//64
void opcodeHandler_TRAC( register VMachine *vm )
{
   if ( vm->stack().length() < 1 ) {
      vm->raiseHardError( e_stackuf, "TRAC", __LINE__ );
   }

   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();
   const Item &top = vm->stack().back();
   fassert( top.isGCPointer() );
   Iterator* iter = dyncast<Iterator*>(top.asGCPointer());

   if( operand1->isString() )
      iter->getCurrent() = new CoreString( *operand1->asString() );
   else
      iter->getCurrent() = *operand1;
}

//65
void opcodeHandler_WRT( register VMachine *vm )
{
   Item *operand1 = vm->getOpcodeParam( 1 )->dereference();

   Stream *stream = vm->stdOut();
   if ( stream == 0 )
   {
      vm->raiseHardError( e_invop, "WRT", __LINE__  );
      return;
   }

   if( operand1->isString() )
   {
      String *s = operand1->asString();
      stream->writeString( *s );
   }
   else {
      String temp;
      vm->itemToString( temp, operand1 );
      stream->writeString( temp );
   }

   stream->flush();
}


void opcodeHandler_STO( register VMachine *vm )
{
   // STO is like LD, but it doesn't dereference param 1.
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   /*if ( operand2->isString() )
      operand1->setString( new CoreString( *operand2->asString() ) );
   else*/
      operand1->copy( *operand2 );

   vm->regA() = *operand1;
}

// 0x67
void opcodeHandler_FORB( register VMachine *vm )
{
   // We know the first operand must be a string
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 )->dereference();

   if ( ! operand1->isString() )
   {
      vm->raiseHardError( e_invop, "FORB", __LINE__  );
      return;
   }

   vm->regA().setLBind( new CoreString( *operand1->asString() ),
      new GarbageItem( *operand2 ) );
}

// 0x68
void opcodeHandler_OOB( register VMachine *vm )
{
   uint32 pmode = (uint32) vm->getNextNTD32();
   Item *operand =  vm->getOpcodeParam( 2 )->dereference();
   switch( pmode )
   {
      case 0:
         vm->regA() = *operand;
         vm->regA().setOob( false );
         break;

      case 1:
         vm->regA() = *operand;
         vm->regA().setOob( true );
         break;

      case 2:
         vm->regA() = *operand;
         vm->regA().setOob( ! operand->isOob() );
         break;

      default:
         vm->regA().setBoolean( operand->isOob() );
   }
}

// 0x69
void opcodeHandler_TRDN( register VMachine *vm )
{
   if ( vm->stack().length() < 1 ) {
      vm->raiseHardError( e_stackuf, "TRAC", __LINE__ );
   }

   uint32 loop_begin = vm->getNextNTD32();
   uint32 loop_out = vm->getNextNTD32();
   uint32 vars = vm->getNextNTD32();

   const Item &top = vm->stack().back();
   fassert( top.isGCPointer() );
   Iterator* iter = dyncast<Iterator*>(top.asGCPointer());

   if( vars == 0 )
   {
      fassert( iter->hasPrev() );
      iter->prev();
      iter->erase();
      vm->jump( loop_begin ); // actually, the "out" pointer
   }
   else
   {
      // a normal TRDN
      iter->erase();
      if( iter->hasCurrent() )
      {
         vm->expandTRAV( vars, *iter );
         vm->jump( loop_begin );
      }
      else
         vm->jump( loop_out );
   }
}

// 0x70
void opcodeHandler_EXEQ( register VMachine *vm )
{
   Item *operand1 =  vm->getOpcodeParam( 1 );
   Item *operand2 =  vm->getOpcodeParam( 2 );
   vm->regA().setBoolean( operand1->exactlyEqual(*operand2) );
}
   

}

/* end of vm_run.cpp */