rt/vm/src/main/java/org/apidesign/vm4brwsr/NumberOperations.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 27 Jan 2016 21:24:22 +0100
changeset 1871 bcbc4c03d35d
parent 1870 790cc8784820
child 1906 48053538940a
permissions -rw-r--r--
Direct access to compare64 as well
     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     private static final int BIT64 = 8;
    25 
    26     private int used;
    27 
    28     public String mul32() {
    29         used |= MUL32;
    30         return "__mul32(@1,@2)";
    31     }
    32     public String div32() {
    33         used |= DIV32;
    34         return "__div32(@1,@2)";
    35     }
    36 
    37     public String mod32() {
    38         used |= MOD32;
    39         return "__mod32(@1,@2)";
    40     }
    41 
    42     public String add64() {
    43         used |= BIT64;
    44         return "__add64(@1,@2)";
    45     }
    46 
    47     public String sub64() {
    48         used |= BIT64;
    49         return "__sub64(@1,@2)";
    50     }
    51 
    52     public String mul64() {
    53         used |= BIT64;
    54         return "__mul64(@1,@2)";
    55     }
    56 
    57     public String div64() {
    58         used |= BIT64;
    59         return "__div64(@1,@2)";
    60     }
    61 
    62     public String mod64() {
    63         used |= BIT64;
    64         return "__mod64(@1,@2)";
    65     }
    66 
    67     public String and64() {
    68         used |= BIT64;
    69         return "__and64(@1,@2)";
    70     }
    71 
    72     public String or64() {
    73         used |= BIT64;
    74         return "__or64(@1,@2)";
    75     }
    76 
    77     public String xor64() {
    78         used |= BIT64;
    79         return "__xor64(@1,@2)";
    80     }
    81 
    82     public String neg64() {
    83         used |= BIT64;
    84         return "__neg64(@1)";
    85     }
    86 
    87     public String shl64() {
    88         used |= BIT64;
    89         return "__shl64(@1,@2)";
    90     }
    91 
    92     public String shr64() {
    93         used |= BIT64;
    94         return "__shr64(@1,@2)";
    95     }
    96 
    97     public String ushr64() {
    98         used |= BIT64;
    99         return "__ushr64(@1,@2)";
   100     }
   101 
   102     public String compare64() {
   103         used |= BIT64;
   104         return "__compare64(@2,@1)";
   105     }
   106 
   107     public String generate() {
   108         if (used == 0) {
   109             return "";
   110         }
   111         StringBuilder sb = new StringBuilder();
   112         if ((used & MUL32) != 0) {
   113             sb.append(
   114                 "    __mul32 = function(x, y) {\n" +
   115                 "        return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;\n" +
   116                 "    };\n" +
   117                 ""
   118             );
   119         }
   120         if ((used & (MOD32 | DIV32)) != 0) {
   121             sb.append(
   122                 "    function __handleDivByZero() {\n" +
   123                 "        var exception = new vm.java_lang_ArithmeticException;\n" +
   124                 "        vm.java_lang_ArithmeticException(false).constructor\n" +
   125                 "          .cons__VLjava_lang_String_2.call(exception, \"/ by zero\");\n" +
   126                 "\n" +
   127                 "        throw exception;\n" +
   128                 "    }\n" +
   129                 ""
   130             );
   131         }
   132         if ((used & MOD32) != 0) {
   133             sb.append(
   134                 "    function __mod32(x, y) {\n" +
   135                 "        if (y === 0) __handleDivByZero();\n" +
   136                 "        return (x % y) | 0;\n" +
   137                 "    }\n" +
   138                 ""
   139             );
   140         }
   141         if ((used & DIV32) != 0) {
   142             sb.append(
   143                 "    function __div32(x, y) {\n" +
   144                 "        if (y === 0) __handleDivByZero();\n" +
   145                 "        return (x / y) | 0;\n" +
   146                 "    }\n" +
   147                 ""
   148             );
   149         }
   150         if ((used & BIT64) != 0) {
   151             sb.append(
   152                 "    var __add64 = Number.prototype['__bit64']['add64'];\n" +
   153                 "    var __sub64 = Number.prototype['__bit64']['sub64'];\n" +
   154                 "    var __mul64 = Number.prototype['__bit64']['mul64'];\n" +
   155                 "    var __div64 = Number.prototype['__bit64']['div64'];\n" +
   156                 "    var __mod64 = Number.prototype['__bit64']['mod64'];\n" +
   157                 "    var __and64 = Number.prototype['__bit64']['and64'];\n" +
   158                 "    var __or64 = Number.prototype['__bit64']['or64'];\n" +
   159                 "    var __xor64 = Number.prototype['__bit64']['xor64'];\n" +
   160                 "    var __neg64 = Number.prototype['__bit64']['neg64'];\n" +
   161                 "    var __shl64 = Number.prototype['__bit64']['shl64'];\n" +
   162                 "    var __shr64 = Number.prototype['__bit64']['shr64'];\n" +
   163                 "    var __ushr64 = Number.prototype['__bit64']['ushr64'];\n" +
   164                 "    var __compare64 = Number.prototype['__bit64']['compare64'];\n" +
   165                 ""
   166             );
   167         }
   168         return sb.toString();
   169     }
   170 }