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;
28 public String mul32() {
30 return "__mul32(@1,@2)";
32 public String div32() {
34 return "__div32(@1,@2)";
37 public String mod32() {
39 return "__mod32(@1,@2)";
42 public String add64() {
44 return "__add64(@1,@2)";
47 public String sub64() {
49 return "__sub64(@1,@2)";
52 public String mul64() {
54 return "__mul64(@1,@2)";
57 public String div64() {
59 return "__div64(@1,@2)";
62 public String mod64() {
64 return "__mod64(@1,@2)";
67 public String and64() {
69 return "__and64(@1,@2)";
72 public String or64() {
74 return "__or64(@1,@2)";
77 public String xor64() {
79 return "__xor64(@1,@2)";
82 public String neg64() {
87 public String shl64() {
89 return "__shl64(@1,@2)";
92 public String shr64() {
94 return "__shr64(@1,@2)";
97 public String ushr64() {
99 return "__ushr64(@1,@2)";
102 public String compare64() {
104 return "__compare64(@1,@2)";
107 public String generate() {
111 StringBuilder sb = new StringBuilder();
112 if ((used & MUL32) != 0) {
114 " __mul32 = function(x, y) {\n" +
115 " return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;\n" +
120 if ((used & (MOD32 | DIV32)) != 0) {
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" +
127 " throw exception;\n" +
132 if ((used & MOD32) != 0) {
134 " function __mod32(x, y) {\n" +
135 " if (y === 0) __handleDivByZero();\n" +
136 " return (x % y) | 0;\n" +
141 if ((used & DIV32) != 0) {
143 " function __div32(x, y) {\n" +
144 " if (y === 0) __handleDivByZero();\n" +
145 " return (x / y) | 0;\n" +
150 if ((used & BIT64) != 0) {
152 " var __add64 = function(x,y) { return x.add64(y); };\n" +
153 " var __sub64 = function(x,y) { return x.sub64(y); };\n" +
154 " var __mul64 = function(x,y) { return x.mul64(y); };\n" +
155 " var __div64 = function(x,y) { return x.div64(y); };\n" +
156 " var __mod64 = function(x,y) { return x.mod64(y); };\n" +
157 " var __and64 = function(x,y) { return x.and64(y); };\n" +
158 " var __or64 = function(x,y) { return x.or64(y); };\n" +
159 " var __xor64 = function(x,y) { return x.xor64(y); };\n" +
160 " var __neg64 = function(x) { return x.neg64(); };\n" +
161 " var __shl64 = function(x,y) { return x.shl64(y); };\n" +
162 " var __shr64 = function(x,y) { return x.shr64(y); };\n" +
163 " var __ushr64 = function(x,y) { return x.ushr64(y); };\n" +
164 " var __compare64 = function(x,y) { return y.compare64(x); };\n" +
168 return sb.toString();