2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
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;
32 public String mul32() {
34 return "__mul32(@1,@2)";
36 public String div32() {
38 return "__div32(@1,@2)";
41 public String mod32() {
43 return "__mod32(@1,@2)";
46 public String add64() {
48 return "__add64(@1,@2)";
51 public String sub64() {
53 return "__sub64(@1,@2)";
56 public String mul64() {
58 return "__mul64(@1,@2)";
61 public String div64() {
63 return "__div64(@1,@2)";
66 public String mod64() {
68 return "__mod64(@1,@2)";
71 public String and64() {
73 return "__and64(@1,@2)";
76 public String or64() {
78 return "__or64(@1,@2)";
81 public String xor64() {
83 return "__xor64(@1,@2)";
86 public String neg64() {
91 public String shl64() {
93 return "__shl64(@1,@2)";
96 public String shr64() {
98 return "__shr64(@1,@2)";
101 public String ushr64() {
103 return "__ushr64(@1,@2)";
106 public String compare64() {
108 return "__compare64(@2,@1)";
111 public String generate() {
115 StringBuilder sb = new StringBuilder();
116 if ((used & MUL32) != 0) {
118 " __mul32 = function(x, y) {\n" +
119 " return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;\n" +
124 if ((used & (MOD32 | DIV32)) != 0) {
126 " function __handleDivByZero() {\n" +
127 " var exception = new vm.java_lang_ArithmeticException;\n" +
128 " vm.java_lang_ArithmeticException(false).constructor\n" +
129 " .cons__VLjava_lang_String_2.call(exception, \"/ by zero\");\n" +
131 " throw exception;\n" +
136 if ((used & MOD32) != 0) {
138 " function __mod32(x, y) {\n" +
139 " if (y === 0) __handleDivByZero();\n" +
140 " return (x % y) | 0;\n" +
145 if ((used & DIV32) != 0) {
147 " function __div32(x, y) {\n" +
148 " if (y === 0) __handleDivByZero();\n" +
149 " return (x / y) | 0;\n" +
154 if ((used & BIT64) != 0) {
156 " var __add64 = Number.prototype['__bit64']['add64'];\n" +
157 " var __sub64 = Number.prototype['__bit64']['sub64'];\n" +
158 " var __mul64 = Number.prototype['__bit64']['mul64'];\n" +
159 " var __div64 = Number.prototype['__bit64']['div64'];\n" +
160 " var __mod64 = Number.prototype['__bit64']['mod64'];\n" +
161 " var __and64 = Number.prototype['__bit64']['and64'];\n" +
162 " var __or64 = Number.prototype['__bit64']['or64'];\n" +
163 " var __xor64 = Number.prototype['__bit64']['xor64'];\n" +
164 " var __neg64 = Number.prototype['__bit64']['neg64'];\n" +
165 " var __shl64 = Number.prototype['__bit64']['shl64'];\n" +
166 " var __shr64 = Number.prototype['__bit64']['shr64'];\n" +
167 " var __ushr64 = Number.prototype['__bit64']['ushr64'];\n" +
168 " var __compare64 = Number.prototype['__bit64']['compare64'];\n" +
172 return sb.toString();