Refactoring

This commit is contained in:
Alexey Andreev 2016-07-28 11:23:41 +03:00
parent 4961e3d92d
commit 888710102f
30 changed files with 124 additions and 248 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,16 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.cache;
import org.teavm.ast.AsyncMethodNode;
import org.teavm.ast.RegularMethodNode;
import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class EmptyRegularMethodNodeCache implements MethodNodeCache {
@Override
public RegularMethodNode get(MethodReference methodReference) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.cache;
import java.util.HashMap;
import java.util.Map;
@ -21,10 +21,6 @@ import org.teavm.ast.AsyncMethodNode;
import org.teavm.ast.RegularMethodNode;
import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class InMemoryRegularMethodNodeCache implements MethodNodeCache {
private Map<MethodReference, RegularMethodNode> cache = new HashMap<>();
private Map<MethodReference, AsyncMethodNode> asyncCache = new HashMap<>();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,16 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.cache;
import org.teavm.ast.AsyncMethodNode;
import org.teavm.ast.RegularMethodNode;
import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public interface MethodNodeCache {
RegularMethodNode get(MethodReference methodReference);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.decompilation;
import java.util.HashSet;
import java.util.List;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,12 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.decompilation;
/**
*
* @author Alexey Andreev
*/
public class DecompilationException extends RuntimeException {
private static final long serialVersionUID = -1400142974526572669L;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2011 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.decompilation;
import java.util.ArrayDeque;
import java.util.ArrayList;
@ -44,6 +44,8 @@ import org.teavm.ast.SequentialStatement;
import org.teavm.ast.Statement;
import org.teavm.ast.TryCatchStatement;
import org.teavm.ast.WhileStatement;
import org.teavm.ast.cache.MethodNodeCache;
import org.teavm.ast.optimization.Optimizer;
import org.teavm.cache.NoCache;
import org.teavm.common.Graph;
import org.teavm.common.GraphIndexer;
@ -70,10 +72,6 @@ import org.teavm.model.util.AsyncProgramSplitter;
import org.teavm.model.util.ListingBuilder;
import org.teavm.model.util.ProgramUtils;
/**
*
* @author Alexey Andreev
*/
public class Decompiler {
private ClassHolderSource classSource;
private ClassLoader classLoader;
@ -88,7 +86,7 @@ public class Decompiler {
private RangeTree.Node currentNode;
private RangeTree.Node parentNode;
private Map<MethodReference, Generator> generators = new HashMap<>();
private Set<MethodReference> methodsToPass = new HashSet<>();
private Set<MethodReference> methodsToSkip = new HashSet<>();
private MethodNodeCache regularMethodCache;
private Set<MethodReference> asyncMethods;
private Set<MethodReference> splitMethods = new HashSet<>();
@ -169,8 +167,8 @@ public class Decompiler {
generators.put(method, generator);
}
public void addMethodToPass(MethodReference method) {
methodsToPass.add(method);
public void addMethodToSkip(MethodReference method) {
methodsToSkip.add(method);
}
private void orderClasses(String className, Set<String> visited, List<String> order) {
@ -203,7 +201,7 @@ public class Decompiler {
continue;
}
if (method.getAnnotations().get(InjectedBy.class.getName()) != null
|| methodsToPass.contains(method.getReference())) {
|| methodsToSkip.contains(method.getReference())) {
continue;
}
MethodNode methodNode = decompile(method);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.decompilation;
import java.util.ArrayList;
import java.util.Arrays;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.HashMap;
import java.util.List;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.List;
import org.teavm.ast.AssignmentStatement;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.HashMap;
import java.util.HashSet;
@ -40,10 +40,6 @@ import org.teavm.ast.ThrowStatement;
import org.teavm.ast.TryCatchStatement;
import org.teavm.ast.WhileStatement;
/**
*
* @author Alexey Andreev
*/
class BreakEliminator implements StatementVisitor {
private Map<BlockStatement, List<Statement>> blockSuccessors = new HashMap<>();
private Set<IdentifiedStatement> outerStatements = new HashSet<>();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.List;
import org.teavm.ast.AssignmentStatement;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.List;
import org.teavm.ast.AssignmentStatement;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import org.teavm.ast.BinaryExpr;
import org.teavm.ast.BinaryOperation;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.BitSet;
import java.util.List;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.ArrayList;
import java.util.Arrays;
@ -58,6 +58,7 @@ import org.teavm.ast.UnaryOperation;
import org.teavm.ast.UnwrapArrayExpr;
import org.teavm.ast.VariableExpr;
import org.teavm.ast.WhileStatement;
import org.teavm.javascript.ExpressionSideEffectDecomposer;
class OptimizingVisitor implements StatementVisitor, ExprVisitor {
private Expr resultExpr;
@ -116,7 +117,7 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
case GREATER:
case GREATER_OR_EQUALS: {
BinaryExpr comparison = (BinaryExpr) p;
Expr result = BinaryExpr.binary(expr.getOperation(), expr.getType(),
Expr result = BinaryExpr.binary(expr.getOperation(), comparison.getType(),
comparison.getFirstOperand(), comparison.getSecondOperand());
result.setLocation(comparison.getLocation());
if (invert) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.Arrays;
import org.teavm.common.Graph;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2014 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.HashSet;
import java.util.List;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012 Alexey Andreev.
* Copyright 2016 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.ast.optimization;
import java.util.Arrays;
import java.util.List;

View File

@ -69,7 +69,7 @@ import org.teavm.ast.UnaryExpr;
import org.teavm.ast.UnwrapArrayExpr;
import org.teavm.ast.VariableExpr;
import org.teavm.ast.WhileStatement;
import org.teavm.javascript.MethodNodeCache;
import org.teavm.ast.cache.MethodNodeCache;
import org.teavm.model.MethodReference;
import org.teavm.parsing.ClassDateProvider;

View File

@ -15,10 +15,6 @@
*/
package org.teavm.javascript;
/**
*
* @author Alexey Andreev
*/
public enum AsyncInvocationType {
COMPLETE,
ERROR

View File

@ -21,10 +21,6 @@ import org.teavm.model.instructions.InvocationType;
import org.teavm.model.instructions.InvokeInstruction;
import org.teavm.model.instructions.NullCheckInstruction;
/**
*
* @author Alexey Andreev
*/
public class NullPointerExceptionTransformer implements ClassHolderTransformer {
@Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource, Diagnostics diagnostics) {

View File

@ -15,10 +15,6 @@
*/
package org.teavm.javascript;
/**
*
* @author Alexey Andreev
*/
public enum Precedence {
COMMA,
ASSIGNMENT,

View File

@ -1,144 +0,0 @@
/*
* Copyright 2012 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
import org.teavm.ast.AssignmentStatement;
import org.teavm.ast.BlockStatement;
import org.teavm.ast.BreakStatement;
import org.teavm.ast.ConditionalStatement;
import org.teavm.ast.ContinueStatement;
import org.teavm.ast.GotoPartStatement;
import org.teavm.ast.IdentifiedStatement;
import org.teavm.ast.InitClassStatement;
import org.teavm.ast.MonitorEnterStatement;
import org.teavm.ast.MonitorExitStatement;
import org.teavm.ast.ReturnStatement;
import org.teavm.ast.SequentialStatement;
import org.teavm.ast.Statement;
import org.teavm.ast.StatementVisitor;
import org.teavm.ast.SwitchClause;
import org.teavm.ast.SwitchStatement;
import org.teavm.ast.ThrowStatement;
import org.teavm.ast.TryCatchStatement;
import org.teavm.ast.WhileStatement;
/**
*
* @author Alexey Andreev
*/
class ReferenceCountingVisitor implements StatementVisitor {
private IdentifiedStatement target;
public int count;
public ReferenceCountingVisitor(IdentifiedStatement target) {
this.target = target;
}
@Override
public void visit(AssignmentStatement statement) {
}
@Override
public void visit(SequentialStatement statement) {
for (Statement part : statement.getSequence()) {
part.acceptVisitor(this);
}
}
@Override
public void visit(ConditionalStatement statement) {
for (Statement part : statement.getConsequent()) {
part.acceptVisitor(this);
}
for (Statement part : statement.getAlternative()) {
part.acceptVisitor(this);
}
}
@Override
public void visit(SwitchStatement statement) {
for (SwitchClause clause : statement.getClauses()) {
for (Statement part : clause.getBody()) {
part.acceptVisitor(this);
}
}
for (Statement part : statement.getDefaultClause()) {
part.acceptVisitor(this);
}
}
@Override
public void visit(WhileStatement statement) {
for (Statement part : statement.getBody()) {
part.acceptVisitor(this);
}
}
@Override
public void visit(BlockStatement statement) {
for (Statement part : statement.getBody()) {
part.acceptVisitor(this);
}
}
@Override
public void visit(BreakStatement statement) {
if (statement.getTarget() == target) {
++count;
}
}
@Override
public void visit(ContinueStatement statement) {
if (statement.getTarget() == target) {
++count;
}
}
@Override
public void visit(ReturnStatement statement) {
}
@Override
public void visit(ThrowStatement statement) {
}
@Override
public void visit(InitClassStatement statement) {
}
@Override
public void visit(TryCatchStatement statement) {
for (Statement part : statement.getProtectedBody()) {
part.acceptVisitor(this);
}
for (Statement part : statement.getHandler()) {
part.acceptVisitor(this);
}
}
@Override
public void visit(GotoPartStatement statement) {
}
@Override
public void visit(MonitorEnterStatement statement) {
}
@Override
public void visit(MonitorExitStatement statement) {
}
}

View File

@ -1477,6 +1477,24 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
case COMPARE:
visitBinaryFunction(expr, "Long_compare");
break;
case EQUALS:
visitBinaryFunction(expr, "Long_eq");
break;
case NOT_EQUALS:
visitBinaryFunction(expr, "Long_ne");
break;
case LESS:
visitBinaryFunction(expr, "Long_lt");
break;
case LESS_OR_EQUALS:
visitBinaryFunction(expr, "Long_le");
break;
case GREATER:
visitBinaryFunction(expr, "Long_gt");
break;
case GREATER_OR_EQUALS:
visitBinaryFunction(expr, "Long_ge");
break;
default:
break;
}
@ -1531,22 +1549,22 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
visitBinary(expr, "&&", false);
break;
case BITWISE_OR:
visitBinary(expr, "|", expr.getType() == OperationType.INT);
visitBinary(expr, "|", false);
break;
case BITWISE_AND:
visitBinary(expr, "&", expr.getType() == OperationType.INT);
visitBinary(expr, "&", false);
break;
case BITWISE_XOR:
visitBinary(expr, "^", expr.getType() == OperationType.INT);
visitBinary(expr, "^", false);
break;
case LEFT_SHIFT:
visitBinary(expr, "<<", expr.getType() == OperationType.INT);
visitBinary(expr, "<<", false);
break;
case RIGHT_SHIFT:
visitBinary(expr, ">>", expr.getType() == OperationType.INT);
visitBinary(expr, ">>", false);
break;
case UNSIGNED_RIGHT_SHIFT:
visitBinary(expr, ">>>", expr.getType() == OperationType.INT);
visitBinary(expr, ">>>", false);
break;
}
}

View File

@ -21,10 +21,6 @@ import org.teavm.codegen.SourceWriter;
import org.teavm.common.ServiceRepository;
import org.teavm.model.ListableClassReaderSource;
/**
*
* @author Alexey Andreev
*/
public interface RenderingContext extends ServiceRepository {
NamingStrategy getNaming();

View File

@ -15,10 +15,6 @@
*/
package org.teavm.javascript;
/**
*
* @author Alexey Andreev
*/
public class RenderingException extends RuntimeException {
private static final long serialVersionUID = 631804556597569547L;

View File

@ -15,10 +15,6 @@
*/
package org.teavm.javascript;
/**
*
* @author Alexey Andreev
*/
public final class RuntimeSupport {
private RuntimeSupport() {
}

View File

@ -35,10 +35,6 @@ import org.teavm.ast.ThrowStatement;
import org.teavm.ast.TryCatchStatement;
import org.teavm.ast.WhileStatement;
/**
*
* @author Alexey Andreev
*/
class TryCatchFinder implements StatementVisitor {
public boolean tryCatchFound;

View File

@ -20,10 +20,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author Alexey Andreev
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InjectedBy {

View File

@ -632,6 +632,61 @@ function Long_toNumber(val) {
}
return 0x100000000 * hi + lo;
}
function Long_eq(a, b) {
return a.hi === b.hi && a.lo === b.lo;
}
function Long_ne(a, b) {
return a.hi !== b.hi || a.lo !== b.lo;
}
function Long_gt(a, b) {
if (a.hi < b.hi) {
return false;
}
if (a.hi > b.hi) {
return true;
}
if ((a.lo >>> 1) > (b.lo >>> 1)) {
return true;
}
return (a.lo & 1) > (b.lo & 1);
}
function Long_ge(a, b) {
if (a.hi < b.hi) {
return false;
}
if (a.hi > b.hi) {
return true;
}
if ((a.lo >>> 1) >= (b.lo >>> 1)) {
return true;
}
return (a.lo & 1) >= (b.lo & 1);
}
function Long_lt(a, b) {
if (a.hi > b.hi) {
return false;
}
if (a.hi < b.hi) {
return true;
}
if ((a.lo >>> 1) < (b.lo >>> 1)) {
return true;
}
return (a.lo & 1) < (b.lo & 1);
}
function Long_le(a, b) {
if (a.hi > b.hi) {
return false;
}
if (a.hi < b.hi) {
return true;
}
if ((a.lo >>> 1) <= (b.lo >>> 1)) {
return true;
}
return (a.lo & 1) <= (b.lo & 1);
}
function Long_add(a, b) {
if (a.hi === (a.lo >> 31) && b.hi === (b.lo >> 31)) {
return Long_fromNumber(a.lo + b.lo);