jasmc.opp.exp 56 KB
Newer Older
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
File "jasmc.mly", line 153, characters 7-10:
Warning: the token EOF is unused.
%{
  open Javalib_pack
  open Javalib

  type switchcase = 
    | CaseIntWord of int * string
    | CaseIntInt of int * int

  type switchdefault =
    | DefaultWord of string
    | DefaultInt of int

  type pcode =
    | DCatchIII of JBasics.class_name * int * int * int

  let unescape s =
    Scanf.unescaped (String.sub s 1 ((String.length s)- 2))

  let cbasic = function 
      | "boolean" -> "Z"
      | "byte"    -> "B"
      | "char"    -> "C"
      | "short"   -> "S"
      | "int"     -> "I"
      | "long"    -> "J"
      | "float"   -> "F"
      | "double"  -> "D"
      | "void"    -> "V"
      | _ -> assert false

  let cf_access a =
    if List.mem `Public a then `Public else
    if List.mem `Protected a then `Protected else
    if List.mem `Private a then `Private else
    `Default

  let replace_dot s =
    let s = String.copy s in
    for i = 0 to String.length s - 1 do
      if s.[i] = '/' then s.[i] <- '.'
    done;
    s

  let split_method name =
    let pos = String.index name '(' in
    (String.sub name 0 pos, String.sub name pos ((String.length name) - pos))

  let split_obj name =
    let name = replace_dot name in
    let pos = String.rindex name '.' in
    (String.sub name 0 pos, String.sub name (pos+1) ((String.length name) - (pos+1)))

  let sourcefile = ref None
  let cn = ref (JBasics.make_cn "_")
  let limit_stack = ref 0
  let limit_locals = ref 0
  let pos = ref 0
  let lines = ref []
  let excs = ref []
  let throws = ref []
  let label2pos = Hashtbl.create 16
  let pos2label = Hashtbl.create 16

  let init_method () =
    limit_stack := 255;
    limit_locals := 255;
    pos := 0;
    lines := [];
    excs := [];
    throws := [];
    Hashtbl.clear label2pos;
    Hashtbl.clear pos2label

  let label2int l =
    if (Hashtbl.mem label2pos l) then Hashtbl.find label2pos l else
    let pos = -(1 + (Hashtbl.length pos2label)) in
    Hashtbl.add pos2label pos l;
    pos

  let addlabel l i =
    Hashtbl.add label2pos l i

  let add n c =
    let p = !pos in
    pos := !pos + n;
    (p, c)

  let mkcode codes =

    let realloc p l =
      if l >= 0 then l - p else
      let label = Hashtbl.find pos2label l in
      (Hashtbl.find label2pos label) - p

    in
    let code = Array.create !pos JCode.OpInvalid in
    List.iter (function 
      | (p, JCode.OpInvalid) -> ()
      | (p, JCode.OpJsr(l)) -> code.(p) <- JCode.OpJsr(realloc p l)
      | (p, JCode.OpIf(a,l)) -> code.(p) <- JCode.OpIf(a,realloc p l)
      | (p, JCode.OpIfCmp(a,l)) -> code.(p) <- JCode.OpIfCmp(a,realloc p l)
      | (p, JCode.OpGoto(l)) -> code.(p) <- JCode.OpGoto(realloc p l)
      | (p, JCode.OpLookupSwitch(d,cases)) ->
        let cases = List.map(fun(a,b)-> (a ,realloc p b)) cases in
        code.(p) <- JCode.OpLookupSwitch(realloc p d ,cases)
      | (p, JCode.OpTableSwitch (def, low, high, defs)) ->
        let defs = Array.map (realloc p) defs in
        code.(p) <- JCode.OpTableSwitch(realloc p def,low, high, defs)
      | (p, c) -> code.(p) <- c
    ) codes;
    let lines = if !lines = [] then None else Some (List.rev !lines) in
    let excs = List.map (fun exc ->
        {
          exc with
          JCode.e_start = realloc 0 exc.JCode.e_start;
          e_end = realloc 0 exc.e_end;
          e_handler = realloc 0 exc.e_handler
        }
    ) (List.rev !excs) in
    let throws = List.rev !throws in
    (code,lines, excs, throws)
%}
%start jas_file
%token <string> Word
%token VOLATILE
%token VISIBLEPARAM
%token VISIBLE
%token VARARGS
%token USING
%token USE
%token TRANSIENT
%token TO
%token TABLESWITCH
%token <string> Str
%token SYNTHETIC
%token SYNCHRONIZED
%token STRICT
%token STATIC
%token STACK
%token SIGNATURE
%token SEP
%token <string> Relative
%token PUBLIC
%token PROTECTED
%token PRIVATE
%token OUTER
%token OFFSET
%token <string> Num
%token NATIVE
%token METHOD
%token LOOKUPSWITCH
%token LOCALS
%token <int> Int
%token <string * string> Insn
%token IS
%token INVISIBLEPARAM
%token INVISIBLE
%token INTERFACE
%token INNER
%token FROM
%token FINAL
%token FIELD
%token EQ
%token EOF
%token ENUM
%token DVAR
%token DTHROWS
%token DSUPER
%token DSTACK
%token DSOURCE
%token DSIGNATURE
%token DSET
%token DMETHOD
%token DLINE
%token DLIMIT
%token DINTERFACE
%token DINNER
%token DIMPLEMENTS
%token DFIELD
%token DEND
%token DENCLOSING
%token DEFAULT
%token DDEPRECATED
%token DDEBUG
%token DCLASS
%token DCATCH
%token DBYTECODE
%token DATTRIBUTE
%token DANNOTATION
%token COLON
%token CLASS
%token BRIDGE
%token ANNOTATION
%token ABSTRACT

%type <Javalib_pack.JCode.jcode Javalib_pack.Javalib.interface_or_class> jas_file
%%

jas_file:
| _1 = sep _2 = jasmin_header _3 = inners _4 = fields _5 = methods
    {    ( _2 _3 _4 _5 )}

sep:
| _1 = SEP
    {        ( () )}
| 
    {    ( () )}

jasmin_header:
| _1 = bytecode_spec _2 = source_spec _3 = class_spec _4 = super_spec _5 = implements _6 = signature_spec _7 = enclosing_spec _8 = deprecated_spec _9 = annotations _10 = generic_attributes _11 = debug_extension
    {    (

      let (class_or_interface, access, name) = _3 in

      match class_or_interface with
      | "class" ->
        (fun inners fields methods ->
          let fields = List.fold_left (fun fields (fs,f) ->
            JBasics.FieldMap.add fs f fields
          ) JBasics.FieldMap.empty fields in
          let methods = List.fold_left (fun methods (ms,m) ->
            JBasics.MethodMap.add ms m methods
          ) JBasics.MethodMap.empty methods in

          JClass {
            c_name = name;
            c_version = _1;
            c_access = if List.mem `Public access then `Public else `Default;
            c_final = List.mem `Final access;
            c_abstract = List.mem `Abstract access;
            c_super_class = _4;
            c_generic_signature = _6;
            c_fields = fields;
            c_interfaces = _5;
            c_consts = [||];
            c_sourcefile = _2;
            c_deprecated = _8;
            c_enclosing_method = _7;
            c_source_debug_extention = _11;
            c_inner_classes = inners;
            c_synthetic = List.mem `Synthetic access;
            c_enum = List.mem `Enum access;
            c_annotations = _9;
            c_other_flags = [];
            c_other_attributes = _10;
            c_methods = methods;
          })
      | "interface" ->
        (fun inners fields methods ->
          let fields = List.fold_left (fun fields (fs,f) ->
            let f = {
                if_signature = f.cf_signature ;
                if_class_signature = f.cf_class_signature;
                if_generic_signature = f.cf_generic_signature;
                if_synthetic = f.cf_synthetic;
                if_value = f.cf_value;
                if_annotations = f.cf_annotations;
                if_other_flags = f.cf_other_flags;
                if_attributes = f.cf_attributes;
              }
            in
            JBasics.FieldMap.add fs f fields
          ) JBasics.FieldMap.empty fields in
          let methods = List.fold_left (fun methods -> function
            (ms, ConcreteMethod {
              cm_signature = signature;
              cm_class_method_signature = class_method_signature;
              cm_access = access;
              cm_generic_signature = generic_signature;
              cm_bridge = bridge;
              cm_varargs = varargs;
              cm_synthetic = synthetic;
              cm_other_flags = other_flags;
              cm_exceptions = exceptions;
              cm_attributes = attributes;
              cm_annotations = annotations;
            }) ->
            let m = {
              am_signature = signature;
              am_class_method_signature = class_method_signature;
              am_access =
                begin match access with
                | `Default -> `Default
                | `Protected -> `Protected
                | `Public -> `Public
                | `Private -> assert false
                end
              ;
              am_generic_signature = generic_signature;
              am_bridge = bridge;
              am_varargs = varargs;
              am_synthetic = synthetic;
              am_other_flags = other_flags;
              am_exceptions = exceptions;
              am_attributes = attributes;
              am_annotations = annotations;
              am_annotation_default = None;
            } in
            JBasics.MethodMap.add ms m methods
          ) JBasics.MethodMap.empty methods in
          JInterface {
            i_name = name;
            i_version = _1;
            i_access = if List.mem `Public access then `Public else `Default;
            i_interfaces = _5;
            i_generic_signature = _6;
            i_consts = [||];
            i_sourcefile = _2;
            i_deprecated = _8;
            i_source_debug_extention = _11;
            i_inner_classes = inners;
            i_initializer = None;
            i_annotation = false;
            i_annotations = _9;
            i_other_attributes = _10;
            i_other_flags = [];
            i_fields = fields;
            i_methods = methods;
          }
        )
    )}

signature_spec:
| _1 = DSIGNATURE _2 = signature_expr _3 = SEP
    {                                  ( Some _2 )}
| 
    {                ( None )}

signature_expr:
| _1 = Str
    {        ( JParseSignature.parse_ClassSignature _1 )}

deprecated_spec:
| _1 = DDEPRECATED _2 = deprecated_expr _3 = SEP
    {                                    ( true )}
| 
    {                  ( false )}

deprecated_expr:
| 
    {    ( () )}

bytecode_spec:
| _1 = DBYTECODE _2 = Num _3 = SEP
    {    (
      let (major, minor) =
        begin try
          let pos = String.index _2 '.' in
          let len = String.length _2 in
          (String.sub _2 0 pos, String.sub _2 (pos+1) (len-pos-1))
        with e -> (_2,"0")
        end
      in
      { JBasics.major = int_of_string major; minor = int_of_string minor }
    )}
| 
    {    (
      { JBasics.major = 45; minor = 3 }
    )}

source_spec:
| _1 = DSOURCE _2 = Str _3 = SEP
    {                    ( Some _2 )}
| _1 = DSOURCE _2 = Word _3 = SEP
    {                     ( Some _2 )}
| 
    {                  ( !sourcefile )}

class_spec:
| _1 = DCLASS _2 = access _3 = classname _4 = SEP
    {    (
      cn := _3;
      ("class",_2,_3)
    )}
| _1 = DINTERFACE _2 = access _3 = classname _4 = SEP
    {    (
      cn := _3;
      ("interface",_2,_3)
    )}

classname:
| _1 = Word
    {         ( JBasics.make_cn (replace_dot _1) )}

access:
| _1 = access_list
    {                ( _1 )}

access_list:
| _1 = access_items
    {                   ( _1 )}
| 
    {      ( [`Default] )}

access_items:
| _1 = access_item _2 = access_items
    {                                 ( _1::_2 )}
| _1 = access_item
    {                    ( [_1] )}

access_item:
| _1 = PUBLIC
    {                 ( `Public )}
| _1 = PRIVATE
    {                  ( `Private )}
| _1 = PROTECTED
    {                    ( `Protected )}
| _1 = STATIC
    {                 ( `Static )}
| _1 = FINAL
    {                ( `Final )}
| _1 = SYNCHRONIZED
    {                       ( `Synchronized )}
| _1 = VOLATILE
    {                   ( `Volatile )}
| _1 = TRANSIENT
    {                    ( `Transient )}
| _1 = NATIVE
    {                 ( `Native )}
| _1 = INTERFACE
    {                    ( `Interface )}
| _1 = ABSTRACT
    {                   ( `Abstract )}
| _1 = ANNOTATION
    {                     ( `Annotation )}
| _1 = ENUM
    {               ( `Enum )}
| _1 = BRIDGE
    {                 ( `Bridge )}
| _1 = VARARGS
    {                  ( `Varargs )}
| _1 = STRICT
    {                 ( `Strict )}
| _1 = SYNTHETIC
    {                    ( `Synthetic )}

super_spec:
| _1 = DSUPER _2 = classname _3 = SEP
    {                         ( Some (_2) )}

implements:
| _1 = implements_list
    {                    ( _1 )}
| 
    {                ( [] )}

implements_list:
| _1 = implements_spec _2 = implements_list
    {                                      ( _1::_2 )}
| _1 = implements_spec
    {                      ( [_1] )}

implements_spec:
| _1 = DIMPLEMENTS _2 = classname _3 = SEP
    {                                  ( _2 )}

annotations:
| _1 = ann_cls_list
    {                 ( [] )}
| 
    {                ( [] )}

ann_cls_list:
| _1 = ann_cls_list _2 = ann_cls_spec
    {      ( _2::_1 )}
| _1 = ann_cls_spec
    {      ( [_1] )}

ann_cls_spec:
| _1 = ann_cls_expr _2 = ann_arglist _3 = endannotationsep
    {        ( "" )}

ann_cls_expr:
| _1 = DANNOTATION _2 = ann_clf_expr
    {          ( () )}

endannotationsep:
| _1 = endannotation _2 = SEP
    {                      ( () )}

endannotation:
| _1 = DEND _2 = ANNOTATION
    {                    ( () )}

ann_clf_expr:
| _1 = VISIBLE _2 = classname _3 = SEP
    {    ( () )}
| _1 = INVISIBLE _2 = classname _3 = SEP
    {    ( () )}

ann_met_expr:
| _1 = VISIBLE _2 = classname _3 = SEP
    {    ( () )}
| _1 = INVISIBLE _2 = classname _3 = SEP
    {    ( () )}
| _1 = VISIBLEPARAM _2 = Int _3 = classname _4 = SEP
    {    ( () )}
| _1 = INVISIBLEPARAM _2 = Int _3 = classname _4 = SEP
    {    ( () )}

ann_arglist:
| _1 = ann_arg_list
    {                 ( [] )}
| 
    {                ( [] )}

ann_arg_list:
| _1 = ann_arg_list _2 = ann_arg_spec
    {      ( _2::_1 )}
| _1 = ann_arg_spec
    {      ( [_1] )}

ann_arg_spec:
| _1 = ann_arg_expr _2 = EQ _3 = ann_value_list
    {        ( (_1,_3) )}

ann_arg_expr:
| _1 = Word _2 = Word
    {          ( "" )}
| _1 = Word _2 = Word _3 = Word
    {          ( "" )}

ann_def_spec:
| _1 = DEFAULT _2 = SEP
    {    ( () )}

ann_value_list:
| _1 = ann_value_items _2 = SEP
    {    ( _1 )}
| _1 = ann_ann_list
    {    ( _1 )}

ann_value_items:
| _1 = ann_value_items _2 = ann_value
    {    ( _2::_1 )}
| _1 = ann_value
    {    ( [_1] )}

ann_value:
| _1 = any_item
    {      ( "" )}

ann_ann_list:
| _1 = ann_ann_list _2 = ann_ann_value
    {                               ( _2::_1 )}
| _1 = ann_ann_value
    {                  ( [_1] )}

ann_ann_value:
| _1 = DANNOTATION _2 = ann_nest _3 = ann_arglist _4 = endannotationsep
    {      ( "" )}

ann_nest:
| _1 = SEP
    {            ( () )}

ann_def_val:
| _1 = ann_def_expr _2 = EQ _3 = ann_value_list
    {    ( () )}

ann_def_expr:
| _1 = Word
    {      ( () )}
| _1 = Word _2 = Word
    {      ( () )}

debug_extension:
| _1 = debug_list
    {               ( Some (String.concat " " (List.rev _1)) )}
| 
    {                ( None )}

debug_list:
| _1 = debug_list _2 = debug_spec
    {      ( _2::_1 )}
| _1 = debug_spec
    {      ( [_1] )}

debug_spec:
| _1 = DDEBUG _2 = Str _3 = SEP
    {                       ( _2 )}

enclosing_spec:
| _1 = DENCLOSING _2 = METHOD _3 = Word _4 = SEP
    {    (
      let (name, md) = split_method _3 in
      let (vts, ovt) = JParseSignature.parse_method_descriptor md in
      let ms = JBasics.make_ms name vts ovt in
      Some (!cn,Some (ms))
    )}
| 
    {                  ( None )}

generic_attributes:
| _1 = generic_list
    {                 ( _1 )}
| 
    {                ( [] )}

generic_list:
| _1 = generic_list _2 = generic_spec
    {                                ( _2::_1 )}
| _1 = generic_spec
    {                   ( [_1] )}

generic_spec:
| _1 = DATTRIBUTE _2 = generic_expr _3 = SEP
    {                                    ( _2 )}

generic_expr:
| _1 = Word _2 = Str
    {             ( (_1,_2) )}

fields:
| _1 = field_list
    {               ( _1 )}
| 
    {    ( [] )}

field_list:
| _1 = field_spec _2 = field_list
    {                            ( _1::_2 )}
| _1 = field_spec
    {                 ( [_1] )}

field_spec:
| _1 = DFIELD _2 = access _3 = Word _4 = Word _5 = SIGNATURE _6 = Str _7 = optional_default _8 = SEP
    {        (
          let fs = JBasics.make_fs _3 (JParseSignature.parse_field_descriptor _4) in
          let f = {
            cf_signature = fs;
            cf_class_signature = JBasics.make_cfs !cn fs;
            cf_generic_signature = None;
            cf_access = cf_access _2;
            cf_static = List.mem `Static _2;
            cf_synthetic = List.mem `Synthetic _2;
            cf_enum = List.mem `Enum _2;
            cf_kind = if List.mem `Final _2 then Final else NotFinal;
            cf_value = None;
            cf_transient = false;
            cf_annotations = [];
            cf_other_flags = [];
            cf_attributes = {
              synthetic = false; deprecated = false; other = []
            }
          } in
          (fs,f)
        )}
| _1 = DFIELD _2 = access _3 = Word _4 = Word _5 = optional_default _6 = SEP
    {        (
          let fs = JBasics.make_fs _3 (JParseSignature.parse_field_descriptor _4) in
          let f = {
            cf_signature = fs;
            cf_class_signature = JBasics.make_cfs !cn fs;
            cf_generic_signature = None;
            cf_access = cf_access _2;
            cf_static = List.mem `Static _2;
            cf_synthetic = List.mem `Synthetic _2;
            cf_enum = List.mem `Enum _2;
            cf_kind = if List.mem `Final _2 then Final else NotFinal;
            cf_value = None;
            cf_transient = false;
            cf_annotations = [];
            cf_other_flags = [];
            cf_attributes = {
              synthetic = false; deprecated = false; other = []
            }
          } in
          (fs,f)
        )}
| _1 = DFIELD _2 = field_start _3 = field_exts _4 = endfield
    {        (
          let fs = JBasics.make_fs "" (JParseSignature.parse_field_descriptor _2) in
          let f = {
            cf_signature = fs;
            cf_class_signature = JBasics.make_cfs !cn fs;
            cf_generic_signature = None;
            cf_access = `Default;
            cf_static = false;
            cf_synthetic = false;
            cf_enum = false;
            cf_kind = NotFinal;
            cf_value = None;
            cf_transient = false;
            cf_annotations = [];
            cf_other_flags = [];
            cf_attributes = {
              synthetic = false; deprecated = false; other = []
            }
          } in
          (fs,f)
        )}

optional_default:
| _1 = EQ _2 = item
    {                  ( Some _2 )}
| 
    {                      ( None )}

field_start:
| _1 = access _2 = Word _3 = Word _4 = optional_default _5 = SEP
    {          ( "" )}

endfield:
| _1 = DEND _2 = FIELD _3 = SEP
    {          ( () )}

field_exts:
| _1 = field_ext_list
    {                         ( _1 )}
| 
    {                      ( [] )}

field_ext_list:
| _1 = field_ext_list _2 = field_ext_expr
    {                                          ( _2::_1 )}
| _1 = field_ext_expr
    {                           ( [_1] )}

field_ext_expr:
| _1 = DSIGNATURE _2 = signature_expr _3 = SEP
    {              ( "" )}
| _1 = DATTRIBUTE _2 = generic_expr _3 = SEP
    {              ( "" )}
| _1 = DDEPRECATED _2 = deprecated_expr _3 = SEP
    {              ( "" )}
| _1 = DANNOTATION _2 = ann_clf_expr _3 = ann_arglist _4 = endannotationsep
    {              ( "" )}

item:
| _1 = Int
    {    ( () )}
| _1 = Num
    {    ( () )}
| _1 = Str
    {    ( () )}

any_item:
| _1 = Word
    {    ( () )}
| _1 = item
    {    ( () )}

inners:
| _1 = inner_list
    {               ( List.rev _1 )}
| 
    {                ( [] )}

inner_list:
| _1 = inner_list _2 = inner_spec
    {                            ( _2::_1 )}
| _1 = inner_spec
    {                 ( [_1] )}

inner_spec:
| _1 = DINNER _2 = CLASS _3 = access _4 = inner_name _5 = inner_inner _6 = inner_outer _7 = SEP
    {        ( 
          {
            ic_class_name = _5;
            ic_outer_class_name = _6;
            ic_source_name = _4;
            ic_access = cf_access _3;
            ic_static = List.mem `Static _3;
            ic_final = List.mem `Final _3;
            ic_synthetic = List.mem `Synthetic _3;
            ic_annotation = List.mem `Annotation _3;
            ic_enum = List.mem `Enum _3;
            ic_other_flags = []; (* TODO *)
            ic_type = `ConcreteClass (* TODO `Abstract *)
          }
        )}
| _1 = DINNER _2 = INTERFACE _3 = access _4 = inner_name _5 = inner_inner _6 = inner_outer _7 = SEP
    {        (
          {
            ic_class_name = _5;
            ic_outer_class_name = _6;
            ic_source_name = _4;
            ic_access = cf_access _3;
            ic_static = List.mem `Static _3;
            ic_final = List.mem `Final _3;
            ic_synthetic = List.mem `Synthetic _3;
            ic_annotation = List.mem `Annotation _3;
            ic_enum = List.mem `Enum _3;
            ic_other_flags = []; (* TODO *)
            ic_type = `Interface
          }
        )}

inner_name:
| _1 = Word
    {               ( Some _1 )}
| 
    {                      ( None )}

inner_inner:
| _1 = INNER _2 = classname
    {                          ( Some _2 )}
| 
    {                      ( None )}

inner_outer:
| _1 = OUTER _2 = classname
    {                          ( Some _2 )}
| 
    {                      ( None )}

methods:
| _1 = method_list
    {                ( _1 )}
| 
    {                ( [] )}

method_list:
| _1 = method_spec
    {                  ( [_1] )}
| _1 = method_list _2 = method_spec
    {                              ( _2::_1 )}

method_spec:
| _1 = defmethod _2 = statements _3 = endmethod
    {        (
          let(access,ms) = _1 in
          let code,lines,excs,throws = mkcode (List.rev _2) in
          let jmethod = {
            JCode.c_max_stack = !limit_stack;
            c_max_locals = !limit_locals;
            c_code = code;
            c_exc_tbl = excs;
            c_line_number_table = lines;
            c_local_variable_table = None; (* TODO *)
            c_local_variable_type_table = None; (* TODO *)
            c_stack_map_midp = None; (* TODO *)
            c_stack_map_java6 = None; (* TODO *)
            c_attributes = []; (* TODO *)
          }
          in  
          let m = ConcreteMethod {
            cm_signature = ms;
            cm_class_method_signature = JBasics.make_cms !cn ms;
            cm_static = List.mem `Static access;
            cm_final = List.mem `Final access;
            cm_synchronized = List.mem `Synchronized access;
            cm_strict = List.mem `Strict access;
            cm_access = cf_access access;
            cm_generic_signature = None; (* TODO *)
            cm_bridge = List.mem `Bridge access;
            cm_varargs = List.mem `Varargs access;
            cm_synthetic = List.mem `Synthetic access;
            cm_other_flags = []; (* TODO *)
            cm_exceptions = throws; (* TODO *)
            cm_attributes = { synthetic = false; deprecated = false; other = [] };  (* TODO *)
            cm_annotations = { ma_global = []; ma_parameters = [] }; (* TODO *)
            cm_implementation = Java (lazy jmethod)
          } in (ms,m)
        )}
| _1 = defmethod _2 = endmethod
    {        (
          let(access,ms) = _1 in
          let code =[||] in
          let jmethod = {
            JCode.c_max_stack = 0;
            c_max_locals = 0;
            c_code = code;
            c_exc_tbl = [];
            c_line_number_table = None;
            c_local_variable_table = None;
            c_local_variable_type_table = None;
            c_stack_map_midp = None;
            c_stack_map_java6 = None;
            c_attributes = []
          }
          in
          let m = ConcreteMethod {
            cm_signature = ms;
            cm_class_method_signature = JBasics.make_cms !cn ms;
            cm_static = List.mem `Static access;
            cm_final = List.mem `Final access;
            cm_synchronized = List.mem `Synchronized access;
            cm_strict = List.mem `Strict access;
            cm_access = cf_access access;
            cm_generic_signature = None;
            cm_bridge = List.mem `Bridge access;
            cm_varargs = List.mem `Varargs access;
            cm_synthetic = List.mem `Synthetic access;
            cm_other_flags = [];
            cm_exceptions = [];
            cm_attributes = { synthetic = false; deprecated = false; other = [] };
            cm_annotations = { ma_global = []; ma_parameters = [] };
            cm_implementation = Java (lazy jmethod)
          } in (ms,m)
        )}

defmethod:
| _1 = DMETHOD _2 = access _3 = Word _4 = SEP
    {          (
            init_method();
            let (name, md) = split_method _3 in
            let (vts, ovt) = JParseSignature.parse_method_descriptor md in
            let ms = JBasics.make_ms name vts ovt
            in (_2, ms)
          )}

endmethod:
| _1 = DEND _2 = METHOD _3 = SEP
    {                          ( () )}

statements:
| _1 = statements _2 = statement
    {                               ( _2::_1 )}
| _1 = statement
    {                    ( [_1] )}

statement:
| _1 = stmnt _2 = SEP
    {                      ( _1 )}

stmnt:
| _1 = instruction
    {                          ( _1 )}
| _1 = directive
    {                        ( _1 )}
| _1 = error
    {                    ( add 0 JCode.OpInvalid )}
| _1 = label
    {                    ( _1 )}
| 
    {                          ( add 0 JCode.OpInvalid )}

label:
| _1 = Word _2 = COLON
    {                           (
                addlabel _1 !pos;
                add 0 JCode.OpInvalid
               )}
| _1 = Int _2 = COLON _3 = instruction
    {                                      (
                let (pos, _) = _3 in
                addlabel (string_of_int _1) pos;
                _3
              )}

directive:
| _1 = DVAR _2 = var_expr
    {                (
                  failwith "TODO: .var"
                )}
| _1 = DLIMIT _2 = limit_expr
    {                                  ( add 0 JCode.OpInvalid )}
| _1 = DLINE _2 = line_expr
    {                                ( add 0 JCode.OpInvalid )}
| _1 = DTHROWS _2 = throws_expr
    {                                    ( add 0 JCode.OpInvalid )}
| _1 = DCATCH _2 = catch_expr
    {                                  ( add 0 JCode.OpInvalid )}
| _1 = DSET _2 = set_expr
    {                (
                  failwith "TODO: .set"
                )}
| _1 = DSIGNATURE _2 = signature_expr
    {                (
                  failwith "TODO: .signature"
                )}
| _1 = DATTRIBUTE _2 = generic_expr
    {                (
                  failwith "TODO: .attribute"
                )}
| _1 = DDEPRECATED _2 = deprecated_expr
    {                (
                  failwith "TODO: .deprected"
                )}
| _1 = DANNOTATION _2 = ann_met_expr _3 = ann_arglist _4 = endannotation
    {                (
                  failwith "TODO: .annotation"
                )}
| _1 = DANNOTATION _2 = ann_def_spec _3 = ann_def_val _4 = endannotation
    {                (
                  failwith "TODO: .annotation"
                )}
| _1 = DSTACK _2 = stackmap
    {                (
                  failwith "TODO: .stack"
                )}

var_expr:
| _1 = Int _2 = IS _3 = Word _4 = Word _5 = optional_signature _6 = FROM _7 = Word _8 = TO _9 = Word
    {                  ( ()  (* TODO *))}
| _1 = Int _2 = IS _3 = Word _4 = Word _5 = optional_signature
    {                  ( ()  (* TODO *))}
| _1 = Int _2 = IS _3 = Word _4 = Word _5 = optional_signature _6 = FROM _7 = Int _8 = TO _9 = Int
    {                  ( ()  (* TODO *))}

optional_signature:
| _1 = SIGNATURE _2 = Str
    {                                  ( Some _2 )}
| 
    {                                ( None )}

limit_expr:
| _1 = LOCALS _2 = Int
    {                             ( limit_locals := _2 )}
| _1 = STACK _2 = Int
    {                            ( limit_stack := _2 )}
| _1 = Word _2 = Int
    {                           ( () )}

line_expr:
| _1 = Int
    {                      ( lines := (!pos, _1) :: !lines )}

throws_expr:
| _1 = classname
    {                            ( throws := _1 :: !throws )}

catch_expr:
| _1 = classname _2 = FROM _3 = Word _4 = TO _5 = Word _6 = USING _7 = Word
    {                  (
                    excs := {
                      JCode.e_start = label2int _3;
                      e_end = label2int _5;
                      e_handler = label2int _7;
                      e_catch_type = Some _1
                    } :: !excs
                  )}
| _1 = classname _2 = FROM _3 = Int _4 = TO _5 = Int _6 = USING _7 = Int
    {                  (
                    excs := {
                      JCode.e_start = _3;
                      e_end = _5;
                      e_handler = _7;
                      e_catch_type = Some _1
                    } :: !excs
                  )}

set_expr:
| _1 = Word _2 = any_item
    {                                ( () )}

stackmap:
| _1 = defstack _2 = stack_map_frame_desc _3 = endstack
    {                  ( () )}
| _1 = USE _2 = defstack_same _3 = stack_map_frame_desc _4 = endstack
    {                  ( () )}

defstack_same:
| _1 = defstack_same_expr _2 = LOCALS _3 = SEP
    {                    ( () )}

defstack_same_expr:
| _1 = Int
    {                    ( () )}
| 
    {                    ( () )}

defstack:
| _1 = SEP
    {                        ( () )}

stack_map_frame_desc:
| _1 = stack_offset_def _2 = stack_items
    {                    ( () )}

stack_offset_def:
| _1 = OFFSET _2 = Int _3 = SEP
    {                    ( () )}
| _1 = OFFSET _2 = Word _3 = SEP
    {                    ( () )}
| 
    {                    ( () )}

stack_items:
| _1 = stack_items _2 = stack_item
    {                                           ( _2::_1 )}
| 
    {                                  ( [] )}

stack_item:
| _1 = stack_item_expr _2 = SEP
    {                                        ( _1 )}

stack_item_expr:
| _1 = LOCALS _2 = Word
    {                    ( "" )}
| _1 = LOCALS _2 = Word _3 = Word
    {                    ( "" )}
| _1 = LOCALS _2 = Word _3 = Int
    {                    ( "" )}
| _1 = STACK _2 = Word
    {                    ( "" )}
| _1 = STACK _2 = Word _3 = Word
    {                    ( "" )}
| _1 = STACK _2 = Word _3 = Int
    {                    ( "" )}

endstack:
| _1 = DEND _2 = STACK
    {                               ( () )}

instruction:
| _1 = simple_instruction
    {                                   ( _1 )}
| _1 = complex_instruction
    {                                    ( _1 )}

simple_instruction:
| _1 = Insn
    {                       (
                  match (fst _1) with
                  (* A *)
                  | "aaload" -> add 1 (JCode.OpArrayLoad `Object)
                  | "aastore" -> add 1 (JCode.OpArrayStore `Object)
                  | "aconst_null" -> add 1 (JCode.OpConst(`ANull))
                  | "aload_0" -> add 1 (JCode.OpLoad (`Object, 0))
                  | "aload_1" -> add 1 (JCode.OpLoad (`Object, 1))
                  | "aload_2" -> add 1 (JCode.OpLoad (`Object, 2))
                  | "aload_3" -> add 1 (JCode.OpLoad (`Object, 3))
                  | "areturn" -> add 1 (JCode.OpReturn `Object)
                  | "arraylength" -> add 1 (JCode.OpArrayLength)
                  | "astore_0" -> add 1 (JCode.OpStore (`Object, 0))
                  | "astore_1" -> add 1 (JCode.OpStore (`Object, 1))
                  | "astore_2" -> add 1 (JCode.OpStore (`Object, 2))
                  | "astore_3" -> add 1 (JCode.OpStore (`Object, 3))
                  | "athrow" -> add 1 (JCode.OpThrow)
                  (* B *)
                  | "baload" ->add 1 (JCode.OpArrayLoad `ByteBool)
                  | "bastore" -> add 1 (JCode.OpArrayStore `ByteBool)
                  | "breakpoint" -> add 1 (JCode.OpBreakpoint)
                  (* C *)
                  | "caload" ->add 1 (JCode.OpArrayLoad `Char)
                  | "castore" -> add 1 (JCode.OpArrayStore `Char)
                  (* D *)
                  | "d2f" -> add 1 (JCode.OpD2F)
                  | "d2i" -> add 1 (JCode.OpD2I)
                  | "d2l" -> add 1 (JCode.OpD2L)
                  | "dadd" -> add 1 (JCode.OpAdd `Double)
                  | "daload" ->add 1 (JCode.OpArrayLoad `Double)
                  | "dastore" -> add 1 (JCode.OpArrayStore `Double)
                  | "dcmpg" -> add 1 (JCode.OpCmp `DG)
                  | "dcmpl" -> add 1 (JCode.OpCmp `DL)
                  | "dconst_0" -> add 1 (JCode.OpConst(`Double (0.)))
                  | "dconst_1" -> add 1 (JCode.OpConst(`Double (1.)))
                  | "ddiv" -> add 1 (JCode.OpDiv `Double)
                  | "dload_0" -> add 1 (JCode.OpLoad (`Double, 0))
                  | "dload_1" -> add 1 (JCode.OpLoad (`Double, 1))
                  | "dload_2" -> add 1 (JCode.OpLoad (`Double, 2))
                  | "dload_3" -> add 1 (JCode.OpLoad (`Double, 3))
                  | "dmul" -> add 1 (JCode.OpMult `Double)
                  | "dneg" -> add 1 (JCode.OpNeg `Double)
                  | "drem" -> add 1 (JCode.OpRem `Double)
                  | "dreturn" -> add 1 (JCode.OpReturn `Double)
                  | "dstore_0" -> add 1 (JCode.OpStore (`Double, 0))
                  | "dstore_1" -> add 1 (JCode.OpStore (`Double, 1))
                  | "dstore_2" -> add 1 (JCode.OpStore (`Double, 2))
                  | "dstore_3" -> add 1 (JCode.OpStore (`Double, 3))
                  | "dsub" -> add 1 (JCode.OpSub `Double)
                  | "dup" -> add 1 (JCode.OpDup)
                  | "dup2" -> add 1 (JCode.OpDup2)
                  | "dup2_x1" -> add 1 (JCode.OpDup2X1)
                  | "dup2_x2" -> add 1 (JCode.OpDup2X2)
                  | "dup_x1" -> add 1 (JCode.OpDupX1)
                  | "dup_x2" -> add 1 (JCode.OpDupX2)
                  (* F *)
                  | "f2i" -> add 1 (JCode.OpF2I)
                  | "f2l" -> add 1 (JCode.OpF2L)
                  | "f2d" -> add 1 (JCode.OpF2D)
                  | "fadd" -> add 1 (JCode.OpAdd `Float)
                  | "faload" ->add 1 (JCode.OpArrayLoad `Float)
                  | "fastore" -> add 1 (JCode.OpArrayStore `Float)
                  | "fcmpg" -> add 1 (JCode.OpCmp `FG)
                  | "fcmpl" -> add 1 (JCode.OpCmp `FL)
                  | "fconst_0" -> add 1(JCode.OpConst(`Float (0.)))
                  | "fconst_1" -> add 1(JCode.OpConst(`Float (1.)))
                  | "fconst_2" -> add 1(JCode.OpConst(`Float (2.)))
                  | "fdiv" -> add 1 (JCode.OpDiv `Float)
                  | "fload_0" -> add 1 (JCode.OpLoad (`Float, 0))
                  | "fload_1" -> add 1 (JCode.OpLoad (`Float, 1))
                  | "fload_2" -> add 1 (JCode.OpLoad (`Float, 2))
                  | "fload_3" -> add 1 (JCode.OpLoad (`Float, 3))
                  | "fmul" -> add 1 (JCode.OpMult `Float)
                  | "fneg" -> add 1 (JCode.OpNeg `Float)
                  | "frem" -> add 1 (JCode.OpRem `Float)
                  | "freturn" -> add 1 (JCode.OpReturn `Float)
                  | "fstore_0" -> add 1 (JCode.OpStore (`Float, 0))
                  | "fstore_1" -> add 1 (JCode.OpStore (`Float, 1))
                  | "fstore_2" -> add 1 (JCode.OpStore (`Float, 2))
                  | "fstore_3" -> add 1 (JCode.OpStore (`Float, 3))
                  | "fsub" -> add 1 (JCode.OpSub `Float)
                  (* I *)
                  | "i2f" -> add 1 (JCode.OpI2F)
                  | "i2d" -> add 1 (JCode.OpI2D)
                  | "i2l" -> add 1 (JCode.OpI2L)
                  | "iadd" -> add 1 (JCode.OpAdd `Int2Bool)
                  | "iaload" -> add 1 (JCode.OpArrayLoad `Int)
                  | "iand" -> add 1 (JCode.OpIAnd)
                  | "iastore" -> add 1 (JCode.OpArrayStore `Int)
                  | "iconst_0" -> add 1 (JCode.OpConst(`Int (0l)))
                  | "iconst_1" -> add 1 (JCode.OpConst(`Int (1l)))
                  | "iconst_2" -> add 1 (JCode.OpConst(`Int (2l)))
                  | "iconst_3" -> add 1 (JCode.OpConst(`Int (3l)))
                  | "iconst_4" -> add 1 (JCode.OpConst(`Int (4l)))
                  | "iconst_5" -> add 1 (JCode.OpConst(`Int (5l)))
                  | "iconst_m1" -> add 1 (JCode.OpConst(`Int (-1l)))
                  | "idiv" -> add 1 (JCode.OpDiv `Int2Bool)
                  | "iload_0" -> add 1 (JCode.OpLoad (`Int2Bool, 0))
                  | "iload_1" -> add 1 (JCode.OpLoad (`Int2Bool, 1))
                  | "iload_2" -> add 1 (JCode.OpLoad (`Int2Bool, 2))
                  | "iload_3" -> add 1 (JCode.OpLoad (`Int2Bool, 3))
                  | "imul" -> add 1 (JCode.OpMult `Int2Bool)
                  | "ineg" -> add 1 (JCode.OpNeg `Int2Bool)
                  | "int2byte" -> add 1 (JCode.OpI2B)
                  | "int2char" -> add 1 (JCode.OpI2C)
                  | "int2short" -> add 1 (JCode.OpI2S)
                  (*
                  | "invokedynamic", "method" -> add 1 ()
                  *)
                  | "ior" -> add 1 (JCode.OpIOr)
                  | "irem" -> add 1 (JCode.OpRem `Int2Bool)
                  | "ireturn" -> add 1 (JCode.OpReturn `Int2Bool)
                  | "ishl" -> add 1 (JCode.OpIShl)
                  | "ishr" -> add 1 (JCode.OpIShr)
                  | "istore_0" -> add 1 (JCode.OpStore (`Int2Bool, 0))
                  | "istore_1" -> add 1 (JCode.OpStore (`Int2Bool, 1))
                  | "istore_2" -> add 1 (JCode.OpStore (`Int2Bool, 2))
                  | "istore_3" -> add 1 (JCode.OpStore (`Int2Bool, 3))
                  | "isub" -> add 1 (JCode.OpSub `Int2Bool)
                  | "iushr" -> add 1 (JCode.OpIUShr)
                  | "ixor" -> add 1 (JCode.OpIXor)
                  (* L *)
                  | "l2f" -> add 1 (JCode.OpL2F)
                  | "l2d" -> add 1 (JCode.OpL2D)
                  | "l2i" -> add 1 (JCode.OpL2I)
                  | "ladd" -> add 1 (JCode.OpAdd `Long)
                  | "laload" ->add 1 (JCode.OpArrayLoad `Long)
                  | "land" -> add 1 (JCode.OpLAnd)
                  | "lastore" -> add 1 (JCode.OpArrayStore `Long)
                  | "lcmp" -> add 1 (JCode.OpCmp `L)
                  | "lconst_0" -> add 1 (JCode.OpConst(`Long (Int64.of_int 0)))
                  | "lconst_1" -> add 1 (JCode.OpConst(`Long (Int64.of_int 1)))
                  | "ldiv" -> add 1 (JCode.OpDiv `Long)
                  | "lload_0" -> add 1 (JCode.OpLoad (`Long, 0))
                  | "lload_1" -> add 1 (JCode.OpLoad (`Long, 1))
                  | "lload_2" -> add 1 (JCode.OpLoad (`Long, 2))
                  | "lload_3" -> add 1 (JCode.OpLoad (`Long, 3))
                  | "lmul" -> add 1 (JCode.OpMult `Long)
                  | "lneg" -> add 1 (JCode.OpNeg `Long)
                  | "lor" -> add 1 (JCode.OpLOr)
                  | "lrem" -> add 1 (JCode.OpRem `Long)
                  | "lreturn" -> add 1 (JCode.OpReturn `Long)
                  | "lshl" -> add 1 (JCode.OpLShl)
                  | "lshr" -> add 1 (JCode.OpLShr)
                  | "lstore_0" -> add 1 (JCode.OpStore (`Long, 0))
                  | "lstore_1" -> add 1 (JCode.OpStore (`Long, 1))
                  | "lstore_2" -> add 1 (JCode.OpStore (`Long, 2))
                  | "lstore_3" -> add 1 (JCode.OpStore (`Long, 3))
                  | "lsub" -> add 1 (JCode.OpSub `Long)
                  | "lushr" -> add 1 (JCode.OpLUShr)
                  | "lxor" -> add 1 (JCode.OpLXor)
                  (* M *)
                  | "monitorenter" -> add 1 (JCode.OpMonitorEnter)
                  | "monitorexit" -> add 1 (JCode.OpMonitorExit)
                  (* N *)
                  | "nop" -> add 1 (JCode.OpNop)
                  (* P *)
                  | "pop" -> add 1 (JCode.OpPop)
                  | "pop2" -> add 1 (JCode.OpPop2)
                  (* R *)
                  | "return" -> add 1 (JCode.OpReturn `Void)
                  (* S *)
                  | "saload" -> add 1 (JCode.OpArrayLoad `Short)
                  | "sastore" -> add 1 (JCode.OpArrayStore `Short)
                  | "swap" -> add 1 (JCode.OpSwap)
                  | a -> Printf.printf "Inst(%S, %S)\n" a (snd _1); assert false
                )}
| _1 = Insn _2 = Int _3 = Int
    {                               (
                  match(fst _1,snd _1, _2,_3)with
                  | "iinc", "ii", i1, i2 -> add 6 (JCode.OpIInc (i1, i2))
                  | "iinc", "Ii", i1, i2 -> add 6 (JCode.OpIInc (i1, i2))
                  | a,b,i1,i2 ->
                    Printf.printf "InstIntInt(%S, %S, %d, %d)\n" a b i1 i2;
                    assert false
                )}
| _1 = Insn _2 = Int
    {                           (
                  match(fst _1,snd _1, _2)with
                  (* A *)
                  | "aload", _, 0 -> add 1 (JCode.OpLoad (`Object, 0))
                  | "aload", _, 1 -> add 1 (JCode.OpLoad (`Object, 1))
                  | "aload", _, 2 -> add 1 (JCode.OpLoad (`Object, 2))
                  | "aload", _, 3 -> add 1 (JCode.OpLoad (`Object, 3))
                  | "aload", "i", n -> add 2 (JCode.OpLoad(`Object, n))
                  | "aload", "I", n -> add 3 (JCode.OpLoad(`Object, n))
                  | "astore", _, 0 -> add 1 (JCode.OpStore (`Object, 0))
                  | "astore", _, 1 -> add 1 (JCode.OpStore (`Object, 1))
                  | "astore", _, 2 -> add 1 (JCode.OpStore (`Object, 2))
                  | "astore", _, 3 -> add 1 (JCode.OpStore (`Object, 3))
                  | "astore", "i", n -> add 2 (JCode.OpStore(`Object, n))
                  | "astore", "I", n -> add 3 (JCode.OpStore(`Object, n))
                  (* B *)
                  | "bipush", "i", n -> add 2 (JCode.OpConst(`Byte n))
                  (* D *)
                  | "dload", _, 0 -> add 1 (JCode.OpLoad (`Double, 0))
                  | "dload", _, 1 -> add 1 (JCode.OpLoad (`Double, 1))
                  | "dload", _, 2 -> add 1 (JCode.OpLoad (`Double, 2))
                  | "dload", _, 3 -> add 1 (JCode.OpLoad (`Double, 3))
                  | "dload", "i", i -> add 2 (JCode.OpLoad (`Double, i))
                  | "dload", "I", n -> add 3 (JCode.OpLoad (`Double, n))
                  | "dstore", _, 0 -> add 1 (JCode.OpStore (`Double, 0))
                  | "dstore", _, 1 -> add 1 (JCode.OpStore (`Double, 1))
                  | "dstore", _, 2 -> add 1 (JCode.OpStore (`Double, 2))
                  | "dstore", _, 3 -> add 1 (JCode.OpStore (`Double, 3))
                  | "dstore", "i", i -> add 2 (JCode.OpStore (`Double, i))
                  | "dstore", "I", n -> add 3 (JCode.OpStore (`Double, n))
                  (* F *)
                  | "fload", _, 0 -> add 1 (JCode.OpLoad (`Float, 0))
                  | "fload", _, 1 -> add 1 (JCode.OpLoad (`Float, 1))
                  | "fload", _, 2 -> add 1 (JCode.OpLoad (`Float, 2))
                  | "fload", _, 3 -> add 1 (JCode.OpLoad (`Float, 3))
                  | "fload", "i", i -> add 2 (JCode.OpLoad (`Float, i))
                  | "fload", "I", n -> add 3 (JCode.OpLoad (`Float, n))
                  | "fstore", _, 0 -> add 1 (JCode.OpStore (`Float, 0))
                  | "fstore", _, 1 -> add 1 (JCode.OpStore (`Float, 1))
                  | "fstore", _, 2 -> add 1 (JCode.OpStore (`Float, 2))
                  | "fstore", _, 3 -> add 1 (JCode.OpStore (`Float, 3))
                  | "fstore", "i", i -> add 2 (JCode.OpStore (`Float, i))
                  | "fstore", "I", n -> add 3 (JCode.OpStore (`Float, n))
                  (* G *)
                  | "goto", "label", n -> add 3 (JCode.OpGoto n)
                  | "goto_w", "label", n -> add 3 (JCode.OpGoto n)
                  (* I *)
                  | "if_acmpeq", "label", n -> add 3 (JCode.OpIfCmp (`AEq, n))
                  | "if_acmpne", "label", n -> add 3 (JCode.OpIfCmp (`ANe, n))
                  | "if_icmpeq", "label", n -> add 3 (JCode.OpIfCmp (`IEq, n))
                  | "if_icmpge", "label", n -> add 3 (JCode.OpIfCmp (`IGe, n))
                  | "if_icmpgt", "label", n -> add 3 (JCode.OpIfCmp (`IGt, n))
                  | "if_icmple", "label", n -> add 3 (JCode.OpIfCmp (`ILe, n))
                  | "if_icmplt", "label", n -> add 3 (JCode.OpIfCmp (`ILt, n))
                  | "if_icmpne", "label", n -> add 3 (JCode.OpIfCmp (`INe, n))
                  | "ifeq", "label", n -> add 3 (JCode.OpIf (`Eq, n))
                  | "ifge", "label", n -> add 3 (JCode.OpIf (`Ge, n))
                  | "ifgt", "label", n -> add 3 (JCode.OpIf (`Gt, n))
                  | "ifle", "label", n -> add 3 (JCode.OpIf (`Le, n))
                  | "iflt", "label", n -> add 3 (JCode.OpIf (`Lt, n))
                  | "ifne", "label", n -> add 3 (JCode.OpIf (`Ne, n))
                  | "ifnonnull", "label", n -> add 3 (JCode.OpIf (`NonNull, n))
                  | "ifnull", "label", n -> add 3 (JCode.OpIf (`Null, n))
                  | "iload", _, 0 -> add 1 (JCode.OpLoad (`Int2Bool, 0))
                  | "iload", _, 1 -> add 1 (JCode.OpLoad (`Int2Bool, 1))
                  | "iload", _, 2 -> add 1 (JCode.OpLoad (`Int2Bool, 2))
                  | "iload", _, 3 -> add 1 (JCode.OpLoad (`Int2Bool, 3))
                  | "iload", "i", n -> add 2 (JCode.OpLoad (`Int2Bool, n))
                  | "iload", "I", n -> add 3 (JCode.OpLoad (`Int2Bool, n))
                  | "istore", _, 0 -> add 1 (JCode.OpStore (`Int2Bool, 0))
                  | "istore", _, 1 -> add 1 (JCode.OpStore (`Int2Bool, 1))
                  | "istore", _, 2 -> add 1 (JCode.OpStore (`Int2Bool, 2))
                  | "istore", _, 3 -> add 1 (JCode.OpStore (`Int2Bool, 3))
                  | "istore", "i", i -> add 2 (JCode.OpStore (`Int2Bool, i))
                  | "istore", "I", i -> add 3 (JCode.OpStore (`Int2Bool, i))
                  (* I *)
                  | "ldc", "constant", n -> add 2 (JCode.OpConst(`Int (Int32.of_int n)))
                  | "ldc2_w", "bigconstant", d -> add 3 (JCode.OpConst(`Long (Int64.of_int d)))
                  | "lload", _, 0 -> add 1 (JCode.OpLoad (`Long, 0))
                  | "lload", _, 1 -> add 1 (JCode.OpLoad (`Long, 1))
                  | "lload", _, 2 -> add 1 (JCode.OpLoad (`Long, 2))
                  | "lload", _, 3 -> add 1 (JCode.OpLoad (`Long, 3))
                  | "lload", "i", i -> add 2 (JCode.OpLoad (`Long, i)) 
                  | "lload", "I", n -> add 3 (JCode.OpLoad (`Long, n))
                  | "lstore", _, 0 -> add 1 (JCode.OpStore (`Long, 0))
                  | "lstore", _, 1 -> add 1 (JCode.OpStore (`Long, 1))
                  | "lstore", _, 2 -> add 1 (JCode.OpStore (`Long, 2))
                  | "lstore", _, 3 -> add 1 (JCode.OpStore (`Long, 3))
                  | "lstore", "i", l -> add 2 (JCode.OpStore (`Long, l))
                  | "lstore", "I", l -> add 3 (JCode.OpStore (`Long, l))
                  (* R *)
                  | "ret", "i", n -> add 2 (JCode.OpRet n)
                  | "ret", "I", n -> add 3 (JCode.OpRet n)
                  (* S *)
                  | "sipush", "i", n -> add 3 (JCode.OpConst(`Short n))
                  | a,b,i1 ->
                    Printf.printf "InstInt(%S, %S, %d)\n" a b i1;
                    assert false
                )}
| _1 = Insn _2 = Num
    {                           (
                  match(fst _1,snd _1, _2)with
                  (* I *)
                  | "ldc", "constant", s -> add 2 (JCode.OpConst(`Float (float_of_string s)))
                  | "ldc2_w", "bigconstant", d -> add 3 (JCode.OpConst(`Double (float_of_string d)))
                  | a,b,s ->
                    Printf.printf "InstNum(%S, %S, %S)\n" a b s;
                    assert false
                )}
| _1 = Insn _2 = Word
    {                            ( 
                  match(fst _1,snd _1, _2)with
                  (* A *)
                  | "anewarray", "class", o -> add 3 (JCode.OpNewArray (JBasics.TObject (JParseSignature.parse_objectType o)))
                  (* C *)
                  | "checkcast", "class", w ->
                    add 3 (JCode.OpCheckCast (JParseSignature.parse_objectType w))
                  (* G *)
                  | "goto", "label", l -> add 3 (JCode.OpGoto(label2int l))
                  | "goto_w", "label", l -> add 3 (JCode.OpGoto(label2int l))
                  (* I *)
                  | "if_acmpeq", "label", l -> add 3 (JCode.OpIfCmp (`AEq, label2int l))
                  | "if_acmpne", "label", l -> add 3 (JCode.OpIfCmp (`ANe, label2int l))
                  | "if_icmpeq", "label", l -> add 3 (JCode.OpIfCmp (`IEq, label2int l))
                  | "if_icmpge", "label", l -> add 3 (JCode.OpIfCmp (`IGe, label2int l))
                  | "if_icmpgt", "label", l -> add 3 (JCode.OpIfCmp (`IGt, label2int l))
                  | "if_icmple", "label", l -> add 3 (JCode.OpIfCmp (`ILe, label2int l))
                  | "if_icmplt", "label", l -> add 3 (JCode.OpIfCmp (`ILt, label2int l))
                  | "if_icmpne", "label", l -> add 3 (JCode.OpIfCmp (`INe, label2int l))
                  | "ifeq", "label", l -> add 3 (JCode.OpIf (`Eq, label2int l))
                  | "ifge", "label", l -> add 3 (JCode.OpIf (`Ge, label2int l))
                  | "ifgt", "label", l -> add 3 (JCode.OpIf (`Gt, label2int l))
                  | "ifle", "label", l -> add 3 (JCode.OpIf (`Le, label2int l))
                  | "iflt", "label", l -> add 3 (JCode.OpIf (`Lt, label2int l))
                  | "ifne", "label", l -> add 3 (JCode.OpIf (`Ne, label2int l))
                  | "instanceof", "class", o -> add 3 (JCode.OpInstanceOf (JParseSignature.parse_objectType o))
                  | "invokenonvirtual", "method", m ->
                    let (obj,f) = split_method m in
                    let (name,o) = split_obj obj in
                    let (args,r) = JParseSignature.parse_method_descriptor f in
                    add 3(JCode.OpInvoke (`Special (JBasics.make_cn name), JBasics.make_ms o args r))
                  | "invokestatic", "method", m ->
                    let (obj,f) = split_method m in
                    let (name,o) = split_obj obj in
                    let (args,r) = JParseSignature.parse_method_descriptor f in
                    add 3(JCode.OpInvoke (`Static (JBasics.make_cn name), JBasics.make_ms o args r))
                  | "invokevirtual", "method", m ->
                    let (obj,f) = split_method m in
                    let (name,o) = split_obj obj in
                    let (args,r) = JParseSignature.parse_method_descriptor f in
                    add 3(JCode.OpInvoke (`Virtual ((JBasics.TClass (JBasics.make_cn name))), JBasics.make_ms o args r))
                  (* J *)
                  | "jsr","label",label -> add 3 (JCode.OpJsr((label2int label)))
                  | "jsr_w","label",label -> add 3 (JCode.OpJsr((label2int label)))
                  | "ldc", "constant", d -> add 2 (JCode.OpConst(`Float (float_of_string d)))
                  | "ldc2_w", "bigconstant", d -> add 3 (JCode.OpConst(`Double (float_of_string d)))
                  (* N *)
                  | "new", "class", o -> add 3 (JCode.OpNew (JBasics.make_cn (replace_dot o)))
                  | "newarray", "atype", t ->
                    let a = JParseSignature.parse_field_descriptor (cbasic t) in
                    add 2 (JCode.OpNewArray a)
                  | a,b,s ->
                    Printf.printf "InstWord(%S, %S, %S)\n" a b s;
                    assert false
                )}
| _1 = Insn _2 = Word _3 = Int
    {                                (
                  match(fst _1,snd _1, _2,_3)with
                  (* I *)
                  | "invokeinterface", "interface", m, i ->
                    let (obj,f) = split_method m in
                    let (name,o) = split_obj obj in
                    let (args,r) = JParseSignature.parse_method_descriptor f in
                    add 5(JCode.OpInvoke (`Interface (JBasics.make_cn name), JBasics.make_ms o args r))
                  (* M *)
                  | "multianewarray", "marray", t, i ->
                    add 4 (JCode.OpAMultiNewArray ((JParseSignature.parse_objectType t), i))
                  | a,b,s,i ->
                    Printf.printf "InstWordInt(%S, %S, %S, %d)\n" a b s i;
                    assert false
                )}
| _1 = Insn _2 = Word _3 = Word
    {                                 (
                  match(fst _1, _2,_3)with
                  | "getfield", cf, fd ->
                    let (c,f) = split_obj(cf) in
                    let fd = JParseSignature.parse_field_descriptor fd in
                    add 3 (JCode.OpGetField (JBasics.make_cn c, JBasics.make_fs f fd))
                  | "getstatic", cf, fd ->
                    let (c,f) = split_obj(cf) in
                    let fd = JParseSignature.parse_field_descriptor fd in
                    add 3 (JCode.OpGetStatic (JBasics.make_cn c, JBasics.make_fs f fd))
                  | "putfield", cf, fd ->
                    let (c,f) = split_obj(cf) in
                    let fd = JParseSignature.parse_field_descriptor fd in
                    add 3 (JCode.OpPutField (JBasics.make_cn c, JBasics.make_fs f fd))
                  | "putstatic", cf, fd ->
                    let (c,f) = split_obj(cf) in
                    let fd = JParseSignature.parse_field_descriptor fd in
                    add 3 (JCode.OpPutStatic (JBasics.make_cn c, JBasics.make_fs f fd))
                  | a, b, s2 ->
                    Printf.printf "InstWordWord(%S, %S, %S, %S)\n" a b (snd _1) s2;
                    assert false
                )}
| _1 = Insn _2 = Str
    {                           ( 
                  match(fst _1,snd _1, unescape _2)with
                  | "ldc", "constant", s -> add 2 (JCode.OpConst(`String (JBasics.make_jstr s)))
                  | a,b,s ->
                    Printf.printf "InstStr(%S, %S, %S)\n" a b s;
                    assert false
                )}
| _1 = Insn _2 = Relative
    {                                (
                  match(fst _1, snd _1,_2)with
                  | a,b,s ->
                    Printf.printf "InstRelative(%S, %S, %S)\n" a b s;
                    assert false
                )}

complex_instruction:
| _1 = LOOKUPSWITCH _2 = lookup
    {                                      ( _2 )}
| _1 = TABLESWITCH _2 = table
    {                                    ( _2 )}

lookup:
| _1 = lookup_args _2 = lookup_list _3 = lookup_default
    {                    (
                      let a =
                        match _3 with
                        | DefaultInt i -> i
                        | DefaultWord l -> label2int l
                      in
                      let ls = List.map(function
                        | CaseIntInt(i, j)-> (Int32.of_int i, j)
                        | CaseIntWord(i, j) -> (Int32.of_int i, (label2int j))
                      ) _2 in
                      let padding_size = (4 - (!pos + 1) mod 4) mod 4 in
                      let n = 9 + padding_size + 8 * (List.length _2) in
                      add n (JCode.OpLookupSwitch (a, ls))

                    )}

lookup_args:
| _1 = SEP
    {                          ( () )}

lookup_list:
| _1 = lookup_entry _2 = lookup_list
    {                                               ( _1 :: _2 )}
| _1 = lookup_entry
    {                                   ( [_1] )}

lookup_entry:
| _1 = Int _2 = COLON _3 = Word _4 = SEP
    {                                           ( CaseIntWord(_1,_3) )}
| _1 = Int _2 = COLON _3 = Int _4 = SEP
    {                                          ( CaseIntInt(_1,_3) )}

lookup_default:
| _1 = DEFAULT _2 = COLON _3 = Word
    {                                         ( DefaultWord _3 )}
| _1 = DEFAULT _2 = COLON _3 = Int
    {                                        ( DefaultInt _3 )}

table:
| _1 = table_args _2 = table_list _3 = table_default
    {                    (
                      (* T *)
                      let (low,high,defs,def) = (fst _1,snd _1,_2,_3) in
                      let default2int = function
                        | DefaultInt i -> i
                        | DefaultWord l -> label2int l
                      in
                      let defs = List.map default2int defs in
                      let defs = Array.of_list defs in
                      let padding_size = (4 - ((!pos + 1) mod 4)) mod 4 in
                      let n = 13 + padding_size + 4 * (Array.length defs) in
                      let high = if high = -1 then Array.length defs - 1 + low else high in
                      add n (JCode.OpTableSwitch ((default2int def), Int32.of_int low, Int32.of_int high, defs))
                    )}

table_args:
| _1 = Int _2 = SEP
    {                              ( (_1,-1) )}
| _1 = Int _2 = Int _3 = SEP
    {                                  ( (_1, _2) )}

table_list:
| _1 = table_entry _2 = table_list
    {                                             ( _1::_2 )}
| _1 = table_entry
    {                                  ( [_1] )}

table_entry:
| _1 = Word _2 = SEP
    {                                 ( DefaultWord(_1) )}
| _1 = Int _2 = SEP
    {                                ( DefaultInt(_1) )}

table_default:
| _1 = DEFAULT _2 = COLON _3 = Word
    {                                         ( DefaultWord(_3) )}
| _1 = DEFAULT _2 = COLON _3 = Int
    {                                        ( DefaultInt(_3) )}

%%