rt/vm/src/main/java/org/apidesign/vm4brwsr/NumberOperations.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Jan 2016 05:54:37 +0100
changeset 1862 fa00fb053c72
parent 1858 4dea14fafc31
child 1865 412cc2201b1a
permissions -rw-r--r--
Extracting direct references to 64-bit operations
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 final class NumberOperations {
    21     private static final int DIV32 = 1;
    22     private static final int MOD32 = 2;
    23     private static final int MUL32 = 4;
    24 
    25     private int used;
    26 
    27     public String mul32() {
    28         used |= MUL32;
    29         return "__mul32(@1,@2)";
    30     }
    31     public String div32() {
    32         used |= DIV32;
    33         return "__div32(@1,@2)";
    34     }
    35 
    36     public String mod32() {
    37         used |= MOD32;
    38         return "__mod32(@1,@2)";
    39     }
    40 
    41     public String add64() {
    42         return "(@1).add64(@2)";
    43     }
    44 
    45     public String sub64() {
    46         return "(@1).sub64(@2)";
    47     }
    48 
    49     public String mul64() {
    50         return "(@1).mul64(@2)";
    51     }
    52 
    53     public String div64() {
    54         return "(@1).div64(@2)";
    55     }
    56 
    57     public String mod64() {
    58         return "(@1).mod64(@2)";
    59     }
    60 
    61     public String and64() {
    62         return "(@1).and64(@2)";
    63     }
    64 
    65     public String or64() {
    66         return "(@1).or64(@2)";
    67     }
    68 
    69     public String xor64() {
    70         return "(@1).xor64(@2)";
    71     }
    72 
    73     public String neg64() {
    74         return "(@1).neg64()";
    75     }
    76 
    77     public String shl64() {
    78         return "(@1).shl64(@2)";
    79     }
    80 
    81     public String shr64() {
    82         return "(@1).shr64(@2)";
    83     }
    84 
    85     public String ushr64() {
    86         return "(@1).ushr64(@2)";
    87     }
    88 
    89     public String compare64() {
    90         return "(@2).compare64(@1)";
    91     }
    92 
    93     public String generate() {
    94         if (used == 0) {
    95             return "";
    96         }
    97         StringBuilder sb = new StringBuilder();
    98         if ((used & MUL32) != 0) {
    99             sb.append(
   100                 "    __mul32 = function(x, y) {\n" +
   101                 "        return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;\n" +
   102                 "    };\n" +
   103                 ""
   104             );
   105         }
   106         if ((used & (MOD32 | DIV32)) != 0) {
   107             sb.append(
   108                 "    function __handleDivByZero() {\n" +
   109                 "        var exception = new vm.java_lang_ArithmeticException;\n" +
   110                 "        vm.java_lang_ArithmeticException(false).constructor\n" +
   111                 "          .cons__VLjava_lang_String_2.call(exception, \"/ by zero\");\n" +
   112                 "\n" +
   113                 "        throw exception;\n" +
   114                 "    }\n" +
   115                 ""
   116             );
   117         }
   118         if ((used & MOD32) != 0) {
   119             sb.append(
   120                 "    function __mod32(x, y) {\n" +
   121                 "        if (y === 0) __handleDivByZero();\n" +
   122                 "        return (x % y) | 0;\n" +
   123                 "    }\n" +
   124                 ""
   125             );
   126         }
   127         if ((used & DIV32) != 0) {
   128             sb.append(
   129                 "    function __div32(x, y) {\n" +
   130                 "        if (y === 0) __handleDivByZero();\n" +
   131                 "        return (x / y) | 0;\n" +
   132                 "    }\n" +
   133                 ""
   134             );
   135         }
   136         return sb.toString();
   137     }
   138 }