summaryrefslogtreecommitdiffstats
path: root/gst/goom/goomsl.c
blob: b07b2596db98a532ededce3bbe33030a40c4e5f2 (plain)
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
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "goomsl.h"
#include "goomsl_private.h"
#include "goomsl_yacc.h"

/*#define TRACE_SCRIPT*/

 /* {{{ definition of the instructions number */
#define INSTR_SETI_VAR_INTEGER     1
#define INSTR_SETI_VAR_VAR         2
#define INSTR_SETF_VAR_FLOAT       3
#define INSTR_SETF_VAR_VAR         4
#define INSTR_NOP                  5
/* #define INSTR_JUMP              6 */
#define INSTR_SETP_VAR_PTR         7
#define INSTR_SETP_VAR_VAR         8
#define INSTR_SUBI_VAR_INTEGER     9
#define INSTR_SUBI_VAR_VAR         10
#define INSTR_SUBF_VAR_FLOAT       11
#define INSTR_SUBF_VAR_VAR         12
#define INSTR_ISLOWERF_VAR_VAR     13
#define INSTR_ISLOWERF_VAR_FLOAT   14
#define INSTR_ISLOWERI_VAR_VAR     15
#define INSTR_ISLOWERI_VAR_INTEGER 16
#define INSTR_ADDI_VAR_INTEGER     17
#define INSTR_ADDI_VAR_VAR         18
#define INSTR_ADDF_VAR_FLOAT       19
#define INSTR_ADDF_VAR_VAR         20
#define INSTR_MULI_VAR_INTEGER     21
#define INSTR_MULI_VAR_VAR         22
#define INSTR_MULF_VAR_FLOAT       23
#define INSTR_MULF_VAR_VAR         24
#define INSTR_DIVI_VAR_INTEGER     25
#define INSTR_DIVI_VAR_VAR         26
#define INSTR_DIVF_VAR_FLOAT       27
#define INSTR_DIVF_VAR_VAR         28
/* #define INSTR_JZERO             29 */
#define INSTR_ISEQUALP_VAR_VAR     30
#define INSTR_ISEQUALP_VAR_PTR     31
#define INSTR_ISEQUALI_VAR_VAR     32
#define INSTR_ISEQUALI_VAR_INTEGER 33
#define INSTR_ISEQUALF_VAR_VAR     34
#define INSTR_ISEQUALF_VAR_FLOAT   35
/* #define INSTR_CALL              36 */
/* #define INSTR_RET               37 */
/* #define INSTR_EXT_CALL          38 */
#define INSTR_NOT_VAR              39
/* #define INSTR_JNZERO            40 */
#define  INSTR_SETS_VAR_VAR        41
#define  INSTR_ISEQUALS_VAR_VAR    42
#define  INSTR_ADDS_VAR_VAR        43
#define  INSTR_SUBS_VAR_VAR        44
#define  INSTR_MULS_VAR_VAR        45
#define  INSTR_DIVS_VAR_VAR        46

 /* }}} */
/* {{{ definition of the validation error types */
static const char *VALIDATE_OK = "ok";

#define VALIDATE_ERROR "error while validating "
#define VALIDATE_TODO "todo"
#define VALIDATE_SYNTHAX_ERROR "synthax error"
#define VALIDATE_NO_SUCH_INT "no such integer variable"
#define VALIDATE_NO_SUCH_VAR "no such variable"
#define VALIDATE_NO_SUCH_DEST_VAR "no such destination variable"
#define VALIDATE_NO_SUCH_SRC_VAR "no such src variable"
/* }}} */

  /***********************************/
 /* PROTOTYPE OF INTERNAL FUNCTIONS */
/***********************************/

/* {{{ */
static void gsl_instr_free (Instruction * _this);
static const char *gsl_instr_validate (Instruction * _this);
static void gsl_instr_display (Instruction * _this);

static InstructionFlow *iflow_new (void);
static void iflow_add_instr (InstructionFlow * _this, Instruction * instr);
static void iflow_clean (InstructionFlow * _this);
static void iflow_free (InstructionFlow * _this);
static void iflow_execute (FastInstructionFlow * _this, GoomSL * gsl);

/* }}} */

  /************************************/
 /* DEFINITION OF INTERNAL FUNCTIONS */
/************************************/

void
iflow_free (InstructionFlow * _this)
{                               /* {{{ */
  goom_hash_free (_this->labels);
  free (_this);                 /*TODO: finir cette fonction */
}                               /* }}} */

void
iflow_clean (InstructionFlow * _this)
{                               /* {{{ */
  /* TODO: clean chaque instruction du flot */
  _this->number = 0;
  goom_hash_free (_this->labels);
  _this->labels = goom_hash_new ();
}                               /* }}} */

InstructionFlow *
iflow_new (void)
{                               /* {{{ */
  InstructionFlow *_this =
      (InstructionFlow *) malloc (sizeof (InstructionFlow));
  _this->number = 0;
  _this->tabsize = 6;
  _this->instr =
      (Instruction **) malloc (_this->tabsize * sizeof (Instruction *));
  _this->labels = goom_hash_new ();

  return _this;
}                               /* }}} */

void
iflow_add_instr (InstructionFlow * _this, Instruction * instr)
{                               /* {{{ */
  if (_this->number == _this->tabsize) {
    _this->tabsize *= 2;
    _this->instr =
        (Instruction **) realloc (_this->instr,
        _this->tabsize * sizeof (Instruction *));
  }
  _this->instr[_this->number] = instr;
  instr->address = _this->number;
  _this->number++;
}                               /* }}} */

void
gsl_instr_set_namespace (Instruction * _this, GoomHash * ns)
{                               /* {{{ */
  if (_this->cur_param <= 0) {
    fprintf (stderr, "ERROR: Line %d, No more params to instructions\n",
        _this->line_number);
    exit (1);
  }
  _this->vnamespace[_this->cur_param - 1] = ns;
}                               /* }}} */

void
gsl_instr_add_param (Instruction * instr, char *param, int type)
{                               /* {{{ */
  int len;

  if (instr == NULL)
    return;
  if (instr->cur_param == 0)
    return;
  --instr->cur_param;
  len = strlen (param);
  instr->params[instr->cur_param] = (char *) malloc (len + 1);
  strcpy (instr->params[instr->cur_param], param);
  instr->types[instr->cur_param] = type;
  if (instr->cur_param == 0) {

    const char *result = gsl_instr_validate (instr);

    if (result != VALIDATE_OK) {
      printf ("ERROR: Line %d: ", instr->parent->num_lines + 1);
      gsl_instr_display (instr);
      printf ("... %s\n", result);
      instr->parent->compilationOK = 0;
      exit (1);
    }
#if USE_JITC_X86
    iflow_add_instr (instr->parent->iflow, instr);
#else
    if (instr->id != INSTR_NOP)
      iflow_add_instr (instr->parent->iflow, instr);
    else
      gsl_instr_free (instr);
#endif
  }
}                               /* }}} */

Instruction *
gsl_instr_init (GoomSL * parent, const char *name, int id, int nb_param,
    int line_number)
{                               /* {{{ */
  Instruction *instr = (Instruction *) malloc (sizeof (Instruction));
  instr->params = (char **) malloc (nb_param * sizeof (char *));
  instr->vnamespace = (GoomHash **) malloc (nb_param * sizeof (GoomHash *));
  instr->types = (int *) malloc (nb_param * sizeof (int));
  instr->cur_param = instr->nb_param = nb_param;
  instr->parent = parent;
  instr->id = id;
  instr->name = name;
  instr->jump_label = NULL;
  instr->line_number = line_number;
  return instr;
}                               /* }}} */

void
gsl_instr_free (Instruction * _this)
{                               /* {{{ */
  int i;

  free (_this->types);
  for (i = _this->cur_param; i < _this->nb_param; ++i)
    free (_this->params[i]);
  free (_this->params);
  free (_this);
}                               /* }}} */

void
gsl_instr_display (Instruction * _this)
{                               /* {{{ */
  int i = _this->nb_param - 1;

  printf ("%s", _this->name);
  while (i >= _this->cur_param) {
    printf (" %s", _this->params[i]);
    --i;
  }
}                               /* }}} */

  /****************************************/
 /* VALIDATION OF INSTRUCTION PARAMETERS */
/****************************************/

static const char *
validate_v_v (Instruction * _this)
{                               /* {{{ */
  HashValue *dest = goom_hash_get (_this->vnamespace[1], _this->params[1]);
  HashValue *src = goom_hash_get (_this->vnamespace[0], _this->params[0]);

  if (dest == NULL) {
    return VALIDATE_NO_SUCH_DEST_VAR;
  }
  if (src == NULL) {
    return VALIDATE_NO_SUCH_SRC_VAR;
  }
  _this->data.udest.var = dest->ptr;
  _this->data.usrc.var = src->ptr;
  return VALIDATE_OK;
}                               /* }}} */

static const char *
validate_v_i (Instruction * _this)
{                               /* {{{ */
  HashValue *dest = goom_hash_get (_this->vnamespace[1], _this->params[1]);

  _this->data.usrc.value_int = strtol (_this->params[0], NULL, 0);

  if (dest == NULL) {
    return VALIDATE_NO_SUCH_INT;
  }
  _this->data.udest.var = dest->ptr;
  return VALIDATE_OK;
}                               /* }}} */

static const char *
validate_v_p (Instruction * _this)
{                               /* {{{ */
  HashValue *dest = goom_hash_get (_this->vnamespace[1], _this->params[1]);

  _this->data.usrc.value_ptr = strtol (_this->params[0], NULL, 0);

  if (dest == NULL) {
    return VALIDATE_NO_SUCH_INT;
  }
  _this->data.udest.var = dest->ptr;
  return VALIDATE_OK;
}                               /* }}} */

static const char *
validate_v_f (Instruction * _this)
{                               /* {{{ */
  HashValue *dest = goom_hash_get (_this->vnamespace[1], _this->params[1]);

  _this->data.usrc.value_float = atof (_this->params[0]);

  if (dest == NULL) {
    return VALIDATE_NO_SUCH_VAR;
  }
  _this->data.udest.var = dest->ptr;
  return VALIDATE_OK;
}                               /* }}} */

static const char *
validate (Instruction * _this,
    int vf_f_id, int vf_v_id,
    int vi_i_id, int vi_v_id, int vp_p_id, int vp_v_id, int vs_v_id)
{                               /* {{{ */
  if ((_this->types[1] == TYPE_FVAR) && (_this->types[0] == TYPE_FLOAT)) {
    _this->id = vf_f_id;
    return validate_v_f (_this);
  } else if ((_this->types[1] == TYPE_FVAR) && (_this->types[0] == TYPE_FVAR)) {
    _this->id = vf_v_id;
    return validate_v_v (_this);
  } else if ((_this->types[1] == TYPE_IVAR)
      && (_this->types[0] == TYPE_INTEGER)) {
    _this->id = vi_i_id;
    return validate_v_i (_this);
  } else if ((_this->types[1] == TYPE_IVAR) && (_this->types[0] == TYPE_IVAR)) {
    _this->id = vi_v_id;
    return validate_v_v (_this);
  } else if ((_this->types[1] == TYPE_PVAR) && (_this->types[0] == TYPE_PTR)) {
    if (vp_p_id == INSTR_NOP)
      return VALIDATE_ERROR;
    _this->id = vp_p_id;
    return validate_v_p (_this);
  } else if ((_this->types[1] == TYPE_PVAR) && (_this->types[0] == TYPE_PVAR)) {
    _this->id = vp_v_id;
    if (vp_v_id == INSTR_NOP)
      return VALIDATE_ERROR;
    return validate_v_v (_this);
  } else if ((_this->types[1] < FIRST_RESERVED) && (_this->types[1] >= 0)
      && (_this->types[0] == _this->types[1])) {
    _this->id = vs_v_id;
    if (vs_v_id == INSTR_NOP)
      return "Impossible operation to perform between two structs";
    return validate_v_v (_this);
  }
  return VALIDATE_ERROR;
}                               /* }}} */

const char *
gsl_instr_validate (Instruction * _this)
{                               /* {{{ */
  if (_this->id != INSTR_EXT_CALL) {
    int i = _this->nb_param;

    while (i > 0) {
      i--;
      if (_this->types[i] == TYPE_VAR) {
        int type = gsl_type_of_var (_this->vnamespace[i], _this->params[i]);

        if (type == INSTR_INT)
          _this->types[i] = TYPE_IVAR;
        else if (type == INSTR_FLOAT)
          _this->types[i] = TYPE_FVAR;
        else if (type == INSTR_PTR)
          _this->types[i] = TYPE_PVAR;
        else if ((type >= 0) && (type < FIRST_RESERVED))
          _this->types[i] = type;
        else
          fprintf (stderr, "WARNING: Line %d, %s has no namespace\n",
              _this->line_number, _this->params[i]);
      }
    }
  }

  switch (_this->id) {

      /* set */
    case INSTR_SET:
      return validate (_this,
          INSTR_SETF_VAR_FLOAT, INSTR_SETF_VAR_VAR,
          INSTR_SETI_VAR_INTEGER, INSTR_SETI_VAR_VAR,
          INSTR_SETP_VAR_PTR, INSTR_SETP_VAR_VAR, INSTR_SETS_VAR_VAR);

      /* extcall */
    case INSTR_EXT_CALL:
      if (_this->types[0] == TYPE_VAR) {
        HashValue *fval =
            goom_hash_get (_this->parent->functions, _this->params[0]);
        if (fval) {
          _this->data.udest.external_function =
              (struct _ExternalFunctionStruct *) fval->ptr;
          return VALIDATE_OK;
        }
      }
      return VALIDATE_ERROR;

      /* call */
    case INSTR_CALL:
      if (_this->types[0] == TYPE_LABEL) {
        _this->jump_label = _this->params[0];
        return VALIDATE_OK;
      }
      return VALIDATE_ERROR;

      /* ret */
    case INSTR_RET:
      return VALIDATE_OK;

      /* jump */
    case INSTR_JUMP:

      if (_this->types[0] == TYPE_LABEL) {
        _this->jump_label = _this->params[0];
        return VALIDATE_OK;
      }
      return VALIDATE_ERROR;

      /* jzero / jnzero */
    case INSTR_JZERO:
    case INSTR_JNZERO:

      if (_this->types[0] == TYPE_LABEL) {
        _this->jump_label = _this->params[0];
        return VALIDATE_OK;
      }
      return VALIDATE_ERROR;

      /* label */
    case INSTR_LABEL:

      if (_this->types[0] == TYPE_LABEL) {
        _this->id = INSTR_NOP;
        _this->nop_label = _this->params[0];
        goom_hash_put_int (_this->parent->iflow->labels, _this->params[0],
            _this->parent->iflow->number);
        return VALIDATE_OK;
      }
      return VALIDATE_ERROR;

      /* isequal */
    case INSTR_ISEQUAL:
      return validate (_this,
          INSTR_ISEQUALF_VAR_FLOAT, INSTR_ISEQUALF_VAR_VAR,
          INSTR_ISEQUALI_VAR_INTEGER, INSTR_ISEQUALI_VAR_VAR,
          INSTR_ISEQUALP_VAR_PTR, INSTR_ISEQUALP_VAR_VAR,
          INSTR_ISEQUALS_VAR_VAR);

      /* not */
    case INSTR_NOT:
      _this->id = INSTR_NOT_VAR;
      return VALIDATE_OK;

      /* islower */
    case INSTR_ISLOWER:
      return validate (_this,
          INSTR_ISLOWERF_VAR_FLOAT, INSTR_ISLOWERF_VAR_VAR,
          INSTR_ISLOWERI_VAR_INTEGER, INSTR_ISLOWERI_VAR_VAR,
          INSTR_NOP, INSTR_NOP, INSTR_NOP);

      /* add */
    case INSTR_ADD:
      return validate (_this,
          INSTR_ADDF_VAR_FLOAT, INSTR_ADDF_VAR_VAR,
          INSTR_ADDI_VAR_INTEGER, INSTR_ADDI_VAR_VAR,
          INSTR_NOP, INSTR_NOP, INSTR_ADDS_VAR_VAR);

      /* mul */
    case INSTR_MUL:
      return validate (_this,
          INSTR_MULF_VAR_FLOAT, INSTR_MULF_VAR_VAR,
          INSTR_MULI_VAR_INTEGER, INSTR_MULI_VAR_VAR,
          INSTR_NOP, INSTR_NOP, INSTR_MULS_VAR_VAR);

      /* sub */
    case INSTR_SUB:
      return validate (_this,
          INSTR_SUBF_VAR_FLOAT, INSTR_SUBF_VAR_VAR,
          INSTR_SUBI_VAR_INTEGER, INSTR_SUBI_VAR_VAR,
          INSTR_NOP, INSTR_NOP, INSTR_SUBS_VAR_VAR);

      /* div */
    case INSTR_DIV:
      return validate (_this,
          INSTR_DIVF_VAR_FLOAT, INSTR_DIVF_VAR_VAR,
          INSTR_DIVI_VAR_INTEGER, INSTR_DIVI_VAR_VAR,
          INSTR_NOP, INSTR_NOP, INSTR_DIVS_VAR_VAR);

    default:
      return VALIDATE_TODO;
  }
  return VALIDATE_ERROR;
}                               /* }}} */

  /*************/
 /* EXECUTION */
/*************/
void
iflow_execute (FastInstructionFlow * _this, GoomSL * gsl)
{                               /* {{{ */
  int flag = 0;
  int ip = 0;
  FastInstruction *instr = _this->instr;
  int stack[0x10000];
  int stack_pointer = 0;

  stack[stack_pointer++] = -1;

  /* Quelques Macro pour rendre le code plus lisible */
#define pSRC_VAR        instr[ip].data.usrc.var
#define SRC_VAR_INT    *instr[ip].data.usrc.var_int
#define SRC_VAR_FLOAT  *instr[ip].data.usrc.var_float
#define SRC_VAR_PTR    *instr[ip].data.usrc.var_ptr

#define pDEST_VAR       instr[ip].data.udest.var
#define DEST_VAR_INT   *instr[ip].data.udest.var_int
#define DEST_VAR_FLOAT *instr[ip].data.udest.var_float
#define DEST_VAR_PTR   *instr[ip].data.udest.var_ptr

#define VALUE_INT       instr[ip].data.usrc.value_int
#define VALUE_FLOAT     instr[ip].data.usrc.value_float
#define VALUE_PTR       instr[ip].data.usrc.value_ptr

#define JUMP_OFFSET     instr[ip].data.udest.jump_offset

#define SRC_STRUCT_ID  instr[ip].data.usrc.var_int[-1]
#define DEST_STRUCT_ID instr[ip].data.udest.var_int[-1]
#define SRC_STRUCT_IBLOCK(i)  gsl->gsl_struct[SRC_STRUCT_ID]->iBlock[i]
#define SRC_STRUCT_FBLOCK(i)  gsl->gsl_struct[SRC_STRUCT_ID]->fBlock[i]
#define DEST_STRUCT_IBLOCK(i) gsl->gsl_struct[DEST_STRUCT_ID]->iBlock[i]
#define DEST_STRUCT_FBLOCK(i) gsl->gsl_struct[DEST_STRUCT_ID]->fBlock[i]
#define DEST_STRUCT_IBLOCK_VAR(i,j) \
  ((int*)((char*)pDEST_VAR   + gsl->gsl_struct[DEST_STRUCT_ID]->iBlock[i].data))[j]
#define DEST_STRUCT_FBLOCK_VAR(i,j) \
  ((float*)((char*)pDEST_VAR + gsl->gsl_struct[DEST_STRUCT_ID]->fBlock[i].data))[j]
#define SRC_STRUCT_IBLOCK_VAR(i,j) \
  ((int*)((char*)pSRC_VAR    + gsl->gsl_struct[SRC_STRUCT_ID]->iBlock[i].data))[j]
#define SRC_STRUCT_FBLOCK_VAR(i,j) \
  ((float*)((char*)pSRC_VAR  + gsl->gsl_struct[SRC_STRUCT_ID]->fBlock[i].data))[j]
#define DEST_STRUCT_SIZE      gsl->gsl_struct[DEST_STRUCT_ID]->size

  while (1) {
    int i;

#ifdef TRACE_SCRIPT
    printf ("execute ");
    gsl_instr_display (instr[ip].proto);
    printf ("\n");
#endif
    switch (instr[ip].id) {

        /* SET.I */
      case INSTR_SETI_VAR_INTEGER:
        DEST_VAR_INT = VALUE_INT;
        ++ip;
        break;

      case INSTR_SETI_VAR_VAR:
        DEST_VAR_INT = SRC_VAR_INT;
        ++ip;
        break;

        /* SET.F */
      case INSTR_SETF_VAR_FLOAT:
        DEST_VAR_FLOAT = VALUE_FLOAT;
        ++ip;
        break;

      case INSTR_SETF_VAR_VAR:
        DEST_VAR_FLOAT = SRC_VAR_FLOAT;
        ++ip;
        break;

        /* SET.P */
      case INSTR_SETP_VAR_VAR:
        DEST_VAR_PTR = SRC_VAR_PTR;
        ++ip;
        break;

      case INSTR_SETP_VAR_PTR:
        DEST_VAR_PTR = VALUE_PTR;
        ++ip;
        break;

        /* JUMP */
      case INSTR_JUMP:
        ip += JUMP_OFFSET;
        break;

        /* JZERO */
      case INSTR_JZERO:
        ip += (flag ? 1 : JUMP_OFFSET);
        break;

      case INSTR_NOP:
        ++ip;
        break;

        /* ISEQUAL.P */
      case INSTR_ISEQUALP_VAR_VAR:
        flag = (DEST_VAR_PTR == SRC_VAR_PTR);
        ++ip;
        break;

      case INSTR_ISEQUALP_VAR_PTR:
        flag = (DEST_VAR_PTR == VALUE_PTR);
        ++ip;
        break;

        /* ISEQUAL.I */
      case INSTR_ISEQUALI_VAR_VAR:
        flag = (DEST_VAR_INT == SRC_VAR_INT);
        ++ip;
        break;

      case INSTR_ISEQUALI_VAR_INTEGER:
        flag = (DEST_VAR_INT == VALUE_INT);
        ++ip;
        break;

        /* ISEQUAL.F */
      case INSTR_ISEQUALF_VAR_VAR:
        flag = (DEST_VAR_FLOAT == SRC_VAR_FLOAT);
        ++ip;
        break;

      case INSTR_ISEQUALF_VAR_FLOAT:
        flag = (DEST_VAR_FLOAT == VALUE_FLOAT);
        ++ip;
        break;

        /* ISLOWER.I */
      case INSTR_ISLOWERI_VAR_VAR:
        flag = (DEST_VAR_INT < SRC_VAR_INT);
        ++ip;
        break;

      case INSTR_ISLOWERI_VAR_INTEGER:
        flag = (DEST_VAR_INT < VALUE_INT);
        ++ip;
        break;

        /* ISLOWER.F */
      case INSTR_ISLOWERF_VAR_VAR:
        flag = (DEST_VAR_FLOAT < SRC_VAR_FLOAT);
        ++ip;
        break;

      case INSTR_ISLOWERF_VAR_FLOAT:
        flag = (DEST_VAR_FLOAT < VALUE_FLOAT);
        ++ip;
        break;

        /* ADD.I */
      case INSTR_ADDI_VAR_VAR:
        DEST_VAR_INT += SRC_VAR_INT;
        ++ip;
        break;

      case INSTR_ADDI_VAR_INTEGER:
        DEST_VAR_INT += VALUE_INT;
        ++ip;
        break;

        /* ADD.F */
      case INSTR_ADDF_VAR_VAR:
        DEST_VAR_FLOAT += SRC_VAR_FLOAT;
        ++ip;
        break;

      case INSTR_ADDF_VAR_FLOAT:
        DEST_VAR_FLOAT += VALUE_FLOAT;
        ++ip;
        break;

        /* MUL.I */
      case INSTR_MULI_VAR_VAR:
        DEST_VAR_INT *= SRC_VAR_INT;
        ++ip;
        break;

      case INSTR_MULI_VAR_INTEGER:
        DEST_VAR_INT *= VALUE_INT;
        ++ip;
        break;

        /* MUL.F */
      case INSTR_MULF_VAR_FLOAT:
        DEST_VAR_FLOAT *= VALUE_FLOAT;
        ++ip;
        break;

      case INSTR_MULF_VAR_VAR:
        DEST_VAR_FLOAT *= SRC_VAR_FLOAT;
        ++ip;
        break;

        /* DIV.I */
      case INSTR_DIVI_VAR_VAR:
        DEST_VAR_INT /= SRC_VAR_INT;
        ++ip;
        break;

      case INSTR_DIVI_VAR_INTEGER:
        DEST_VAR_INT /= VALUE_INT;
        ++ip;
        break;

        /* DIV.F */
      case INSTR_DIVF_VAR_FLOAT:
        DEST_VAR_FLOAT /= VALUE_FLOAT;
        ++ip;
        break;

      case INSTR_DIVF_VAR_VAR:
        DEST_VAR_FLOAT /= SRC_VAR_FLOAT;
        ++ip;
        break;

        /* SUB.I */
      case INSTR_SUBI_VAR_VAR:
        DEST_VAR_INT -= SRC_VAR_INT;
        ++ip;
        break;

      case INSTR_SUBI_VAR_INTEGER:
        DEST_VAR_INT -= VALUE_INT;
        ++ip;
        break;

        /* SUB.F */
      case INSTR_SUBF_VAR_FLOAT:
        DEST_VAR_FLOAT -= VALUE_FLOAT;
        ++ip;
        break;

      case INSTR_SUBF_VAR_VAR:
        DEST_VAR_FLOAT -= SRC_VAR_FLOAT;
        ++ip;
        break;

        /* CALL */
      case INSTR_CALL:
        stack[stack_pointer++] = ip + 1;
        ip += JUMP_OFFSET;
        break;

        /* RET */
      case INSTR_RET:
        ip = stack[--stack_pointer];
        if (ip < 0)
          return;
        break;

        /* EXT_CALL */
      case INSTR_EXT_CALL:
        instr[ip].data.udest.external_function->function (gsl, gsl->vars,
            instr[ip].data.udest.external_function->vars);
        ++ip;
        break;

        /* NOT */
      case INSTR_NOT_VAR:
        flag = !flag;
        ++ip;
        break;

        /* JNZERO */
      case INSTR_JNZERO:
        ip += (flag ? JUMP_OFFSET : 1);
        break;

      case INSTR_SETS_VAR_VAR:
        memcpy (pDEST_VAR, pSRC_VAR, DEST_STRUCT_SIZE);
        ++ip;
        break;

      case INSTR_ISEQUALS_VAR_VAR:
        break;

      case INSTR_ADDS_VAR_VAR:
        /* process integers */
        i = 0;
        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_IBLOCK_VAR (i, j) += SRC_STRUCT_IBLOCK_VAR (i, j);
          }
          ++i;
        }
        /* process floats */
        i = 0;
        while (DEST_STRUCT_FBLOCK (i).size > 0) {
          int j = DEST_STRUCT_FBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_FBLOCK_VAR (i, j) += SRC_STRUCT_FBLOCK_VAR (i, j);
          }
          ++i;
        }
        ++ip;
        break;

      case INSTR_SUBS_VAR_VAR:
        /* process integers */
        i = 0;
        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_IBLOCK_VAR (i, j) -= SRC_STRUCT_IBLOCK_VAR (i, j);
          }
          ++i;
        }
        /* process floats */
        i = 0;
        while (DEST_STRUCT_FBLOCK (i).size > 0) {
          int j = DEST_STRUCT_FBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_FBLOCK_VAR (i, j) -= SRC_STRUCT_FBLOCK_VAR (i, j);
          }
          ++i;
        }
        ++ip;
        break;

      case INSTR_MULS_VAR_VAR:
        /* process integers */
        i = 0;
        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_IBLOCK_VAR (i, j) *= SRC_STRUCT_IBLOCK_VAR (i, j);
          }
          ++i;
        }
        /* process floats */
        i = 0;
        while (DEST_STRUCT_FBLOCK (i).size > 0) {
          int j = DEST_STRUCT_FBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_FBLOCK_VAR (i, j) *= SRC_STRUCT_FBLOCK_VAR (i, j);
          }
          ++i;
        }
        ++ip;
        break;

      case INSTR_DIVS_VAR_VAR:
        /* process integers */
        i = 0;
        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_IBLOCK_VAR (i, j) /= SRC_STRUCT_IBLOCK_VAR (i, j);
          }
          ++i;
        }
        /* process floats */
        i = 0;
        while (DEST_STRUCT_FBLOCK (i).size > 0) {
          int j = DEST_STRUCT_FBLOCK (i).size;

          while (j--) {
            DEST_STRUCT_FBLOCK_VAR (i, j) /= SRC_STRUCT_FBLOCK_VAR (i, j);
          }
          ++i;
        }
        ++ip;
        break;

      default:
        printf ("NOT IMPLEMENTED : %d\n", instr[ip].id);
        ++ip;
        exit (1);
    }
  }
}                               /* }}} */

int
gsl_malloc (GoomSL * _this, int size)
{                               /* {{{ */
  if (_this->nbPtr >= _this->ptrArraySize) {
    _this->ptrArraySize *= 2;
    _this->ptrArray =
        (void **) realloc (_this->ptrArray,
        sizeof (void *) * _this->ptrArraySize);
  }
  _this->ptrArray[_this->nbPtr] = malloc (size);
  return _this->nbPtr++;
}                               /* }}} */

void *
gsl_get_ptr (GoomSL * _this, int id)
{                               /* {{{ */
  if ((id >= 0) && (id < _this->nbPtr))
    return _this->ptrArray[id];
  fprintf (stderr, "INVALID GET PTR 0x%08x\n", id);
  return NULL;
}                               /* }}} */

void
gsl_free_ptr (GoomSL * _this, int id)
{                               /* {{{ */
  if ((id >= 0) && (id < _this->nbPtr)) {
    free (_this->ptrArray[id]);
    _this->ptrArray[id] = 0;
  }
}                               /* }}} */

void
gsl_enternamespace (const char *name)
{                               /* {{{ */
  HashValue *val = goom_hash_get (currentGoomSL->functions, name);

  if (val) {
    ExternalFunctionStruct *function = (ExternalFunctionStruct *) val->ptr;

    currentGoomSL->currentNS++;
    currentGoomSL->namespaces[currentGoomSL->currentNS] = function->vars;
  } else {
    fprintf (stderr, "ERROR: Line %d, Could not find namespace: %s\n",
        currentGoomSL->num_lines, name);
    exit (1);
  }
}                               /* }}} */

void
gsl_reenternamespace (GoomHash * nsinfo)
{
  currentGoomSL->currentNS++;
  currentGoomSL->namespaces[currentGoomSL->currentNS] = nsinfo;
}

GoomHash *
gsl_leavenamespace (void)
{                               /* {{{ */
  currentGoomSL->currentNS--;
  return currentGoomSL->namespaces[currentGoomSL->currentNS + 1];
}                               /* }}} */

GoomHash *
gsl_find_namespace (const char *name)
{                               /* {{{ */
  int i;

  for (i = currentGoomSL->currentNS; i >= 0; --i) {
    if (goom_hash_get (currentGoomSL->namespaces[i], name))
      return currentGoomSL->namespaces[i];
  }
  return NULL;
}                               /* }}} */

void
gsl_declare_task (const char *name)
{                               /* {{{ */
  if (goom_hash_get (currentGoomSL->functions, name)) {
    return;
  } else {
    ExternalFunctionStruct *gef =
        (ExternalFunctionStruct *) malloc (sizeof (ExternalFunctionStruct));
    gef->function = 0;
    gef->vars = goom_hash_new ();
    gef->is_extern = 0;
    goom_hash_put_ptr (currentGoomSL->functions, name, (void *) gef);
  }
}                               /* }}} */

void
gsl_declare_external_task (const char *name)
{                               /* {{{ */
  if (goom_hash_get (currentGoomSL->functions, name)) {
    fprintf (stderr, "ERROR: Line %d, Duplicate declaration of %s\n",
        currentGoomSL->num_lines, name);
    return;
  } else {
    ExternalFunctionStruct *gef =
        (ExternalFunctionStruct *) malloc (sizeof (ExternalFunctionStruct));
    gef->function = 0;
    gef->vars = goom_hash_new ();
    gef->is_extern = 1;
    goom_hash_put_ptr (currentGoomSL->functions, name, (void *) gef);
  }
}                               /* }}} */

static void
reset_scanner (GoomSL * gss)
{                               /* {{{ */
  gss->num_lines = 0;
  gss->instr = NULL;
  iflow_clean (gss->iflow);

  /* reset variables */
  goom_hash_free (gss->vars);
  gss->vars = goom_hash_new ();
  gss->currentNS = 0;
  gss->namespaces[0] = gss->vars;

  goom_hash_free (gss->structIDS);
  gss->structIDS = goom_hash_new ();

  while (gss->nbStructID > 0) {
    int i;

    gss->nbStructID--;
    for (i = 0; i < gss->gsl_struct[gss->nbStructID]->nbFields; ++i)
      free (gss->gsl_struct[gss->nbStructID]->fields[i]);
    free (gss->gsl_struct[gss->nbStructID]);
  }

  gss->compilationOK = 1;

  goom_heap_delete (gss->data_heap);
  gss->data_heap = goom_heap_new ();
}                               /* }}} */

static void
calculate_labels (InstructionFlow * iflow)
{                               /* {{{ */
  int i = 0;

  while (i < iflow->number) {
    Instruction *instr = iflow->instr[i];

    if (instr->jump_label) {
      HashValue *label = goom_hash_get (iflow->labels, instr->jump_label);

      if (label) {
        instr->data.udest.jump_offset = -instr->address + label->i;
      } else {
        fprintf (stderr, "ERROR: Line %d, Could not find label %s\n",
            instr->line_number, instr->jump_label);
        instr->id = INSTR_NOP;
        instr->nop_label = 0;
        exit (1);
      }
    }
    ++i;
  }
}                               /* }}} */

static int
powerOfTwo (int i)
{
  int b;

  for (b = 0; b < 31; b++)
    if (i == (1 << b))
      return b;
  return 0;
}

/* Cree un flow d'instruction optimise */
static void
gsl_create_fast_iflow (void)
{                               /* {{{ */
  int number = currentGoomSL->iflow->number;
  int i;

#ifdef USE_JITC_X86

  /* pour compatibilite avec les MACROS servant a execution */
  int ip = 0;
  GoomSL *gsl = currentGoomSL;

  JitcX86Env *jitc;

  if (currentGoomSL->jitc != NULL)
    jitc_x86_delete (currentGoomSL->jitc);
  jitc = currentGoomSL->jitc = jitc_x86_env_new (0xffff);
  currentGoomSL->jitc_func = jitc_prepare_func (jitc);

#if 0
#define SRC_STRUCT_ID  instr[ip].data.usrc.var_int[-1]
#define DEST_STRUCT_ID instr[ip].data.udest.var_int[-1]
#define SRC_STRUCT_IBLOCK(i)  gsl->gsl_struct[SRC_STRUCT_ID]->iBlock[i]
#define SRC_STRUCT_FBLOCK(i)  gsl->gsl_struct[SRC_STRUCT_ID]->fBlock[i]
#define DEST_STRUCT_IBLOCK(i) gsl->gsl_struct[DEST_STRUCT_ID]->iBlock[i]
#define DEST_STRUCT_FBLOCK(i) gsl->gsl_struct[DEST_STRUCT_ID]->fBlock[i]
#define DEST_STRUCT_IBLOCK_VAR(i,j) \
  ((int*)((char*)pDEST_VAR   + gsl->gsl_struct[DEST_STRUCT_ID]->iBlock[i].data))[j]
#define DEST_STRUCT_FBLOCK_VAR(i,j) \
  ((float*)((char*)pDEST_VAR + gsl->gsl_struct[DEST_STRUCT_ID]->fBlock[i].data))[j]
#define SRC_STRUCT_IBLOCK_VAR(i,j) \
  ((int*)((char*)pSRC_VAR    + gsl->gsl_struct[SRC_STRUCT_ID]->iBlock[i].data))[j]
#define SRC_STRUCT_FBLOCK_VAR(i,j) \
  ((float*)((char*)pSRC_VAR  + gsl->gsl_struct[SRC_STRUCT_ID]->fBlock[i].data))[j]
#define DEST_STRUCT_SIZE      gsl->gsl_struct[DEST_STRUCT_ID]->size
#endif

  JITC_JUMP_LABEL (jitc, "__very_end__");
  JITC_ADD_LABEL (jitc, "__very_start__");

  for (i = 0; i < number; ++i) {
    Instruction *instr = currentGoomSL->iflow->instr[i];

    switch (instr->id) {
      case INSTR_SETI_VAR_INTEGER:
        jitc_add (jitc, "mov [$d], $d", instr->data.udest.var_int,
            instr->data.usrc.value_int);
        break;
      case INSTR_SETI_VAR_VAR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.usrc.var_int);
        jitc_add (jitc, "mov [$d], eax", instr->data.udest.var_int);
        break;
        /* SET.F */
      case INSTR_SETF_VAR_FLOAT:
        jitc_add (jitc, "mov [$d], $d", instr->data.udest.var_float,
            *(int *) (&instr->data.usrc.value_float));
        break;
      case INSTR_SETF_VAR_VAR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.usrc.var_float);
        jitc_add (jitc, "mov [$d], eax", instr->data.udest.var_float);
        break;
      case INSTR_NOP:
        if (instr->nop_label != 0)
          JITC_ADD_LABEL (jitc, instr->nop_label);
        break;
      case INSTR_JUMP:
        JITC_JUMP_LABEL (jitc, instr->jump_label);
        break;
      case INSTR_SETP_VAR_PTR:
        jitc_add (jitc, "mov [$d], $d", instr->data.udest.var_ptr,
            instr->data.usrc.value_ptr);
        break;
      case INSTR_SETP_VAR_VAR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.usrc.var_ptr);
        jitc_add (jitc, "mov [$d], eax", instr->data.udest.var_ptr);
        break;
      case INSTR_SUBI_VAR_INTEGER:
        jitc_add (jitc, "add [$d],  $d", instr->data.udest.var_int,
            -instr->data.usrc.value_int);
        break;
      case INSTR_SUBI_VAR_VAR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "sub eax, [$d]", instr->data.usrc.var_int);
        jitc_add (jitc, "mov [$d], eax", instr->data.udest.var_int);
        break;
      case INSTR_SUBF_VAR_FLOAT:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_SUBF_VAR_VAR:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_ISLOWERF_VAR_VAR:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_ISLOWERF_VAR_FLOAT:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_ISLOWERI_VAR_VAR:
        jitc_add (jitc, "mov edx, [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "sub edx, [$d]", instr->data.usrc.var_int);
        jitc_add (jitc, "shr edx, $d", 31);
        break;
      case INSTR_ISLOWERI_VAR_INTEGER:
        jitc_add (jitc, "mov edx, [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "sub edx, $d", instr->data.usrc.value_int);
        jitc_add (jitc, "shr edx, $d", 31);
        break;
      case INSTR_ADDI_VAR_INTEGER:
        jitc_add (jitc, "add [$d],  $d", instr->data.udest.var_int,
            instr->data.usrc.value_int);
        break;
      case INSTR_ADDI_VAR_VAR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "add eax, [$d]", instr->data.usrc.var_int);
        jitc_add (jitc, "mov [$d], eax", instr->data.udest.var_int);
        break;
      case INSTR_ADDF_VAR_FLOAT:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_ADDF_VAR_VAR:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_MULI_VAR_INTEGER:
        if (instr->data.usrc.value_int != 1) {
          int po2 = powerOfTwo (instr->data.usrc.value_int);

          if (po2) {
            /* performs (V / 2^n) by doing V >> n */
            jitc_add (jitc, "mov  eax, [$d]", instr->data.udest.var_int);
            jitc_add (jitc, "sal  eax, $d", po2);
            jitc_add (jitc, "mov  [$d], eax", instr->data.udest.var_int);
          } else {
            jitc_add (jitc, "mov  eax, [$d]", instr->data.udest.var_int);
            jitc_add (jitc, "imul eax, $d", instr->data.usrc.value_int);
            jitc_add (jitc, "mov  [$d], eax", instr->data.udest.var_int);
          }
        }
        break;
      case INSTR_MULI_VAR_VAR:
        jitc_add (jitc, "mov  eax,  [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "imul eax,  [$d]", instr->data.usrc.var_int);
        jitc_add (jitc, "mov  [$d], eax", instr->data.udest.var_int);
        break;
      case INSTR_MULF_VAR_FLOAT:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_MULF_VAR_VAR:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_DIVI_VAR_INTEGER:
        if ((instr->data.usrc.value_int != 1)
            && (instr->data.usrc.value_int != 0)) {
          int po2 = powerOfTwo (instr->data.usrc.value_int);

          if (po2) {
            /* performs (V / 2^n) by doing V >> n */
            jitc_add (jitc, "mov  eax, [$d]", instr->data.udest.var_int);
            jitc_add (jitc, "sar  eax, $d", po2);
            jitc_add (jitc, "mov  [$d], eax", instr->data.udest.var_int);
          } else {
            /* performs (V/n) by doing (V*(32^2/n)) */
            long coef;
            double dcoef =
                (double) 4294967296.0 / (double) instr->data.usrc.value_int;
            if (dcoef < 0.0)
              dcoef = -dcoef;
            coef = (long) floor (dcoef);
            dcoef -= floor (dcoef);
            if (dcoef < 0.5)
              coef += 1;

            jitc_add (jitc, "mov  eax, [$d]", instr->data.udest.var_int);
            jitc_add (jitc, "mov  edx, $d", coef);
            jitc_add (jitc, "imul edx");
            if (instr->data.usrc.value_int < 0)
              jitc_add (jitc, "neg edx");
            jitc_add (jitc, "mov [$d], edx", instr->data.udest.var_int);
          }
        }
        break;
      case INSTR_DIVI_VAR_VAR:
        jitc_add (jitc, "mov  eax, [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "cdq"); /* sign extend eax into edx */
        jitc_add (jitc, "idiv [$d]", instr->data.usrc.var_int);
        jitc_add (jitc, "mov [$d], eax", instr->data.udest.var_int);
        break;
      case INSTR_DIVF_VAR_FLOAT:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_DIVF_VAR_VAR:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_JZERO:
        jitc_add (jitc, "cmp edx, $d", 0);
        jitc_add (jitc, "je $s", instr->jump_label);
        break;
      case INSTR_ISEQUALP_VAR_VAR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.udest.var_ptr);
        jitc_add (jitc, "mov edx, $d", 0);
        jitc_add (jitc, "cmp eax, [$d]", instr->data.usrc.var_ptr);
        jitc_add (jitc, "jne $d", 1);
        jitc_add (jitc, "inc edx");
        break;
      case INSTR_ISEQUALP_VAR_PTR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.udest.var_ptr);
        jitc_add (jitc, "mov edx, $d", 0);
        jitc_add (jitc, "cmp eax, $d", instr->data.usrc.value_ptr);
        jitc_add (jitc, "jne $d", 1);
        jitc_add (jitc, "inc edx");
        break;
      case INSTR_ISEQUALI_VAR_VAR:
        jitc_add (jitc, "mov eax, [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "mov edx, $d", 0);
        jitc_add (jitc, "cmp eax, [$d]", instr->data.usrc.var_int);
        jitc_add (jitc, "jne $d", 1);
        jitc_add (jitc, "inc edx");
        break;
      case INSTR_ISEQUALI_VAR_INTEGER:
        jitc_add (jitc, "mov eax, [$d]", instr->data.udest.var_int);
        jitc_add (jitc, "mov edx, $d", 0);
        jitc_add (jitc, "cmp eax, $d", instr->data.usrc.value_int);
        jitc_add (jitc, "jne $d", 1);
        jitc_add (jitc, "inc edx");
        break;
      case INSTR_ISEQUALF_VAR_VAR:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_ISEQUALF_VAR_FLOAT:
        printf ("NOT IMPLEMENTED : %d\n", instr->id);
        break;
      case INSTR_CALL:
        jitc_add (jitc, "call $s", instr->jump_label);
        break;
      case INSTR_RET:
        jitc_add (jitc, "ret");
        break;
      case INSTR_EXT_CALL:
        jitc_add (jitc, "mov eax, [$d]",
            &(instr->data.udest.external_function->vars));
        jitc_add (jitc, "push eax");
        jitc_add (jitc, "mov edx, [$d]", &(currentGoomSL->vars));
        jitc_add (jitc, "push edx");
        jitc_add (jitc, "mov eax, [$d]", &(currentGoomSL));
        jitc_add (jitc, "push eax");

        jitc_add (jitc, "mov eax, [$d]",
            &(instr->data.udest.external_function));
        jitc_add (jitc, "mov eax, [eax]");
        jitc_add (jitc, "call [eax]");
        jitc_add (jitc, "add esp, $d", 12);
        break;
      case INSTR_NOT_VAR:
        jitc_add (jitc, "mov eax, edx");
        jitc_add (jitc, "mov edx, $d", 1);
        jitc_add (jitc, "sub edx, eax");
        break;
      case INSTR_JNZERO:
        jitc_add (jitc, "cmp edx, $d", 0);
        jitc_add (jitc, "jne $s", instr->jump_label);
        break;
      case INSTR_SETS_VAR_VAR:
      {
        int loop = DEST_STRUCT_SIZE / sizeof (int);
        int dst = (int) pDEST_VAR;
        int src = (int) pSRC_VAR;

        while (loop--) {
          jitc_add (jitc, "mov eax, [$d]", src);
          jitc_add (jitc, "mov [$d], eax", dst);
          src += 4;
          dst += 4;
        }
      }
        break;
      case INSTR_ISEQUALS_VAR_VAR:
        break;
      case INSTR_ADDS_VAR_VAR:
      {
        /* process integers */
        int i = 0;

        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {         /* TODO interlace 2 */
            jitc_add (jitc, "mov eax, [$d]", &DEST_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "add eax, [$d]", &SRC_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "mov [$d], eax", &DEST_STRUCT_IBLOCK_VAR (i, j));
          }
          ++i;
        }
        /* process floats */
        i = 0;
        while (DEST_STRUCT_FBLOCK (i).size > 0) {
          int j = DEST_STRUCT_FBLOCK (i).size;

          while (j--) {
            /* DEST_STRUCT_FBLOCK_VAR(i,j) += SRC_STRUCT_FBLOCK_VAR(i,j); */
            /* TODO */
          }
          ++i;
        }
        break;
      }
      case INSTR_SUBS_VAR_VAR:
      {
        /* process integers */
        int i = 0;

        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {
            jitc_add (jitc, "mov eax, [$d]", &DEST_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "sub eax, [$d]", &SRC_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "mov [$d], eax", &DEST_STRUCT_IBLOCK_VAR (i, j));
          }
          ++i;
        }
        break;
      }
      case INSTR_MULS_VAR_VAR:
      {
        /* process integers */
        int i = 0;

        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {
            jitc_add (jitc, "mov  eax,  [$d]", &DEST_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "imul eax,  [$d]", &SRC_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "mov  [$d], eax", &DEST_STRUCT_IBLOCK_VAR (i, j));
          }
          ++i;
        }
        break;
      }
      case INSTR_DIVS_VAR_VAR:
      {
        /* process integers */
        int i = 0;

        while (DEST_STRUCT_IBLOCK (i).size > 0) {
          int j = DEST_STRUCT_IBLOCK (i).size;

          while (j--) {
            jitc_add (jitc, "mov  eax,  [$d]", &DEST_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "cdq");
            jitc_add (jitc, "idiv [$d]", &SRC_STRUCT_IBLOCK_VAR (i, j));
            jitc_add (jitc, "mov  [$d], eax", &DEST_STRUCT_IBLOCK_VAR (i, j));
          }
          ++i;
        }
        break;
      }
    }
  }

  JITC_ADD_LABEL (jitc, "__very_end__");
  jitc_add (jitc, "call $s", "__very_start__");
  jitc_add (jitc, "mov eax, $d", 0);
  jitc_validate_func (jitc);
#else
  InstructionFlow *iflow = currentGoomSL->iflow;
  FastInstructionFlow *fastiflow =
      (FastInstructionFlow *) malloc (sizeof (FastInstructionFlow));
  fastiflow->mallocedInstr = calloc (number * 16, sizeof (FastInstruction));
  /* fastiflow->instr = (FastInstruction*)(((int)fastiflow->mallocedInstr) + 16 - (((int)fastiflow->mallocedInstr)%16)); */
  fastiflow->instr = (FastInstruction *) fastiflow->mallocedInstr;
  fastiflow->number = number;
  for (i = 0; i < number; ++i) {
    fastiflow->instr[i].id = iflow->instr[i]->id;
    fastiflow->instr[i].data = iflow->instr[i]->data;
    fastiflow->instr[i].proto = iflow->instr[i];
  }
  currentGoomSL->fastiflow = fastiflow;
#endif
}                               /* }}} */

void yy_scan_string (const char *str);
void yyparse (void);

GoomHash *
gsl_globals (GoomSL * _this)
{
  return _this->vars;
}


/**
 * Some native external functions
 */
static void
ext_charAt (GoomSL * gsl, GoomHash * global, GoomHash * local)
{
  char *string = GSL_LOCAL_PTR (gsl, local, "value");
  int index = GSL_LOCAL_INT (gsl, local, "index");

  GSL_GLOBAL_INT (gsl, "charAt") = 0;
  if (string == NULL) {
    return;
  }
  if (index < strlen (string))
    GSL_GLOBAL_INT (gsl, "charAt") = string[index];
}

static void
ext_i2f (GoomSL * gsl, GoomHash * global, GoomHash * local)
{
  int i = GSL_LOCAL_INT (gsl, local, "value");

  GSL_GLOBAL_FLOAT (gsl, "i2f") = i;
}

static void
ext_f2i (GoomSL * gsl, GoomHash * global, GoomHash * local)
{
  float f = GSL_LOCAL_FLOAT (gsl, local, "value");

  GSL_GLOBAL_INT (gsl, "f2i") = f;
}

/**
 *
 */
void
gsl_compile (GoomSL * _currentGoomSL, const char *script)
{                               /* {{{ */
  char *script_and_externals;
  static const char *sBinds =
      "external <charAt: string value, int index> : int\n"
      "external <f2i: float value> : int\n"
      "external <i2f: int value> : float\n";

#ifdef VERBOSE
  printf ("\n=== Starting Compilation ===\n");
#endif

  script_and_externals = malloc (strlen (script) + strlen (sBinds) + 2);
  strcpy (script_and_externals, sBinds);
  strcat (script_and_externals, script);

  /* 0- reset */
  currentGoomSL = _currentGoomSL;
  reset_scanner (currentGoomSL);

  /* 1- create the syntaxic tree */
  yy_scan_string (script_and_externals);
  yyparse ();

  /* 2- generate code */
  gsl_commit_compilation ();

  /* 3- resolve symbols */
  calculate_labels (currentGoomSL->iflow);

  /* 4- optimize code */
  gsl_create_fast_iflow ();

  /* 5- bind a few internal functions */
  gsl_bind_function (currentGoomSL, "charAt", ext_charAt);
  gsl_bind_function (currentGoomSL, "f2i", ext_f2i);
  gsl_bind_function (currentGoomSL, "i2f", ext_i2f);
  free (script_and_externals);

#ifdef VERBOSE
  printf ("=== Compilation done. # of lines: %d. # of instr: %d ===\n",
      currentGoomSL->num_lines, currentGoomSL->iflow->number);
#endif
}                               /* }}} */

void
gsl_execute (GoomSL * scanner)
{                               /* {{{ */
  if (scanner->compilationOK) {
#if USE_JITC_X86
    scanner->jitc_func ();
#else
    iflow_execute (scanner->fastiflow, scanner);
#endif
  }
}                               /* }}} */

GoomSL *
gsl_new (void)
{                               /* {{{ */
  GoomSL *gss = (GoomSL *) malloc (sizeof (GoomSL));

  gss->iflow = iflow_new ();
  gss->vars = goom_hash_new ();
  gss->functions = goom_hash_new ();
  gss->nbStructID = 0;
  gss->structIDS = goom_hash_new ();
  gss->gsl_struct_size = 32;
  gss->gsl_struct =
      (GSL_Struct **) malloc (gss->gsl_struct_size * sizeof (GSL_Struct *));
  gss->currentNS = 0;
  gss->namespaces[0] = gss->vars;
  gss->data_heap = goom_heap_new ();

  reset_scanner (gss);

  gss->compilationOK = 0;
  gss->nbPtr = 0;
  gss->ptrArraySize = 256;
  gss->ptrArray = (void **) malloc (gss->ptrArraySize * sizeof (void *));
#ifdef USE_JITC_X86
  gss->jitc = NULL;
#endif
  return gss;
}                               /* }}} */

void
gsl_bind_function (GoomSL * gss, const char *fname,
    GoomSL_ExternalFunction func)
{                               /* {{{ */
  HashValue *val = goom_hash_get (gss->functions, fname);

  if (val) {
    ExternalFunctionStruct *gef = (ExternalFunctionStruct *) val->ptr;

    gef->function = func;
  } else
    fprintf (stderr, "Unable to bind function %s\n", fname);
}                               /* }}} */

int
gsl_is_compiled (GoomSL * gss)
{                               /* {{{ */
  return gss->compilationOK;
}                               /* }}} */

void
gsl_free (GoomSL * gss)
{                               /* {{{ */
  iflow_free (gss->iflow);
  free (gss->vars);
  free (gss->functions);
  free (gss);
}                               /* }}} */


static int gsl_nb_import;
static char gsl_already_imported[256][256];

char *
gsl_init_buffer (const char *fname)
{
  char *fbuffer;

  fbuffer = (char *) malloc (512);
  fbuffer[0] = 0;
  gsl_nb_import = 0;
  if (fname)
    gsl_append_file_to_buffer (fname, &fbuffer);
  return fbuffer;
}

static char *
gsl_read_file (const char *fname)
{
  FILE *f;
  char *buffer;
  int fsize;

  f = fopen (fname, "rt");
  if (!f) {
    fprintf (stderr, "ERROR: Could not load file %s\n", fname);
    exit (1);
  }
  fseek (f, 0, SEEK_END);
  fsize = ftell (f);
  rewind (f);
  buffer = (char *) malloc (fsize + 512);
  fread (buffer, 1, fsize, f);
  fclose (f);
  buffer[fsize] = 0;
  return buffer;
}

void
gsl_append_file_to_buffer (const char *fname, char **buffer)
{
  char *fbuffer;
  int size, fsize, i = 0;
  char reset_msg[256];

  /* look if the file have not been already imported */
  for (i = 0; i < gsl_nb_import; ++i) {
    if (strcmp (gsl_already_imported[i], fname) == 0)
      return;
  }

  /* add fname to the already imported files. */
  strcpy (gsl_already_imported[gsl_nb_import++], fname);

  /* load the file */
  fbuffer = gsl_read_file (fname);
  fsize = strlen (fbuffer);

  /* look for #import */
  while (fbuffer[i]) {
    if ((fbuffer[i] == '#') && (fbuffer[i + 1] == 'i')) {
      char impName[256];
      int j;

      while (fbuffer[i] && (fbuffer[i] != ' '))
        i++;
      i++;
      j = 0;
      while (fbuffer[i] && (fbuffer[i] != '\n'))
        impName[j++] = fbuffer[i++];
      impName[j++] = 0;
      gsl_append_file_to_buffer (impName, buffer);
    }
    i++;
  }

  sprintf (reset_msg, "\n#FILE %s#\n#RST_LINE#\n", fname);
  strcat (*buffer, reset_msg);
  size = strlen (*buffer);
  *buffer = (char *) realloc (*buffer, size + fsize + 256);
  strcat ((*buffer) + size, fbuffer);
  free (fbuffer);
}