2007-02-02 17:25:09 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*****************************************************************************/
|
|
|
|
package com.ibm.wala.cast.js.ssa;
|
|
|
|
|
2009-04-09 20:31:14 +00:00
|
|
|
import java.util.Collection;
|
2007-02-02 17:25:09 +00:00
|
|
|
|
2009-04-09 20:31:14 +00:00
|
|
|
import com.ibm.wala.cast.ir.ssa.AbstractLexicalInvoke;
|
2007-02-02 17:25:09 +00:00
|
|
|
import com.ibm.wala.cast.ir.ssa.AstLexicalAccess.Access;
|
2009-04-09 20:31:14 +00:00
|
|
|
import com.ibm.wala.cast.js.types.JavaScriptMethods;
|
|
|
|
import com.ibm.wala.classLoader.CallSiteReference;
|
|
|
|
import com.ibm.wala.ssa.SSAInstruction;
|
2009-04-10 18:33:07 +00:00
|
|
|
import com.ibm.wala.ssa.SSAInstructionFactory;
|
2009-04-09 20:31:14 +00:00
|
|
|
import com.ibm.wala.ssa.SymbolTable;
|
|
|
|
import com.ibm.wala.types.TypeReference;
|
2007-02-02 17:25:09 +00:00
|
|
|
|
|
|
|
public class JavaScriptInvoke extends AbstractLexicalInvoke {
|
|
|
|
/**
|
|
|
|
* The value numbers of the arguments passed to the call.
|
|
|
|
*/
|
|
|
|
private final int[] params;
|
|
|
|
|
|
|
|
private int function;
|
|
|
|
|
2009-04-10 18:33:07 +00:00
|
|
|
public JavaScriptInvoke(int function, int results[], int[] params, int exception, CallSiteReference site) {
|
2007-12-10 04:32:54 +00:00
|
|
|
super(results, exception, site);
|
2007-02-02 17:25:09 +00:00
|
|
|
this.function = function;
|
|
|
|
this.params = params;
|
|
|
|
}
|
|
|
|
|
2009-04-10 18:33:07 +00:00
|
|
|
public JavaScriptInvoke(int function, int results[], int[] params, int exception, CallSiteReference site, Access[] lexicalReads,
|
2008-01-15 14:40:23 +00:00
|
|
|
Access[] lexicalWrites) {
|
2007-12-10 04:32:54 +00:00
|
|
|
super(results, exception, site, lexicalReads, lexicalWrites);
|
2007-02-02 17:25:09 +00:00
|
|
|
this.function = function;
|
|
|
|
this.params = params;
|
|
|
|
}
|
|
|
|
|
2007-12-10 04:32:54 +00:00
|
|
|
public JavaScriptInvoke(int function, int result, int[] params, int exception, CallSiteReference site) {
|
2008-01-15 14:40:23 +00:00
|
|
|
this(function, new int[] { result }, params, exception, site);
|
2007-12-10 04:32:54 +00:00
|
|
|
}
|
|
|
|
|
2007-02-02 17:25:09 +00:00
|
|
|
public JavaScriptInvoke(int function, int[] params, int exception, CallSiteReference site) {
|
2007-12-10 04:32:54 +00:00
|
|
|
this(function, null, params, exception, site);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2009-04-10 18:33:07 +00:00
|
|
|
public SSAInstruction copyForSSA(SSAInstructionFactory insts, int[] defs, int[] uses) {
|
2007-02-02 17:25:09 +00:00
|
|
|
int fn = function;
|
|
|
|
int newParams[] = params;
|
|
|
|
Access[] reads = lexicalReads;
|
|
|
|
|
|
|
|
if (uses != null) {
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
fn = uses[i++];
|
|
|
|
|
2008-01-15 14:40:23 +00:00
|
|
|
newParams = new int[params.length];
|
|
|
|
for (int j = 0; j < newParams.length; j++)
|
|
|
|
newParams[j] = uses[i++];
|
2007-02-02 17:25:09 +00:00
|
|
|
|
|
|
|
if (lexicalReads != null) {
|
2008-01-15 14:40:23 +00:00
|
|
|
reads = new Access[lexicalReads.length];
|
|
|
|
for (int j = 0; j < reads.length; j++)
|
|
|
|
reads[j] = new Access(lexicalReads[j].variableName, lexicalReads[j].variableDefiner, uses[i++]);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-15 14:40:23 +00:00
|
|
|
int newLvals[] = new int[results.length];
|
2007-12-10 04:32:54 +00:00
|
|
|
System.arraycopy(results, 0, newLvals, 0, results.length);
|
2007-02-02 17:25:09 +00:00
|
|
|
int newExp = exception;
|
|
|
|
Access[] writes = lexicalWrites;
|
2008-01-15 14:40:23 +00:00
|
|
|
|
2007-02-02 17:25:09 +00:00
|
|
|
if (defs != null) {
|
|
|
|
int i = 0;
|
2007-12-10 04:32:54 +00:00
|
|
|
if (getNumberOfReturnValues() > 0) {
|
2008-01-15 14:40:23 +00:00
|
|
|
newLvals[0] = defs[i++];
|
2007-12-10 04:32:54 +00:00
|
|
|
}
|
2007-02-02 17:25:09 +00:00
|
|
|
newExp = defs[i++];
|
2008-01-15 14:40:23 +00:00
|
|
|
for (int j = 1; j < getNumberOfReturnValues(); j++) {
|
|
|
|
newLvals[j] = defs[i++];
|
2007-12-10 04:32:54 +00:00
|
|
|
}
|
|
|
|
|
2007-02-02 17:25:09 +00:00
|
|
|
if (lexicalWrites != null) {
|
2008-01-15 14:40:23 +00:00
|
|
|
writes = new Access[lexicalWrites.length];
|
|
|
|
for (int j = 0; j < writes.length; j++)
|
|
|
|
writes[j] = new Access(lexicalWrites[j].variableName, lexicalWrites[j].variableDefiner, defs[i++]);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-10 18:33:07 +00:00
|
|
|
return ((JSInstructionFactory)insts).Invoke(fn, newLvals, newParams, newExp, site, reads, writes);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
2008-01-15 14:40:23 +00:00
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2008-01-15 14:40:23 +00:00
|
|
|
public String toString(SymbolTable symbolTable) {
|
2007-02-02 17:25:09 +00:00
|
|
|
StringBuffer s = new StringBuffer();
|
2007-12-10 04:32:54 +00:00
|
|
|
if (getNumberOfReturnValues() > 0) {
|
2008-01-15 14:40:23 +00:00
|
|
|
s.append(getValueString(symbolTable, getReturnValue(0)));
|
2007-12-10 04:32:54 +00:00
|
|
|
s.append(" = ");
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
if (site.getDeclaredTarget().equals(JavaScriptMethods.ctorReference))
|
|
|
|
s.append("construct ");
|
2012-03-01 02:45:51 +00:00
|
|
|
else if (site.getDeclaredTarget().equals(JavaScriptMethods.dispatchReference))
|
|
|
|
s.append("dispatch ");
|
2007-02-02 17:25:09 +00:00
|
|
|
else
|
|
|
|
s.append("invoke ");
|
2008-01-15 14:40:23 +00:00
|
|
|
s.append(getValueString(symbolTable, function));
|
2007-02-02 17:25:09 +00:00
|
|
|
|
2008-01-15 14:40:23 +00:00
|
|
|
if (site != null)
|
|
|
|
s.append("@").append(site.getProgramCounter());
|
2007-02-02 17:25:09 +00:00
|
|
|
|
|
|
|
if (params != null) {
|
|
|
|
if (params.length > 0) {
|
2008-01-15 14:40:23 +00:00
|
|
|
s.append(" ").append(getValueString(symbolTable, params[0]));
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
for (int i = 1; i < params.length; i++) {
|
2008-01-15 14:40:23 +00:00
|
|
|
s.append(",").append(getValueString(symbolTable, params[i]));
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-15 14:40:23 +00:00
|
|
|
|
2007-02-02 17:25:09 +00:00
|
|
|
if (exception == -1) {
|
|
|
|
s.append(" exception: NOT MODELED");
|
|
|
|
} else {
|
2008-01-15 14:40:23 +00:00
|
|
|
s.append(" exception:").append(getValueString(symbolTable, exception));
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
2008-01-15 14:40:23 +00:00
|
|
|
|
2007-02-02 17:25:09 +00:00
|
|
|
if (lexicalReads != null) {
|
|
|
|
s.append(" (reads:");
|
2008-01-15 14:40:23 +00:00
|
|
|
for (int i = 0; i < lexicalReads.length; i++) {
|
|
|
|
s.append(" ").append(lexicalReads[i].variableName).append(":").append(
|
|
|
|
getValueString(symbolTable, lexicalReads[i].valueNumber));
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
s.append(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lexicalWrites != null) {
|
|
|
|
s.append(" (writes:");
|
2008-01-15 14:40:23 +00:00
|
|
|
for (int i = 0; i < lexicalWrites.length; i++) {
|
|
|
|
s.append(" ").append(lexicalWrites[i].variableName).append(":").append(
|
|
|
|
getValueString(symbolTable, lexicalWrites[i].valueNumber));
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
s.append(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see com.ibm.domo.ssa.Instruction#visit(Visitor)
|
|
|
|
*/
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-02 17:25:09 +00:00
|
|
|
public void visit(IVisitor v) {
|
2012-03-01 02:45:51 +00:00
|
|
|
assert v instanceof JSInstructionVisitor;
|
|
|
|
((JSInstructionVisitor) v).visitJavaScriptInvoke(this);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-02 17:25:09 +00:00
|
|
|
public int getNumberOfParameters() {
|
|
|
|
if (params == null) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
2008-01-15 14:40:23 +00:00
|
|
|
return params.length + 1;
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see com.ibm.domo.ssa.Instruction#getUse(int)
|
|
|
|
*/
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-02 17:25:09 +00:00
|
|
|
public int getUse(int j) {
|
|
|
|
if (j == 0)
|
|
|
|
return function;
|
|
|
|
else if (j <= params.length)
|
2008-01-15 14:40:23 +00:00
|
|
|
return params[j - 1];
|
2007-02-02 17:25:09 +00:00
|
|
|
else {
|
|
|
|
return super.getUse(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getFunction() {
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
2008-01-15 14:40:23 +00:00
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
2007-02-02 17:25:09 +00:00
|
|
|
* @see com.ibm.domo.ssa.Instruction#getExceptionTypes()
|
|
|
|
*/
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-09 15:34:25 +00:00
|
|
|
public Collection<TypeReference> getExceptionTypes() {
|
2007-02-02 17:25:09 +00:00
|
|
|
return Util.typeErrorExceptions();
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-02-02 17:25:09 +00:00
|
|
|
public int hashCode() {
|
|
|
|
return site.hashCode() * function * 7529;
|
|
|
|
}
|
|
|
|
|
2008-01-15 14:40:23 +00:00
|
|
|
// public boolean equals(Object obj) {
|
|
|
|
// if (obj instanceof JavaScriptInvoke) {
|
|
|
|
// JavaScriptInvoke other = (JavaScriptInvoke)obj;
|
|
|
|
// if (site.equals(other.site)) {
|
|
|
|
// if (getNumberOfUses() == other.getNumberOfUses()) {
|
|
|
|
// for(int i = 0; i < getNumberOfUses(); i++) {
|
|
|
|
// if (getUse(i) != other.getUse(i)) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (getNumberOfDefs() == other.getNumberOfDefs()) {
|
|
|
|
// for(int i = 0; i < getNumberOfDefs(); i++) {
|
|
|
|
// if (getDef(i) != other.getDef(i)) {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return false;
|
|
|
|
// }
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|