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;
27 public String mul32() {
29 return "__mul32(@1,@2)";
31 public String div32() {
33 return "__div32(@1,@2)";
36 public String mod32() {
38 return "__mod32(@1,@2)";
41 public String add64() {
42 return "(@1).add64(@2)";
45 public String sub64() {
46 return "(@1).sub64(@2)";
49 public String mul64() {
50 return "(@1).mul64(@2)";
53 public String div64() {
54 return "(@1).div64(@2)";
57 public String mod64() {
58 return "(@1).mod64(@2)";
61 public String and64() {
62 return "(@1).and64(@2)";
65 public String or64() {
66 return "(@1).or64(@2)";
69 public String xor64() {
70 return "(@1).xor64(@2)";
73 public String neg64() {
74 return "(@1).neg64()";
77 public String shl64() {
78 return "(@1).shl64(@2)";
81 public String shr64() {
82 return "(@1).shr64(@2)";
85 public String ushr64() {
86 return "(@1).ushr64(@2)";
89 public String compare64() {
90 return "(@2).compare64(@1)";
93 public String generate() {
97 StringBuilder sb = new StringBuilder();
98 if ((used & MUL32) != 0) {
100 " __mul32 = function(x, y) {\n" +
101 " return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;\n" +
106 if ((used & (MOD32 | DIV32)) != 0) {
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" +
113 " throw exception;\n" +
118 if ((used & MOD32) != 0) {
120 " function __mod32(x, y) {\n" +
121 " if (y === 0) __handleDivByZero();\n" +
122 " return (x % y) | 0;\n" +
127 if ((used & DIV32) != 0) {
129 " function __div32(x, y) {\n" +
130 " if (y === 0) __handleDivByZero();\n" +
131 " return (x / y) | 0;\n" +
136 return sb.toString();