| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package org.mozilla.javascript.optimizer; |
| 9 |
|
| 10 |
import org.mozilla.javascript.*; |
| 11 |
import org.mozilla.javascript.ast.FunctionNode; |
| 12 |
import org.mozilla.javascript.ast.Jump; |
| 13 |
import org.mozilla.javascript.ast.Name; |
| 14 |
import org.mozilla.javascript.ast.ScriptNode; |
| 15 |
import org.mozilla.classfile.*; |
| 16 |
|
| 17 |
import java.util.*; |
| 18 |
import java.lang.reflect.Constructor; |
| 19 |
|
| 20 |
import static org.mozilla.classfile.ClassFileWriter.ACC_FINAL; |
| 21 |
import static org.mozilla.classfile.ClassFileWriter.ACC_PRIVATE; |
| 22 |
import static org.mozilla.classfile.ClassFileWriter.ACC_PUBLIC; |
| 23 |
import static org.mozilla.classfile.ClassFileWriter.ACC_STATIC; |
| 24 |
import static org.mozilla.classfile.ClassFileWriter.ACC_VOLATILE; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class Codegen implements Evaluator |
| 34 |
{ |
| 35 |
public void captureStackInfo(RhinoException ex) { |
| 36 |
throw new UnsupportedOperationException(); |
| 37 |
} |
| 38 |
|
| 39 |
public String getSourcePositionFromStack(Context cx, int[] linep) { |
| 40 |
throw new UnsupportedOperationException(); |
| 41 |
} |
| 42 |
|
| 43 |
public String getPatchedStack(RhinoException ex, String nativeStackTrace) { |
| 44 |
throw new UnsupportedOperationException(); |
| 45 |
} |
| 46 |
|
| 47 |
public List<String> getScriptStack(RhinoException ex) { |
| 48 |
throw new UnsupportedOperationException(); |
| 49 |
} |
| 50 |
|
| 51 |
public void setEvalScriptFlag(Script script) { |
| 52 |
throw new UnsupportedOperationException(); |
| 53 |
} |
| 54 |
|
| 55 |
public Object compile(CompilerEnvirons compilerEnv, |
| 56 |
ScriptNode tree, |
| 57 |
String encodedSource, |
| 58 |
boolean returnFunction) |
| 59 |
{ |
| 60 |
int serial; |
| 61 |
synchronized (globalLock) { |
| 62 |
serial = ++globalSerialClassCounter; |
| 63 |
} |
| 64 |
|
| 65 |
String baseName = "c"; |
| 66 |
if (tree.getSourceName().length() > 0) { |
| 67 |
baseName = tree.getSourceName().replaceAll("\\W", "_"); |
| 68 |
if (!Character.isJavaIdentifierStart(baseName.charAt(0))) { |
| 69 |
baseName = "_" + baseName; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
String mainClassName = "org.mozilla.javascript.gen." + baseName + "_" + serial; |
| 74 |
|
| 75 |
byte[] mainClassBytes = compileToClassFile(compilerEnv, mainClassName, |
| 76 |
tree, encodedSource, |
| 77 |
returnFunction); |
| 78 |
|
| 79 |
return new Object[] { mainClassName, mainClassBytes }; |
| 80 |
} |
| 81 |
|
| 82 |
public Script createScriptObject(Object bytecode, |
| 83 |
Object staticSecurityDomain) |
| 84 |
{ |
| 85 |
Class<?> cl = defineClass(bytecode, staticSecurityDomain); |
| 86 |
|
| 87 |
Script script; |
| 88 |
try { |
| 89 |
script = (Script)cl.newInstance(); |
| 90 |
} catch (Exception ex) { |
| 91 |
throw new RuntimeException |
| 92 |
("Unable to instantiate compiled class:" + ex.toString()); |
| 93 |
} |
| 94 |
return script; |
| 95 |
} |
| 96 |
|
| 97 |
public Function createFunctionObject(Context cx, Scriptable scope, |
| 98 |
Object bytecode, |
| 99 |
Object staticSecurityDomain) |
| 100 |
{ |
| 101 |
Class<?> cl = defineClass(bytecode, staticSecurityDomain); |
| 102 |
|
| 103 |
NativeFunction f; |
| 104 |
try { |
| 105 |
Constructor<?>ctor = cl.getConstructors()[0]; |
| 106 |
Object[] initArgs = { scope, cx, Integer.valueOf(0) }; |
| 107 |
f = (NativeFunction)ctor.newInstance(initArgs); |
| 108 |
} catch (Exception ex) { |
| 109 |
throw new RuntimeException |
| 110 |
("Unable to instantiate compiled class:"+ex.toString()); |
| 111 |
} |
| 112 |
return f; |
| 113 |
} |
| 114 |
|
| 115 |
private Class<?> defineClass(Object bytecode, |
| 116 |
Object staticSecurityDomain) |
| 117 |
{ |
| 118 |
Object[] nameBytesPair = (Object[])bytecode; |
| 119 |
String className = (String)nameBytesPair[0]; |
| 120 |
byte[] classBytes = (byte[])nameBytesPair[1]; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ClassLoader rhinoLoader = getClass().getClassLoader(); |
| 125 |
GeneratedClassLoader loader; |
| 126 |
loader = SecurityController.createLoader(rhinoLoader, |
| 127 |
staticSecurityDomain); |
| 128 |
Exception e; |
| 129 |
try { |
| 130 |
Class<?> cl = loader.defineClass(className, classBytes); |
| 131 |
loader.linkClass(cl); |
| 132 |
return cl; |
| 133 |
} catch (SecurityException x) { |
| 134 |
e = x; |
| 135 |
} catch (IllegalArgumentException x) { |
| 136 |
e = x; |
| 137 |
} |
| 138 |
throw new RuntimeException("Malformed optimizer package " + e); |
| 139 |
} |
| 140 |
|
| 141 |
public byte[] compileToClassFile(CompilerEnvirons compilerEnv, |
| 142 |
String mainClassName, |
| 143 |
ScriptNode scriptOrFn, |
| 144 |
String encodedSource, |
| 145 |
boolean returnFunction) |
| 146 |
{ |
| 147 |
this.compilerEnv = compilerEnv; |
| 148 |
|
| 149 |
transform(scriptOrFn); |
| 150 |
|
| 151 |
if (Token.printTrees) { |
| 152 |
System.out.println(scriptOrFn.toStringTree(scriptOrFn)); |
| 153 |
} |
| 154 |
|
| 155 |
if (returnFunction) { |
| 156 |
scriptOrFn = scriptOrFn.getFunctionNode(0); |
| 157 |
} |
| 158 |
|
| 159 |
initScriptNodesData(scriptOrFn); |
| 160 |
|
| 161 |
this.mainClassName = mainClassName; |
| 162 |
this.mainClassSignature |
| 163 |
= ClassFileWriter.classNameToSignature(mainClassName); |
| 164 |
|
| 165 |
try { |
| 166 |
return generateCode(encodedSource); |
| 167 |
} catch (ClassFileWriter.ClassFileFormatException e) { |
| 168 |
throw reportClassFileFormatException(scriptOrFn, e.getMessage()); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
private RuntimeException reportClassFileFormatException( |
| 173 |
ScriptNode scriptOrFn, |
| 174 |
String message) |
| 175 |
{ |
| 176 |
String msg = scriptOrFn instanceof FunctionNode |
| 177 |
? ScriptRuntime.getMessage2("msg.while.compiling.fn", |
| 178 |
((FunctionNode)scriptOrFn).getFunctionName(), message) |
| 179 |
: ScriptRuntime.getMessage1("msg.while.compiling.script", message); |
| 180 |
return Context.reportRuntimeError(msg, scriptOrFn.getSourceName(), |
| 181 |
scriptOrFn.getLineno(), null, 0); |
| 182 |
} |
| 183 |
|
| 184 |
private void transform(ScriptNode tree) |
| 185 |
{ |
| 186 |
initOptFunctions_r(tree); |
| 187 |
|
| 188 |
int optLevel = compilerEnv.getOptimizationLevel(); |
| 189 |
|
| 190 |
Map<String,OptFunctionNode> possibleDirectCalls = null; |
| 191 |
if (optLevel > 0) { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if (tree.getType() == Token.SCRIPT) { |
| 198 |
int functionCount = tree.getFunctionCount(); |
| 199 |
for (int i = 0; i != functionCount; ++i) { |
| 200 |
OptFunctionNode ofn = OptFunctionNode.get(tree, i); |
| 201 |
if (ofn.fnode.getFunctionType() |
| 202 |
== FunctionNode.FUNCTION_STATEMENT) |
| 203 |
{ |
| 204 |
String name = ofn.fnode.getName(); |
| 205 |
if (name.length() != 0) { |
| 206 |
if (possibleDirectCalls == null) { |
| 207 |
possibleDirectCalls = new HashMap<String,OptFunctionNode>(); |
| 208 |
} |
| 209 |
possibleDirectCalls.put(name, ofn); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if (possibleDirectCalls != null) { |
| 217 |
directCallTargets = new ObjArray(); |
| 218 |
} |
| 219 |
|
| 220 |
OptTransformer ot = new OptTransformer(possibleDirectCalls, |
| 221 |
directCallTargets); |
| 222 |
ot.transform(tree); |
| 223 |
|
| 224 |
if (optLevel > 0) { |
| 225 |
(new Optimizer()).optimize(tree); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
private static void initOptFunctions_r(ScriptNode scriptOrFn) |
| 230 |
{ |
| 231 |
for (int i = 0, N = scriptOrFn.getFunctionCount(); i != N; ++i) { |
| 232 |
FunctionNode fn = scriptOrFn.getFunctionNode(i); |
| 233 |
new OptFunctionNode(fn); |
| 234 |
initOptFunctions_r(fn); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
private void initScriptNodesData(ScriptNode scriptOrFn) |
| 239 |
{ |
| 240 |
ObjArray x = new ObjArray(); |
| 241 |
collectScriptNodes_r(scriptOrFn, x); |
| 242 |
|
| 243 |
int count = x.size(); |
| 244 |
scriptOrFnNodes = new ScriptNode[count]; |
| 245 |
x.toArray(scriptOrFnNodes); |
| 246 |
|
| 247 |
scriptOrFnIndexes = new ObjToIntMap(count); |
| 248 |
for (int i = 0; i != count; ++i) { |
| 249 |
scriptOrFnIndexes.put(scriptOrFnNodes[i], i); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private static void collectScriptNodes_r(ScriptNode n, |
| 254 |
ObjArray x) |
| 255 |
{ |
| 256 |
x.add(n); |
| 257 |
int nestedCount = n.getFunctionCount(); |
| 258 |
for (int i = 0; i != nestedCount; ++i) { |
| 259 |
collectScriptNodes_r(n.getFunctionNode(i), x); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
private byte[] generateCode(String encodedSource) |
| 264 |
{ |
| 265 |
boolean hasScript = (scriptOrFnNodes[0].getType() == Token.SCRIPT); |
| 266 |
boolean hasFunctions = (scriptOrFnNodes.length > 1 || !hasScript); |
| 267 |
boolean isStrictMode = scriptOrFnNodes[0].isInStrictMode(); |
| 268 |
|
| 269 |
String sourceFile = null; |
| 270 |
if (compilerEnv.isGenerateDebugInfo()) { |
| 271 |
sourceFile = scriptOrFnNodes[0].getSourceName(); |
| 272 |
} |
| 273 |
|
| 274 |
ClassFileWriter cfw = new ClassFileWriter(mainClassName, |
| 275 |
SUPER_CLASS_NAME, |
| 276 |
sourceFile); |
| 277 |
cfw.addField(ID_FIELD_NAME, "I", ACC_PRIVATE); |
| 278 |
|
| 279 |
if (hasFunctions) { |
| 280 |
generateFunctionConstructor(cfw); |
| 281 |
} |
| 282 |
|
| 283 |
if (hasScript) { |
| 284 |
cfw.addInterface("org/mozilla/javascript/Script"); |
| 285 |
generateScriptCtor(cfw); |
| 286 |
generateMain(cfw); |
| 287 |
generateExecute(cfw); |
| 288 |
} |
| 289 |
|
| 290 |
generateCallMethod(cfw, isStrictMode); |
| 291 |
generateResumeGenerator(cfw); |
| 292 |
|
| 293 |
generateNativeFunctionOverrides(cfw, encodedSource); |
| 294 |
|
| 295 |
int count = scriptOrFnNodes.length; |
| 296 |
for (int i = 0; i != count; ++i) { |
| 297 |
ScriptNode n = scriptOrFnNodes[i]; |
| 298 |
|
| 299 |
BodyCodegen bodygen = new BodyCodegen(); |
| 300 |
bodygen.cfw = cfw; |
| 301 |
bodygen.codegen = this; |
| 302 |
bodygen.compilerEnv = compilerEnv; |
| 303 |
bodygen.scriptOrFn = n; |
| 304 |
bodygen.scriptOrFnIndex = i; |
| 305 |
|
| 306 |
try { |
| 307 |
bodygen.generateBodyCode(); |
| 308 |
} catch (ClassFileWriter.ClassFileFormatException e) { |
| 309 |
throw reportClassFileFormatException(n, e.getMessage()); |
| 310 |
} |
| 311 |
|
| 312 |
if (n.getType() == Token.FUNCTION) { |
| 313 |
OptFunctionNode ofn = OptFunctionNode.get(n); |
| 314 |
generateFunctionInit(cfw, ofn); |
| 315 |
if (ofn.isTargetOfDirectCall()) { |
| 316 |
emitDirectConstructor(cfw, ofn); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
emitRegExpInit(cfw); |
| 322 |
emitConstantDudeInitializers(cfw); |
| 323 |
|
| 324 |
return cfw.toByteArray(); |
| 325 |
} |
| 326 |
|
| 327 |
private void emitDirectConstructor(ClassFileWriter cfw, |
| 328 |
OptFunctionNode ofn) |
| 329 |
{ |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
cfw.startMethod(getDirectCtorName(ofn.fnode), |
| 342 |
getBodyMethodSignature(ofn.fnode), |
| 343 |
(short)(ACC_STATIC | ACC_PRIVATE)); |
| 344 |
|
| 345 |
int argCount = ofn.fnode.getParamCount(); |
| 346 |
int firstLocal = (4 + argCount * 3) + 1; |
| 347 |
|
| 348 |
cfw.addALoad(0); |
| 349 |
cfw.addALoad(1); |
| 350 |
cfw.addALoad(2); |
| 351 |
cfw.addInvoke(ByteCode.INVOKEVIRTUAL, |
| 352 |
"org/mozilla/javascript/BaseFunction", |
| 353 |
"createObject", |
| 354 |
"(Lorg/mozilla/javascript/Context;" |
| 355 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 356 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 357 |
cfw.addAStore(firstLocal); |
| 358 |
|
| 359 |
cfw.addALoad(0); |
| 360 |
cfw.addALoad(1); |
| 361 |
cfw.addALoad(2); |
| 362 |
cfw.addALoad(firstLocal); |
| 363 |
for (int i = 0; i < argCount; i++) { |
| 364 |
cfw.addALoad(4 + (i * 3)); |
| 365 |
cfw.addDLoad(5 + (i * 3)); |
| 366 |
} |
| 367 |
cfw.addALoad(4 + argCount * 3); |
| 368 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 369 |
mainClassName, |
| 370 |
getBodyMethodName(ofn.fnode), |
| 371 |
getBodyMethodSignature(ofn.fnode)); |
| 372 |
int exitLabel = cfw.acquireLabel(); |
| 373 |
cfw.add(ByteCode.DUP); |
| 374 |
cfw.add(ByteCode.INSTANCEOF, "org/mozilla/javascript/Scriptable"); |
| 375 |
cfw.add(ByteCode.IFEQ, exitLabel); |
| 376 |
|
| 377 |
cfw.add(ByteCode.CHECKCAST, "org/mozilla/javascript/Scriptable"); |
| 378 |
cfw.add(ByteCode.ARETURN); |
| 379 |
cfw.markLabel(exitLabel); |
| 380 |
|
| 381 |
cfw.addALoad(firstLocal); |
| 382 |
cfw.add(ByteCode.ARETURN); |
| 383 |
|
| 384 |
cfw.stopMethod((short)(firstLocal + 1)); |
| 385 |
} |
| 386 |
|
| 387 |
static boolean isGenerator(ScriptNode node) |
| 388 |
{ |
| 389 |
return (node.getType() == Token.FUNCTION ) && |
| 390 |
((FunctionNode)node).isGenerator(); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
private void generateResumeGenerator(ClassFileWriter cfw) |
| 406 |
{ |
| 407 |
boolean hasGenerators = false; |
| 408 |
for (int i=0; i < scriptOrFnNodes.length; i++) { |
| 409 |
if (isGenerator(scriptOrFnNodes[i])) |
| 410 |
hasGenerators = true; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if (!hasGenerators) |
| 416 |
return; |
| 417 |
|
| 418 |
cfw.startMethod("resumeGenerator", |
| 419 |
"(Lorg/mozilla/javascript/Context;" + |
| 420 |
"Lorg/mozilla/javascript/Scriptable;" + |
| 421 |
"ILjava/lang/Object;" + |
| 422 |
"Ljava/lang/Object;)Ljava/lang/Object;", |
| 423 |
(short)(ACC_PUBLIC | ACC_FINAL)); |
| 424 |
|
| 425 |
|
| 426 |
cfw.addALoad(0); |
| 427 |
cfw.addALoad(1); |
| 428 |
cfw.addALoad(2); |
| 429 |
cfw.addALoad(4); |
| 430 |
cfw.addALoad(5); |
| 431 |
cfw.addILoad(3); |
| 432 |
|
| 433 |
cfw.addLoadThis(); |
| 434 |
cfw.add(ByteCode.GETFIELD, cfw.getClassName(), ID_FIELD_NAME, "I"); |
| 435 |
|
| 436 |
int startSwitch = cfw.addTableSwitch(0, scriptOrFnNodes.length - 1); |
| 437 |
cfw.markTableSwitchDefault(startSwitch); |
| 438 |
int endlabel = cfw.acquireLabel(); |
| 439 |
|
| 440 |
for (int i = 0; i < scriptOrFnNodes.length; i++) { |
| 441 |
ScriptNode n = scriptOrFnNodes[i]; |
| 442 |
cfw.markTableSwitchCase(startSwitch, i, (short)6); |
| 443 |
if (isGenerator(n)) { |
| 444 |
String type = "(" + |
| 445 |
mainClassSignature + |
| 446 |
"Lorg/mozilla/javascript/Context;" + |
| 447 |
"Lorg/mozilla/javascript/Scriptable;" + |
| 448 |
"Ljava/lang/Object;" + |
| 449 |
"Ljava/lang/Object;I)Ljava/lang/Object;"; |
| 450 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 451 |
mainClassName, |
| 452 |
getBodyMethodName(n) + "_gen", |
| 453 |
type); |
| 454 |
cfw.add(ByteCode.ARETURN); |
| 455 |
} else { |
| 456 |
cfw.add(ByteCode.GOTO, endlabel); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
cfw.markLabel(endlabel); |
| 461 |
pushUndefined(cfw); |
| 462 |
cfw.add(ByteCode.ARETURN); |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
cfw.stopMethod((short)6); |
| 467 |
} |
| 468 |
|
| 469 |
private void generateCallMethod(ClassFileWriter cfw, boolean isStrictMode) |
| 470 |
{ |
| 471 |
cfw.startMethod("call", |
| 472 |
"(Lorg/mozilla/javascript/Context;" + |
| 473 |
"Lorg/mozilla/javascript/Scriptable;" + |
| 474 |
"Lorg/mozilla/javascript/Scriptable;" + |
| 475 |
"[Ljava/lang/Object;)Ljava/lang/Object;", |
| 476 |
(short)(ACC_PUBLIC | ACC_FINAL)); |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
int nonTopCallLabel = cfw.acquireLabel(); |
| 484 |
cfw.addALoad(1); |
| 485 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 486 |
"org/mozilla/javascript/ScriptRuntime", |
| 487 |
"hasTopCall", |
| 488 |
"(Lorg/mozilla/javascript/Context;" |
| 489 |
+")Z"); |
| 490 |
cfw.add(ByteCode.IFNE, nonTopCallLabel); |
| 491 |
cfw.addALoad(0); |
| 492 |
cfw.addALoad(1); |
| 493 |
cfw.addALoad(2); |
| 494 |
cfw.addALoad(3); |
| 495 |
cfw.addALoad(4); |
| 496 |
cfw.addPush(isStrictMode); |
| 497 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 498 |
"org/mozilla/javascript/ScriptRuntime", |
| 499 |
"doTopCall", |
| 500 |
"(Lorg/mozilla/javascript/Callable;" |
| 501 |
+"Lorg/mozilla/javascript/Context;" |
| 502 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 503 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 504 |
+"[Ljava/lang/Object;" |
| 505 |
+"Z" |
| 506 |
+")Ljava/lang/Object;"); |
| 507 |
cfw.add(ByteCode.ARETURN); |
| 508 |
cfw.markLabel(nonTopCallLabel); |
| 509 |
|
| 510 |
|
| 511 |
cfw.addALoad(0); |
| 512 |
cfw.addALoad(1); |
| 513 |
cfw.addALoad(2); |
| 514 |
cfw.addALoad(3); |
| 515 |
cfw.addALoad(4); |
| 516 |
|
| 517 |
int end = scriptOrFnNodes.length; |
| 518 |
boolean generateSwitch = (2 <= end); |
| 519 |
|
| 520 |
int switchStart = 0; |
| 521 |
int switchStackTop = 0; |
| 522 |
if (generateSwitch) { |
| 523 |
cfw.addLoadThis(); |
| 524 |
cfw.add(ByteCode.GETFIELD, cfw.getClassName(), ID_FIELD_NAME, "I"); |
| 525 |
|
| 526 |
|
| 527 |
switchStart = cfw.addTableSwitch(1, end - 1); |
| 528 |
} |
| 529 |
|
| 530 |
for (int i = 0; i != end; ++i) { |
| 531 |
ScriptNode n = scriptOrFnNodes[i]; |
| 532 |
if (generateSwitch) { |
| 533 |
if (i == 0) { |
| 534 |
cfw.markTableSwitchDefault(switchStart); |
| 535 |
switchStackTop = cfw.getStackTop(); |
| 536 |
} else { |
| 537 |
cfw.markTableSwitchCase(switchStart, i - 1, |
| 538 |
switchStackTop); |
| 539 |
} |
| 540 |
} |
| 541 |
if (n.getType() == Token.FUNCTION) { |
| 542 |
OptFunctionNode ofn = OptFunctionNode.get(n); |
| 543 |
if (ofn.isTargetOfDirectCall()) { |
| 544 |
int pcount = ofn.fnode.getParamCount(); |
| 545 |
if (pcount != 0) { |
| 546 |
|
| 547 |
|
| 548 |
for (int p = 0; p != pcount; ++p) { |
| 549 |
cfw.add(ByteCode.ARRAYLENGTH); |
| 550 |
cfw.addPush(p); |
| 551 |
int undefArg = cfw.acquireLabel(); |
| 552 |
int beyond = cfw.acquireLabel(); |
| 553 |
cfw.add(ByteCode.IF_ICMPLE, undefArg); |
| 554 |
|
| 555 |
cfw.addALoad(4); |
| 556 |
cfw.addPush(p); |
| 557 |
cfw.add(ByteCode.AALOAD); |
| 558 |
cfw.add(ByteCode.GOTO, beyond); |
| 559 |
cfw.markLabel(undefArg); |
| 560 |
pushUndefined(cfw); |
| 561 |
cfw.markLabel(beyond); |
| 562 |
|
| 563 |
cfw.adjustStackTop(-1); |
| 564 |
cfw.addPush(0.0); |
| 565 |
|
| 566 |
cfw.addALoad(4); |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 572 |
mainClassName, |
| 573 |
getBodyMethodName(n), |
| 574 |
getBodyMethodSignature(n)); |
| 575 |
cfw.add(ByteCode.ARETURN); |
| 576 |
} |
| 577 |
cfw.stopMethod((short)5); |
| 578 |
|
| 579 |
} |
| 580 |
|
| 581 |
private void generateMain(ClassFileWriter cfw) |
| 582 |
{ |
| 583 |
cfw.startMethod("main", "([Ljava/lang/String;)V", |
| 584 |
(short)(ACC_PUBLIC | ACC_STATIC)); |
| 585 |
|
| 586 |
|
| 587 |
cfw.add(ByteCode.NEW, cfw.getClassName()); |
| 588 |
cfw.add(ByteCode.DUP); |
| 589 |
cfw.addInvoke(ByteCode.INVOKESPECIAL, cfw.getClassName(), |
| 590 |
"<init>", "()V"); |
| 591 |
|
| 592 |
cfw.add(ByteCode.ALOAD_0); |
| 593 |
|
| 594 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 595 |
mainMethodClass, |
| 596 |
"main", |
| 597 |
"(Lorg/mozilla/javascript/Script;[Ljava/lang/String;)V"); |
| 598 |
cfw.add(ByteCode.RETURN); |
| 599 |
|
| 600 |
cfw.stopMethod((short)1); |
| 601 |
} |
| 602 |
|
| 603 |
private void generateExecute(ClassFileWriter cfw) |
| 604 |
{ |
| 605 |
cfw.startMethod("exec", |
| 606 |
"(Lorg/mozilla/javascript/Context;" |
| 607 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 608 |
+")Ljava/lang/Object;", |
| 609 |
(short)(ACC_PUBLIC | ACC_FINAL)); |
| 610 |
|
| 611 |
final int CONTEXT_ARG = 1; |
| 612 |
final int SCOPE_ARG = 2; |
| 613 |
|
| 614 |
cfw.addLoadThis(); |
| 615 |
cfw.addALoad(CONTEXT_ARG); |
| 616 |
cfw.addALoad(SCOPE_ARG); |
| 617 |
cfw.add(ByteCode.DUP); |
| 618 |
cfw.add(ByteCode.ACONST_NULL); |
| 619 |
cfw.addInvoke(ByteCode.INVOKEVIRTUAL, |
| 620 |
cfw.getClassName(), |
| 621 |
"call", |
| 622 |
"(Lorg/mozilla/javascript/Context;" |
| 623 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 624 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 625 |
+"[Ljava/lang/Object;" |
| 626 |
+")Ljava/lang/Object;"); |
| 627 |
|
| 628 |
cfw.add(ByteCode.ARETURN); |
| 629 |
|
| 630 |
cfw.stopMethod((short)3); |
| 631 |
} |
| 632 |
|
| 633 |
private void generateScriptCtor(ClassFileWriter cfw) |
| 634 |
{ |
| 635 |
cfw.startMethod("<init>", "()V", ACC_PUBLIC); |
| 636 |
|
| 637 |
cfw.addLoadThis(); |
| 638 |
cfw.addInvoke(ByteCode.INVOKESPECIAL, SUPER_CLASS_NAME, |
| 639 |
"<init>", "()V"); |
| 640 |
|
| 641 |
cfw.addLoadThis(); |
| 642 |
cfw.addPush(0); |
| 643 |
cfw.add(ByteCode.PUTFIELD, cfw.getClassName(), ID_FIELD_NAME, "I"); |
| 644 |
|
| 645 |
cfw.add(ByteCode.RETURN); |
| 646 |
|
| 647 |
cfw.stopMethod((short)1); |
| 648 |
} |
| 649 |
|
| 650 |
private void generateFunctionConstructor(ClassFileWriter cfw) |
| 651 |
{ |
| 652 |
final int SCOPE_ARG = 1; |
| 653 |
final int CONTEXT_ARG = 2; |
| 654 |
final int ID_ARG = 3; |
| 655 |
|
| 656 |
cfw.startMethod("<init>", FUNCTION_CONSTRUCTOR_SIGNATURE, ACC_PUBLIC); |
| 657 |
cfw.addALoad(0); |
| 658 |
cfw.addInvoke(ByteCode.INVOKESPECIAL, SUPER_CLASS_NAME, |
| 659 |
"<init>", "()V"); |
| 660 |
|
| 661 |
cfw.addLoadThis(); |
| 662 |
cfw.addILoad(ID_ARG); |
| 663 |
cfw.add(ByteCode.PUTFIELD, cfw.getClassName(), ID_FIELD_NAME, "I"); |
| 664 |
|
| 665 |
cfw.addLoadThis(); |
| 666 |
cfw.addALoad(CONTEXT_ARG); |
| 667 |
cfw.addALoad(SCOPE_ARG); |
| 668 |
|
| 669 |
int start = (scriptOrFnNodes[0].getType() == Token.SCRIPT) ? 1 : 0; |
| 670 |
int end = scriptOrFnNodes.length; |
| 671 |
if (start == end) throw badTree(); |
| 672 |
boolean generateSwitch = (2 <= end - start); |
| 673 |
|
| 674 |
int switchStart = 0; |
| 675 |
int switchStackTop = 0; |
| 676 |
if (generateSwitch) { |
| 677 |
cfw.addILoad(ID_ARG); |
| 678 |
|
| 679 |
|
| 680 |
switchStart = cfw.addTableSwitch(start + 1, end - 1); |
| 681 |
} |
| 682 |
|
| 683 |
for (int i = start; i != end; ++i) { |
| 684 |
if (generateSwitch) { |
| 685 |
if (i == start) { |
| 686 |
cfw.markTableSwitchDefault(switchStart); |
| 687 |
switchStackTop = cfw.getStackTop(); |
| 688 |
} else { |
| 689 |
cfw.markTableSwitchCase(switchStart, i - 1 - start, |
| 690 |
switchStackTop); |
| 691 |
} |
| 692 |
} |
| 693 |
OptFunctionNode ofn = OptFunctionNode.get(scriptOrFnNodes[i]); |
| 694 |
cfw.addInvoke(ByteCode.INVOKESPECIAL, |
| 695 |
mainClassName, |
| 696 |
getFunctionInitMethodName(ofn), |
| 697 |
FUNCTION_INIT_SIGNATURE); |
| 698 |
cfw.add(ByteCode.RETURN); |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
cfw.stopMethod((short)4); |
| 703 |
} |
| 704 |
|
| 705 |
private void generateFunctionInit(ClassFileWriter cfw, |
| 706 |
OptFunctionNode ofn) |
| 707 |
{ |
| 708 |
final int CONTEXT_ARG = 1; |
| 709 |
final int SCOPE_ARG = 2; |
| 710 |
cfw.startMethod(getFunctionInitMethodName(ofn), |
| 711 |
FUNCTION_INIT_SIGNATURE, |
| 712 |
(short)(ACC_PRIVATE | ACC_FINAL)); |
| 713 |
|
| 714 |
|
| 715 |
cfw.addLoadThis(); |
| 716 |
cfw.addALoad(CONTEXT_ARG); |
| 717 |
cfw.addALoad(SCOPE_ARG); |
| 718 |
cfw.addInvoke(ByteCode.INVOKEVIRTUAL, |
| 719 |
"org/mozilla/javascript/NativeFunction", |
| 720 |
"initScriptFunction", |
| 721 |
"(Lorg/mozilla/javascript/Context;" |
| 722 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 723 |
+")V"); |
| 724 |
|
| 725 |
|
| 726 |
if (ofn.fnode.getRegexpCount() != 0) { |
| 727 |
cfw.addALoad(CONTEXT_ARG); |
| 728 |
cfw.addInvoke(ByteCode.INVOKESTATIC, mainClassName, |
| 729 |
REGEXP_INIT_METHOD_NAME, REGEXP_INIT_METHOD_SIGNATURE); |
| 730 |
} |
| 731 |
|
| 732 |
cfw.add(ByteCode.RETURN); |
| 733 |
|
| 734 |
cfw.stopMethod((short)3); |
| 735 |
} |
| 736 |
|
| 737 |
private void generateNativeFunctionOverrides(ClassFileWriter cfw, |
| 738 |
String encodedSource) |
| 739 |
{ |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
cfw.startMethod("getLanguageVersion", "()I", ACC_PUBLIC); |
| 744 |
|
| 745 |
cfw.addPush(compilerEnv.getLanguageVersion()); |
| 746 |
cfw.add(ByteCode.IRETURN); |
| 747 |
|
| 748 |
|
| 749 |
cfw.stopMethod((short)1); |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
final int Do_getFunctionName = 0; |
| 755 |
final int Do_getParamCount = 1; |
| 756 |
final int Do_getParamAndVarCount = 2; |
| 757 |
final int Do_getParamOrVarName = 3; |
| 758 |
final int Do_getEncodedSource = 4; |
| 759 |
final int Do_getParamOrVarConst = 5; |
| 760 |
final int SWITCH_COUNT = 6; |
| 761 |
|
| 762 |
for (int methodIndex = 0; methodIndex != SWITCH_COUNT; ++methodIndex) { |
| 763 |
if (methodIndex == Do_getEncodedSource && encodedSource == null) { |
| 764 |
continue; |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
short methodLocals; |
| 773 |
switch (methodIndex) { |
| 774 |
case Do_getFunctionName: |
| 775 |
methodLocals = 1; |
| 776 |
cfw.startMethod("getFunctionName", "()Ljava/lang/String;", |
| 777 |
ACC_PUBLIC); |
| 778 |
break; |
| 779 |
case Do_getParamCount: |
| 780 |
methodLocals = 1; |
| 781 |
cfw.startMethod("getParamCount", "()I", |
| 782 |
ACC_PUBLIC); |
| 783 |
break; |
| 784 |
case Do_getParamAndVarCount: |
| 785 |
methodLocals = 1; |
| 786 |
cfw.startMethod("getParamAndVarCount", "()I", |
| 787 |
ACC_PUBLIC); |
| 788 |
break; |
| 789 |
case Do_getParamOrVarName: |
| 790 |
methodLocals = 1 + 1; |
| 791 |
cfw.startMethod("getParamOrVarName", "(I)Ljava/lang/String;", |
| 792 |
ACC_PUBLIC); |
| 793 |
break; |
| 794 |
case Do_getParamOrVarConst: |
| 795 |
methodLocals = 1 + 1 + 1; |
| 796 |
cfw.startMethod("getParamOrVarConst", "(I)Z", |
| 797 |
ACC_PUBLIC); |
| 798 |
break; |
| 799 |
case Do_getEncodedSource: |
| 800 |
methodLocals = 1; |
| 801 |
cfw.startMethod("getEncodedSource", "()Ljava/lang/String;", |
| 802 |
ACC_PUBLIC); |
| 803 |
cfw.addPush(encodedSource); |
| 804 |
break; |
| 805 |
default: |
| 806 |
throw Kit.codeBug(); |
| 807 |
} |
| 808 |
|
| 809 |
int count = scriptOrFnNodes.length; |
| 810 |
|
| 811 |
int switchStart = 0; |
| 812 |
int switchStackTop = 0; |
| 813 |
if (count > 1) { |
| 814 |
|
| 815 |
|
| 816 |
cfw.addLoadThis(); |
| 817 |
cfw.add(ByteCode.GETFIELD, cfw.getClassName(), |
| 818 |
ID_FIELD_NAME, "I"); |
| 819 |
|
| 820 |
|
| 821 |
switchStart = cfw.addTableSwitch(1, count - 1); |
| 822 |
} |
| 823 |
|
| 824 |
for (int i = 0; i != count; ++i) { |
| 825 |
ScriptNode n = scriptOrFnNodes[i]; |
| 826 |
if (i == 0) { |
| 827 |
if (count > 1) { |
| 828 |
cfw.markTableSwitchDefault(switchStart); |
| 829 |
switchStackTop = cfw.getStackTop(); |
| 830 |
} |
| 831 |
} else { |
| 832 |
cfw.markTableSwitchCase(switchStart, i - 1, |
| 833 |
switchStackTop); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
switch (methodIndex) { |
| 838 |
case Do_getFunctionName: |
| 839 |
|
| 840 |
if (n.getType() == Token.SCRIPT) { |
| 841 |
cfw.addPush(""); |
| 842 |
} else { |
| 843 |
String name = ((FunctionNode)n).getName(); |
| 844 |
cfw.addPush(name); |
| 845 |
} |
| 846 |
cfw.add(ByteCode.ARETURN); |
| 847 |
break; |
| 848 |
|
| 849 |
case Do_getParamCount: |
| 850 |
|
| 851 |
cfw.addPush(n.getParamCount()); |
| 852 |
cfw.add(ByteCode.IRETURN); |
| 853 |
break; |
| 854 |
|
| 855 |
case Do_getParamAndVarCount: |
| 856 |
|
| 857 |
cfw.addPush(n.getParamAndVarCount()); |
| 858 |
cfw.add(ByteCode.IRETURN); |
| 859 |
break; |
| 860 |
|
| 861 |
case Do_getParamOrVarName: |
| 862 |
|
| 863 |
|
| 864 |
int paramAndVarCount = n.getParamAndVarCount(); |
| 865 |
if (paramAndVarCount == 0) { |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
cfw.add(ByteCode.ACONST_NULL); |
| 870 |
cfw.add(ByteCode.ARETURN); |
| 871 |
} else if (paramAndVarCount == 1) { |
| 872 |
|
| 873 |
|
| 874 |
cfw.addPush(n.getParamOrVarName(0)); |
| 875 |
cfw.add(ByteCode.ARETURN); |
| 876 |
} else { |
| 877 |
|
| 878 |
cfw.addILoad(1); |
| 879 |
|
| 880 |
|
| 881 |
int paramSwitchStart = cfw.addTableSwitch( |
| 882 |
1, paramAndVarCount - 1); |
| 883 |
for (int j = 0; j != paramAndVarCount; ++j) { |
| 884 |
if (cfw.getStackTop() != 0) Kit.codeBug(); |
| 885 |
String s = n.getParamOrVarName(j); |
| 886 |
if (j == 0) { |
| 887 |
cfw.markTableSwitchDefault(paramSwitchStart); |
| 888 |
} else { |
| 889 |
cfw.markTableSwitchCase(paramSwitchStart, j - 1, |
| 890 |
0); |
| 891 |
} |
| 892 |
cfw.addPush(s); |
| 893 |
cfw.add(ByteCode.ARETURN); |
| 894 |
} |
| 895 |
} |
| 896 |
break; |
| 897 |
|
| 898 |
case Do_getParamOrVarConst: |
| 899 |
|
| 900 |
|
| 901 |
paramAndVarCount = n.getParamAndVarCount(); |
| 902 |
boolean [] constness = n.getParamAndVarConst(); |
| 903 |
if (paramAndVarCount == 0) { |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
cfw.add(ByteCode.ICONST_0); |
| 908 |
cfw.add(ByteCode.IRETURN); |
| 909 |
} else if (paramAndVarCount == 1) { |
| 910 |
|
| 911 |
|
| 912 |
cfw.addPush(constness[0]); |
| 913 |
cfw.add(ByteCode.IRETURN); |
| 914 |
} else { |
| 915 |
|
| 916 |
cfw.addILoad(1); |
| 917 |
|
| 918 |
|
| 919 |
int paramSwitchStart = cfw.addTableSwitch( |
| 920 |
1, paramAndVarCount - 1); |
| 921 |
for (int j = 0; j != paramAndVarCount; ++j) { |
| 922 |
if (cfw.getStackTop() != 0) Kit.codeBug(); |
| 923 |
if (j == 0) { |
| 924 |
cfw.markTableSwitchDefault(paramSwitchStart); |
| 925 |
} else { |
| 926 |
cfw.markTableSwitchCase(paramSwitchStart, j - 1, |
| 927 |
0); |
| 928 |
} |
| 929 |
cfw.addPush(constness[j]); |
| 930 |
cfw.add(ByteCode.IRETURN); |
| 931 |
} |
| 932 |
} |
| 933 |
break; |
| 934 |
|
| 935 |
case Do_getEncodedSource: |
| 936 |
|
| 937 |
|
| 938 |
cfw.addPush(n.getEncodedSourceStart()); |
| 939 |
cfw.addPush(n.getEncodedSourceEnd()); |
| 940 |
cfw.addInvoke(ByteCode.INVOKEVIRTUAL, |
| 941 |
"java/lang/String", |
| 942 |
"substring", |
| 943 |
"(II)Ljava/lang/String;"); |
| 944 |
cfw.add(ByteCode.ARETURN); |
| 945 |
break; |
| 946 |
|
| 947 |
default: |
| 948 |
throw Kit.codeBug(); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
cfw.stopMethod(methodLocals); |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
private void emitRegExpInit(ClassFileWriter cfw) |
| 957 |
{ |
| 958 |
|
| 959 |
|
| 960 |
int totalRegCount = 0; |
| 961 |
for (int i = 0; i != scriptOrFnNodes.length; ++i) { |
| 962 |
totalRegCount += scriptOrFnNodes[i].getRegexpCount(); |
| 963 |
} |
| 964 |
if (totalRegCount == 0) { |
| 965 |
return; |
| 966 |
} |
| 967 |
|
| 968 |
cfw.startMethod(REGEXP_INIT_METHOD_NAME, REGEXP_INIT_METHOD_SIGNATURE, |
| 969 |
(short)(ACC_STATIC | ACC_PRIVATE)); |
| 970 |
cfw.addField("_reInitDone", "Z", |
| 971 |
(short)(ACC_STATIC | ACC_PRIVATE | ACC_VOLATILE)); |
| 972 |
cfw.add(ByteCode.GETSTATIC, mainClassName, "_reInitDone", "Z"); |
| 973 |
int doInit = cfw.acquireLabel(); |
| 974 |
cfw.add(ByteCode.IFEQ, doInit); |
| 975 |
cfw.add(ByteCode.RETURN); |
| 976 |
cfw.markLabel(doInit); |
| 977 |
|
| 978 |
|
| 979 |
cfw.addALoad(0); |
| 980 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 981 |
"org/mozilla/javascript/ScriptRuntime", |
| 982 |
"checkRegExpProxy", |
| 983 |
"(Lorg/mozilla/javascript/Context;" |
| 984 |
+")Lorg/mozilla/javascript/RegExpProxy;"); |
| 985 |
cfw.addAStore(1); |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
for (int i = 0; i != scriptOrFnNodes.length; ++i) { |
| 990 |
ScriptNode n = scriptOrFnNodes[i]; |
| 991 |
int regCount = n.getRegexpCount(); |
| 992 |
for (int j = 0; j != regCount; ++j) { |
| 993 |
String reFieldName = getCompiledRegexpName(n, j); |
| 994 |
String reFieldType = "Ljava/lang/Object;"; |
| 995 |
String reString = n.getRegexpString(j); |
| 996 |
String reFlags = n.getRegexpFlags(j); |
| 997 |
cfw.addField(reFieldName, reFieldType, |
| 998 |
(short)(ACC_STATIC | ACC_PRIVATE)); |
| 999 |
cfw.addALoad(1); |
| 1000 |
cfw.addALoad(0); |
| 1001 |
cfw.addPush(reString); |
| 1002 |
if (reFlags == null) { |
| 1003 |
cfw.add(ByteCode.ACONST_NULL); |
| 1004 |
} else { |
| 1005 |
cfw.addPush(reFlags); |
| 1006 |
} |
| 1007 |
cfw.addInvoke(ByteCode.INVOKEINTERFACE, |
| 1008 |
"org/mozilla/javascript/RegExpProxy", |
| 1009 |
"compileRegExp", |
| 1010 |
"(Lorg/mozilla/javascript/Context;" |
| 1011 |
+"Ljava/lang/String;Ljava/lang/String;" |
| 1012 |
+")Ljava/lang/Object;"); |
| 1013 |
cfw.add(ByteCode.PUTSTATIC, mainClassName, |
| 1014 |
reFieldName, reFieldType); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
cfw.addPush(1); |
| 1019 |
cfw.add(ByteCode.PUTSTATIC, mainClassName, "_reInitDone", "Z"); |
| 1020 |
cfw.add(ByteCode.RETURN); |
| 1021 |
cfw.stopMethod((short)2); |
| 1022 |
} |
| 1023 |
|
| 1024 |
private void emitConstantDudeInitializers(ClassFileWriter cfw) |
| 1025 |
{ |
| 1026 |
int N = itsConstantListSize; |
| 1027 |
if (N == 0) |
| 1028 |
return; |
| 1029 |
|
| 1030 |
cfw.startMethod("<clinit>", "()V", (short)(ACC_STATIC | ACC_FINAL)); |
| 1031 |
|
| 1032 |
double[] array = itsConstantList; |
| 1033 |
for (int i = 0; i != N; ++i) { |
| 1034 |
double num = array[i]; |
| 1035 |
String constantName = "_k" + i; |
| 1036 |
String constantType = getStaticConstantWrapperType(num); |
| 1037 |
cfw.addField(constantName, constantType, |
| 1038 |
(short)(ACC_STATIC | ACC_PRIVATE)); |
| 1039 |
int inum = (int)num; |
| 1040 |
if (inum == num) { |
| 1041 |
cfw.addPush(inum); |
| 1042 |
cfw.addInvoke(ByteCode.INVOKESTATIC, "java/lang/Integer", |
| 1043 |
"valueOf", "(I)Ljava/lang/Integer;"); |
| 1044 |
} else { |
| 1045 |
cfw.addPush(num); |
| 1046 |
addDoubleWrap(cfw); |
| 1047 |
} |
| 1048 |
cfw.add(ByteCode.PUTSTATIC, mainClassName, |
| 1049 |
constantName, constantType); |
| 1050 |
} |
| 1051 |
|
| 1052 |
cfw.add(ByteCode.RETURN); |
| 1053 |
cfw.stopMethod((short)0); |
| 1054 |
} |
| 1055 |
|
| 1056 |
void pushNumberAsObject(ClassFileWriter cfw, double num) |
| 1057 |
{ |
| 1058 |
if (num == 0.0) { |
| 1059 |
if (1 / num > 0) { |
| 1060 |
|
| 1061 |
cfw.add(ByteCode.GETSTATIC, |
| 1062 |
"org/mozilla/javascript/optimizer/OptRuntime", |
| 1063 |
"zeroObj", "Ljava/lang/Double;"); |
| 1064 |
} else { |
| 1065 |
cfw.addPush(num); |
| 1066 |
addDoubleWrap(cfw); |
| 1067 |
} |
| 1068 |
|
| 1069 |
} else if (num == 1.0) { |
| 1070 |
cfw.add(ByteCode.GETSTATIC, |
| 1071 |
"org/mozilla/javascript/optimizer/OptRuntime", |
| 1072 |
"oneObj", "Ljava/lang/Double;"); |
| 1073 |
return; |
| 1074 |
|
| 1075 |
} else if (num == -1.0) { |
| 1076 |
cfw.add(ByteCode.GETSTATIC, |
| 1077 |
"org/mozilla/javascript/optimizer/OptRuntime", |
| 1078 |
"minusOneObj", "Ljava/lang/Double;"); |
| 1079 |
|
| 1080 |
} else if (num != num) { |
| 1081 |
cfw.add(ByteCode.GETSTATIC, |
| 1082 |
"org/mozilla/javascript/ScriptRuntime", |
| 1083 |
"NaNobj", "Ljava/lang/Double;"); |
| 1084 |
|
| 1085 |
} else if (itsConstantListSize >= 2000) { |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
cfw.addPush(num); |
| 1091 |
addDoubleWrap(cfw); |
| 1092 |
|
| 1093 |
} else { |
| 1094 |
int N = itsConstantListSize; |
| 1095 |
int index = 0; |
| 1096 |
if (N == 0) { |
| 1097 |
itsConstantList = new double[64]; |
| 1098 |
} else { |
| 1099 |
double[] array = itsConstantList; |
| 1100 |
while (index != N && array[index] != num) { |
| 1101 |
++index; |
| 1102 |
} |
| 1103 |
if (N == array.length) { |
| 1104 |
array = new double[N * 2]; |
| 1105 |
System.arraycopy(itsConstantList, 0, array, 0, N); |
| 1106 |
itsConstantList = array; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
if (index == N) { |
| 1110 |
itsConstantList[N] = num; |
| 1111 |
itsConstantListSize = N + 1; |
| 1112 |
} |
| 1113 |
String constantName = "_k" + index; |
| 1114 |
String constantType = getStaticConstantWrapperType(num); |
| 1115 |
cfw.add(ByteCode.GETSTATIC, mainClassName, |
| 1116 |
constantName, constantType); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
private static void addDoubleWrap(ClassFileWriter cfw) |
| 1121 |
{ |
| 1122 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 1123 |
"org/mozilla/javascript/optimizer/OptRuntime", |
| 1124 |
"wrapDouble", "(D)Ljava/lang/Double;"); |
| 1125 |
} |
| 1126 |
|
| 1127 |
private static String getStaticConstantWrapperType(double num) |
| 1128 |
{ |
| 1129 |
int inum = (int)num; |
| 1130 |
if (inum == num) { |
| 1131 |
return "Ljava/lang/Integer;"; |
| 1132 |
} else { |
| 1133 |
return "Ljava/lang/Double;"; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
static void pushUndefined(ClassFileWriter cfw) |
| 1137 |
{ |
| 1138 |
cfw.add(ByteCode.GETSTATIC, "org/mozilla/javascript/Undefined", |
| 1139 |
"instance", "Ljava/lang/Object;"); |
| 1140 |
} |
| 1141 |
|
| 1142 |
int getIndex(ScriptNode n) |
| 1143 |
{ |
| 1144 |
return scriptOrFnIndexes.getExisting(n); |
| 1145 |
} |
| 1146 |
|
| 1147 |
String getDirectCtorName(ScriptNode n) |
| 1148 |
{ |
| 1149 |
return "_n" + getIndex(n); |
| 1150 |
} |
| 1151 |
|
| 1152 |
String getBodyMethodName(ScriptNode n) |
| 1153 |
{ |
| 1154 |
return "_c_" + cleanName(n) + "_" + getIndex(n); |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
String cleanName(final ScriptNode n) |
| 1161 |
{ |
| 1162 |
String result = ""; |
| 1163 |
if (n instanceof FunctionNode) { |
| 1164 |
Name name = ((FunctionNode) n).getFunctionName(); |
| 1165 |
if (name == null) { |
| 1166 |
result = "anonymous"; |
| 1167 |
} else { |
| 1168 |
result = name.getIdentifier(); |
| 1169 |
} |
| 1170 |
} else { |
| 1171 |
result = "script"; |
| 1172 |
} |
| 1173 |
return result; |
| 1174 |
} |
| 1175 |
|
| 1176 |
String getBodyMethodSignature(ScriptNode n) |
| 1177 |
{ |
| 1178 |
StringBuilder sb = new StringBuilder(); |
| 1179 |
sb.append('('); |
| 1180 |
sb.append(mainClassSignature); |
| 1181 |
sb.append("Lorg/mozilla/javascript/Context;" |
| 1182 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1183 |
+"Lorg/mozilla/javascript/Scriptable;"); |
| 1184 |
if (n.getType() == Token.FUNCTION) { |
| 1185 |
OptFunctionNode ofn = OptFunctionNode.get(n); |
| 1186 |
if (ofn.isTargetOfDirectCall()) { |
| 1187 |
int pCount = ofn.fnode.getParamCount(); |
| 1188 |
for (int i = 0; i != pCount; i++) { |
| 1189 |
sb.append("Ljava/lang/Object;D"); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
} |
| 1193 |
sb.append("[Ljava/lang/Object;)Ljava/lang/Object;"); |
| 1194 |
return sb.toString(); |
| 1195 |
} |
| 1196 |
|
| 1197 |
String getFunctionInitMethodName(OptFunctionNode ofn) |
| 1198 |
{ |
| 1199 |
return "_i"+getIndex(ofn.fnode); |
| 1200 |
} |
| 1201 |
|
| 1202 |
String getCompiledRegexpName(ScriptNode n, int regexpIndex) |
| 1203 |
{ |
| 1204 |
return "_re"+getIndex(n)+"_"+regexpIndex; |
| 1205 |
} |
| 1206 |
|
| 1207 |
static RuntimeException badTree() |
| 1208 |
{ |
| 1209 |
throw new RuntimeException("Bad tree in codegen"); |
| 1210 |
} |
| 1211 |
|
| 1212 |
public void setMainMethodClass(String className) |
| 1213 |
{ |
| 1214 |
mainMethodClass = className; |
| 1215 |
} |
| 1216 |
|
| 1217 |
static final String DEFAULT_MAIN_METHOD_CLASS |
| 1218 |
= "org.mozilla.javascript.optimizer.OptRuntime"; |
| 1219 |
|
| 1220 |
private static final String SUPER_CLASS_NAME |
| 1221 |
= "org.mozilla.javascript.NativeFunction"; |
| 1222 |
|
| 1223 |
static final String ID_FIELD_NAME = "_id"; |
| 1224 |
|
| 1225 |
static final String REGEXP_INIT_METHOD_NAME = "_reInit"; |
| 1226 |
static final String REGEXP_INIT_METHOD_SIGNATURE |
| 1227 |
= "(Lorg/mozilla/javascript/Context;)V"; |
| 1228 |
|
| 1229 |
static final String FUNCTION_INIT_SIGNATURE |
| 1230 |
= "(Lorg/mozilla/javascript/Context;" |
| 1231 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1232 |
+")V"; |
| 1233 |
|
| 1234 |
static final String FUNCTION_CONSTRUCTOR_SIGNATURE |
| 1235 |
= "(Lorg/mozilla/javascript/Scriptable;" |
| 1236 |
+"Lorg/mozilla/javascript/Context;I)V"; |
| 1237 |
|
| 1238 |
private static final Object globalLock = new Object(); |
| 1239 |
private static int globalSerialClassCounter; |
| 1240 |
|
| 1241 |
private CompilerEnvirons compilerEnv; |
| 1242 |
|
| 1243 |
private ObjArray directCallTargets; |
| 1244 |
ScriptNode[] scriptOrFnNodes; |
| 1245 |
private ObjToIntMap scriptOrFnIndexes; |
| 1246 |
|
| 1247 |
private String mainMethodClass = DEFAULT_MAIN_METHOD_CLASS; |
| 1248 |
|
| 1249 |
String mainClassName; |
| 1250 |
String mainClassSignature; |
| 1251 |
|
| 1252 |
private double[] itsConstantList; |
| 1253 |
private int itsConstantListSize; |
| 1254 |
} |
| 1255 |
|
| 1256 |
|
| 1257 |
class BodyCodegen |
| 1258 |
{ |
| 1259 |
void generateBodyCode() |
| 1260 |
{ |
| 1261 |
isGenerator = Codegen.isGenerator(scriptOrFn); |
| 1262 |
|
| 1263 |
|
| 1264 |
initBodyGeneration(); |
| 1265 |
|
| 1266 |
if (isGenerator) { |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
String type = "(" + |
| 1271 |
codegen.mainClassSignature + |
| 1272 |
"Lorg/mozilla/javascript/Context;" + |
| 1273 |
"Lorg/mozilla/javascript/Scriptable;" + |
| 1274 |
"Ljava/lang/Object;" + |
| 1275 |
"Ljava/lang/Object;I)Ljava/lang/Object;"; |
| 1276 |
cfw.startMethod(codegen.getBodyMethodName(scriptOrFn) + "_gen", |
| 1277 |
type, |
| 1278 |
(short)(ACC_STATIC | ACC_PRIVATE)); |
| 1279 |
} else { |
| 1280 |
cfw.startMethod(codegen.getBodyMethodName(scriptOrFn), |
| 1281 |
codegen.getBodyMethodSignature(scriptOrFn), |
| 1282 |
(short)(ACC_STATIC | ACC_PRIVATE)); |
| 1283 |
} |
| 1284 |
|
| 1285 |
generatePrologue(); |
| 1286 |
Node treeTop; |
| 1287 |
if (fnCurrent != null) { |
| 1288 |
treeTop = scriptOrFn.getLastChild(); |
| 1289 |
} else { |
| 1290 |
treeTop = scriptOrFn; |
| 1291 |
} |
| 1292 |
generateStatement(treeTop); |
| 1293 |
generateEpilogue(); |
| 1294 |
|
| 1295 |
cfw.stopMethod((short)(localsMax + 1)); |
| 1296 |
|
| 1297 |
if (isGenerator) { |
| 1298 |
|
| 1299 |
|
| 1300 |
generateGenerator(); |
| 1301 |
} |
| 1302 |
|
| 1303 |
if (literals != null) { |
| 1304 |
|
| 1305 |
for (int i = 0; i < literals.size(); i++) { |
| 1306 |
Node node = literals.get(i); |
| 1307 |
int type = node.getType(); |
| 1308 |
switch (type) { |
| 1309 |
case Token.OBJECTLIT: |
| 1310 |
generateObjectLiteralFactory(node, i + 1); |
| 1311 |
break; |
| 1312 |
case Token.ARRAYLIT: |
| 1313 |
generateArrayLiteralFactory(node, i + 1); |
| 1314 |
break; |
| 1315 |
default: |
| 1316 |
Kit.codeBug(Token.typeToName(type)); |
| 1317 |
} |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
private void generateGenerator() |
| 1326 |
{ |
| 1327 |
cfw.startMethod(codegen.getBodyMethodName(scriptOrFn), |
| 1328 |
codegen.getBodyMethodSignature(scriptOrFn), |
| 1329 |
(short)(ACC_STATIC | ACC_PRIVATE)); |
| 1330 |
|
| 1331 |
initBodyGeneration(); |
| 1332 |
argsLocal = firstFreeLocal++; |
| 1333 |
localsMax = firstFreeLocal; |
| 1334 |
|
| 1335 |
|
| 1336 |
if (fnCurrent != null) |
| 1337 |
{ |
| 1338 |
|
| 1339 |
|
| 1340 |
cfw.addALoad(funObjLocal); |
| 1341 |
cfw.addInvoke(ByteCode.INVOKEINTERFACE, |
| 1342 |
"org/mozilla/javascript/Scriptable", |
| 1343 |
"getParentScope", |
| 1344 |
"()Lorg/mozilla/javascript/Scriptable;"); |
| 1345 |
cfw.addAStore(variableObjectLocal); |
| 1346 |
} |
| 1347 |
|
| 1348 |
|
| 1349 |
cfw.addALoad(funObjLocal); |
| 1350 |
cfw.addALoad(variableObjectLocal); |
| 1351 |
cfw.addALoad(argsLocal); |
| 1352 |
cfw.addPush(scriptOrFn.isInStrictMode()); |
| 1353 |
addScriptRuntimeInvoke("createFunctionActivation", |
| 1354 |
"(Lorg/mozilla/javascript/NativeFunction;" |
| 1355 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1356 |
+"[Ljava/lang/Object;" |
| 1357 |
+"Z" |
| 1358 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 1359 |
cfw.addAStore(variableObjectLocal); |
| 1360 |
|
| 1361 |
|
| 1362 |
cfw.add(ByteCode.NEW, codegen.mainClassName); |
| 1363 |
|
| 1364 |
cfw.add(ByteCode.DUP); |
| 1365 |
cfw.addALoad(variableObjectLocal); |
| 1366 |
cfw.addALoad(contextLocal); |
| 1367 |
cfw.addPush(scriptOrFnIndex); |
| 1368 |
cfw.addInvoke(ByteCode.INVOKESPECIAL, codegen.mainClassName, |
| 1369 |
"<init>", Codegen.FUNCTION_CONSTRUCTOR_SIGNATURE); |
| 1370 |
|
| 1371 |
generateNestedFunctionInits(); |
| 1372 |
|
| 1373 |
|
| 1374 |
cfw.addALoad(variableObjectLocal); |
| 1375 |
cfw.addALoad(thisObjLocal); |
| 1376 |
cfw.addLoadConstant(maxLocals); |
| 1377 |
cfw.addLoadConstant(maxStack); |
| 1378 |
addOptRuntimeInvoke("createNativeGenerator", |
| 1379 |
"(Lorg/mozilla/javascript/NativeFunction;" |
| 1380 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1381 |
+"Lorg/mozilla/javascript/Scriptable;II" |
| 1382 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 1383 |
|
| 1384 |
cfw.add(ByteCode.ARETURN); |
| 1385 |
cfw.stopMethod((short)(localsMax + 1)); |
| 1386 |
} |
| 1387 |
|
| 1388 |
private void generateNestedFunctionInits() |
| 1389 |
{ |
| 1390 |
int functionCount = scriptOrFn.getFunctionCount(); |
| 1391 |
for (int i = 0; i != functionCount; i++) { |
| 1392 |
OptFunctionNode ofn = OptFunctionNode.get(scriptOrFn, i); |
| 1393 |
if (ofn.fnode.getFunctionType() |
| 1394 |
== FunctionNode.FUNCTION_STATEMENT) |
| 1395 |
{ |
| 1396 |
visitFunction(ofn, FunctionNode.FUNCTION_STATEMENT); |
| 1397 |
} |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
private void initBodyGeneration() |
| 1402 |
{ |
| 1403 |
varRegisters = null; |
| 1404 |
if (scriptOrFn.getType() == Token.FUNCTION) { |
| 1405 |
fnCurrent = OptFunctionNode.get(scriptOrFn); |
| 1406 |
hasVarsInRegs = !fnCurrent.fnode.requiresActivation(); |
| 1407 |
if (hasVarsInRegs) { |
| 1408 |
int n = fnCurrent.fnode.getParamAndVarCount(); |
| 1409 |
if (n != 0) { |
| 1410 |
varRegisters = new short[n]; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
inDirectCallFunction = fnCurrent.isTargetOfDirectCall(); |
| 1414 |
if (inDirectCallFunction && !hasVarsInRegs) Codegen.badTree(); |
| 1415 |
} else { |
| 1416 |
fnCurrent = null; |
| 1417 |
hasVarsInRegs = false; |
| 1418 |
inDirectCallFunction = false; |
| 1419 |
} |
| 1420 |
|
| 1421 |
locals = new int[MAX_LOCALS]; |
| 1422 |
|
| 1423 |
funObjLocal = 0; |
| 1424 |
contextLocal = 1; |
| 1425 |
variableObjectLocal = 2; |
| 1426 |
thisObjLocal = 3; |
| 1427 |
localsMax = (short) 4; |
| 1428 |
firstFreeLocal = 4; |
| 1429 |
|
| 1430 |
popvLocal = -1; |
| 1431 |
argsLocal = -1; |
| 1432 |
itsZeroArgArray = -1; |
| 1433 |
itsOneArgArray = -1; |
| 1434 |
epilogueLabel = -1; |
| 1435 |
enterAreaStartLabel = -1; |
| 1436 |
generatorStateLocal = -1; |
| 1437 |
} |
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
private void generatePrologue() |
| 1443 |
{ |
| 1444 |
if (inDirectCallFunction) { |
| 1445 |
int directParameterCount = scriptOrFn.getParamCount(); |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
if (firstFreeLocal != 4) Kit.codeBug(); |
| 1451 |
for (int i = 0; i != directParameterCount; ++i) { |
| 1452 |
varRegisters[i] = firstFreeLocal; |
| 1453 |
|
| 1454 |
firstFreeLocal += 3; |
| 1455 |
} |
| 1456 |
if (!fnCurrent.getParameterNumberContext()) { |
| 1457 |
|
| 1458 |
itsForcedObjectParameters = true; |
| 1459 |
for (int i = 0; i != directParameterCount; ++i) { |
| 1460 |
short reg = varRegisters[i]; |
| 1461 |
cfw.addALoad(reg); |
| 1462 |
cfw.add(ByteCode.GETSTATIC, |
| 1463 |
"java/lang/Void", |
| 1464 |
"TYPE", |
| 1465 |
"Ljava/lang/Class;"); |
| 1466 |
int isObjectLabel = cfw.acquireLabel(); |
| 1467 |
cfw.add(ByteCode.IF_ACMPNE, isObjectLabel); |
| 1468 |
cfw.addDLoad(reg + 1); |
| 1469 |
addDoubleWrap(); |
| 1470 |
cfw.addAStore(reg); |
| 1471 |
cfw.markLabel(isObjectLabel); |
| 1472 |
} |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
if (fnCurrent != null) { |
| 1477 |
|
| 1478 |
cfw.addALoad(funObjLocal); |
| 1479 |
cfw.addInvoke(ByteCode.INVOKEINTERFACE, |
| 1480 |
"org/mozilla/javascript/Scriptable", |
| 1481 |
"getParentScope", |
| 1482 |
"()Lorg/mozilla/javascript/Scriptable;"); |
| 1483 |
cfw.addAStore(variableObjectLocal); |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
argsLocal = firstFreeLocal++; |
| 1488 |
localsMax = firstFreeLocal; |
| 1489 |
|
| 1490 |
|
| 1491 |
if (isGenerator) { |
| 1492 |
|
| 1493 |
|
| 1494 |
operationLocal = firstFreeLocal++; |
| 1495 |
localsMax = firstFreeLocal; |
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
cfw.addALoad(thisObjLocal); |
| 1502 |
generatorStateLocal = firstFreeLocal++; |
| 1503 |
localsMax = firstFreeLocal; |
| 1504 |
cfw.add(ByteCode.CHECKCAST, OptRuntime.GeneratorState.CLASS_NAME); |
| 1505 |
cfw.add(ByteCode.DUP); |
| 1506 |
cfw.addAStore(generatorStateLocal); |
| 1507 |
cfw.add(ByteCode.GETFIELD, |
| 1508 |
OptRuntime.GeneratorState.CLASS_NAME, |
| 1509 |
OptRuntime.GeneratorState.thisObj_NAME, |
| 1510 |
OptRuntime.GeneratorState.thisObj_TYPE); |
| 1511 |
cfw.addAStore(thisObjLocal); |
| 1512 |
|
| 1513 |
if (epilogueLabel == -1) { |
| 1514 |
epilogueLabel = cfw.acquireLabel(); |
| 1515 |
} |
| 1516 |
|
| 1517 |
List<Node> targets = ((FunctionNode)scriptOrFn).getResumptionPoints(); |
| 1518 |
if (targets != null) { |
| 1519 |
|
| 1520 |
generateGetGeneratorResumptionPoint(); |
| 1521 |
|
| 1522 |
|
| 1523 |
generatorSwitch = cfw.addTableSwitch(0, |
| 1524 |
targets.size() + GENERATOR_START); |
| 1525 |
generateCheckForThrowOrClose(-1, false, GENERATOR_START); |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
if (fnCurrent == null && scriptOrFn.getRegexpCount() != 0) { |
| 1532 |
cfw.addALoad(contextLocal); |
| 1533 |
cfw.addInvoke(ByteCode.INVOKESTATIC, codegen.mainClassName, |
| 1534 |
Codegen.REGEXP_INIT_METHOD_NAME, |
| 1535 |
Codegen.REGEXP_INIT_METHOD_SIGNATURE); |
| 1536 |
} |
| 1537 |
|
| 1538 |
if (compilerEnv.isGenerateObserverCount()) |
| 1539 |
saveCurrentCodeOffset(); |
| 1540 |
|
| 1541 |
if (hasVarsInRegs) { |
| 1542 |
|
| 1543 |
int parmCount = scriptOrFn.getParamCount(); |
| 1544 |
if (parmCount > 0 && !inDirectCallFunction) { |
| 1545 |
|
| 1546 |
|
| 1547 |
cfw.addALoad(argsLocal); |
| 1548 |
cfw.add(ByteCode.ARRAYLENGTH); |
| 1549 |
cfw.addPush(parmCount); |
| 1550 |
int label = cfw.acquireLabel(); |
| 1551 |
cfw.add(ByteCode.IF_ICMPGE, label); |
| 1552 |
cfw.addALoad(argsLocal); |
| 1553 |
cfw.addPush(parmCount); |
| 1554 |
addScriptRuntimeInvoke("padArguments", |
| 1555 |
"([Ljava/lang/Object;I" |
| 1556 |
+")[Ljava/lang/Object;"); |
| 1557 |
cfw.addAStore(argsLocal); |
| 1558 |
cfw.markLabel(label); |
| 1559 |
} |
| 1560 |
|
| 1561 |
int paramCount = fnCurrent.fnode.getParamCount(); |
| 1562 |
int varCount = fnCurrent.fnode.getParamAndVarCount(); |
| 1563 |
boolean [] constDeclarations = fnCurrent.fnode.getParamAndVarConst(); |
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
short firstUndefVar = -1; |
| 1568 |
for (int i = 0; i != varCount; ++i) { |
| 1569 |
short reg = -1; |
| 1570 |
if (i < paramCount) { |
| 1571 |
if (!inDirectCallFunction) { |
| 1572 |
reg = getNewWordLocal(); |
| 1573 |
cfw.addALoad(argsLocal); |
| 1574 |
cfw.addPush(i); |
| 1575 |
cfw.add(ByteCode.AALOAD); |
| 1576 |
cfw.addAStore(reg); |
| 1577 |
} |
| 1578 |
} else if (fnCurrent.isNumberVar(i)) { |
| 1579 |
reg = getNewWordPairLocal(constDeclarations[i]); |
| 1580 |
cfw.addPush(0.0); |
| 1581 |
cfw.addDStore(reg); |
| 1582 |
} else { |
| 1583 |
reg = getNewWordLocal(constDeclarations[i]); |
| 1584 |
if (firstUndefVar == -1) { |
| 1585 |
Codegen.pushUndefined(cfw); |
| 1586 |
firstUndefVar = reg; |
| 1587 |
} else { |
| 1588 |
cfw.addALoad(firstUndefVar); |
| 1589 |
} |
| 1590 |
cfw.addAStore(reg); |
| 1591 |
} |
| 1592 |
if (reg >= 0) { |
| 1593 |
if (constDeclarations[i]) { |
| 1594 |
cfw.addPush(0); |
| 1595 |
cfw.addIStore(reg + (fnCurrent.isNumberVar(i) ? 2 : 1)); |
| 1596 |
} |
| 1597 |
varRegisters[i] = reg; |
| 1598 |
} |
| 1599 |
|
| 1600 |
|
| 1601 |
if (compilerEnv.isGenerateDebugInfo()) { |
| 1602 |
String name = fnCurrent.fnode.getParamOrVarName(i); |
| 1603 |
String type = fnCurrent.isNumberVar(i) |
| 1604 |
? "D" : "Ljava/lang/Object;"; |
| 1605 |
int startPC = cfw.getCurrentCodeOffset(); |
| 1606 |
if (reg < 0) { |
| 1607 |
reg = varRegisters[i]; |
| 1608 |
} |
| 1609 |
cfw.addVariableDescriptor(name, type, startPC, reg); |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
return; |
| 1615 |
} |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
if (isGenerator) |
| 1621 |
return; |
| 1622 |
|
| 1623 |
|
| 1624 |
String debugVariableName; |
| 1625 |
boolean isArrow = false; |
| 1626 |
if (scriptOrFn instanceof FunctionNode) { |
| 1627 |
isArrow = ((FunctionNode)scriptOrFn).getFunctionType() == FunctionNode.ARROW_FUNCTION; |
| 1628 |
} |
| 1629 |
if (fnCurrent != null) { |
| 1630 |
debugVariableName = "activation"; |
| 1631 |
cfw.addALoad(funObjLocal); |
| 1632 |
cfw.addALoad(variableObjectLocal); |
| 1633 |
cfw.addALoad(argsLocal); |
| 1634 |
String methodName = isArrow ? "createArrowFunctionActivation" : "createFunctionActivation"; |
| 1635 |
cfw.addPush(scriptOrFn.isInStrictMode()); |
| 1636 |
addScriptRuntimeInvoke(methodName, |
| 1637 |
"(Lorg/mozilla/javascript/NativeFunction;" |
| 1638 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1639 |
+"[Ljava/lang/Object;" |
| 1640 |
+"Z" |
| 1641 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 1642 |
cfw.addAStore(variableObjectLocal); |
| 1643 |
cfw.addALoad(contextLocal); |
| 1644 |
cfw.addALoad(variableObjectLocal); |
| 1645 |
addScriptRuntimeInvoke("enterActivationFunction", |
| 1646 |
"(Lorg/mozilla/javascript/Context;" |
| 1647 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1648 |
+")V"); |
| 1649 |
} else { |
| 1650 |
debugVariableName = "global"; |
| 1651 |
cfw.addALoad(funObjLocal); |
| 1652 |
cfw.addALoad(thisObjLocal); |
| 1653 |
cfw.addALoad(contextLocal); |
| 1654 |
cfw.addALoad(variableObjectLocal); |
| 1655 |
cfw.addPush(0); |
| 1656 |
addScriptRuntimeInvoke("initScript", |
| 1657 |
"(Lorg/mozilla/javascript/NativeFunction;" |
| 1658 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1659 |
+"Lorg/mozilla/javascript/Context;" |
| 1660 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1661 |
+"Z" |
| 1662 |
+")V"); |
| 1663 |
} |
| 1664 |
|
| 1665 |
enterAreaStartLabel = cfw.acquireLabel(); |
| 1666 |
epilogueLabel = cfw.acquireLabel(); |
| 1667 |
cfw.markLabel(enterAreaStartLabel); |
| 1668 |
|
| 1669 |
generateNestedFunctionInits(); |
| 1670 |
|
| 1671 |
|
| 1672 |
if (compilerEnv.isGenerateDebugInfo()) { |
| 1673 |
cfw.addVariableDescriptor(debugVariableName, |
| 1674 |
"Lorg/mozilla/javascript/Scriptable;", |
| 1675 |
cfw.getCurrentCodeOffset(), variableObjectLocal); |
| 1676 |
} |
| 1677 |
|
| 1678 |
if (fnCurrent == null) { |
| 1679 |
|
| 1680 |
popvLocal = getNewWordLocal(); |
| 1681 |
Codegen.pushUndefined(cfw); |
| 1682 |
cfw.addAStore(popvLocal); |
| 1683 |
|
| 1684 |
int linenum = scriptOrFn.getEndLineno(); |
| 1685 |
if (linenum != -1) |
| 1686 |
cfw.addLineNumberEntry((short)linenum); |
| 1687 |
|
| 1688 |
} else { |
| 1689 |
if (fnCurrent.itsContainsCalls0) { |
| 1690 |
itsZeroArgArray = getNewWordLocal(); |
| 1691 |
cfw.add(ByteCode.GETSTATIC, |
| 1692 |
"org/mozilla/javascript/ScriptRuntime", |
| 1693 |
"emptyArgs", "[Ljava/lang/Object;"); |
| 1694 |
cfw.addAStore(itsZeroArgArray); |
| 1695 |
} |
| 1696 |
if (fnCurrent.itsContainsCalls1) { |
| 1697 |
itsOneArgArray = getNewWordLocal(); |
| 1698 |
cfw.addPush(1); |
| 1699 |
cfw.add(ByteCode.ANEWARRAY, "java/lang/Object"); |
| 1700 |
cfw.addAStore(itsOneArgArray); |
| 1701 |
} |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
private void generateGetGeneratorResumptionPoint() |
| 1706 |
{ |
| 1707 |
cfw.addALoad(generatorStateLocal); |
| 1708 |
cfw.add(ByteCode.GETFIELD, |
| 1709 |
OptRuntime.GeneratorState.CLASS_NAME, |
| 1710 |
OptRuntime.GeneratorState.resumptionPoint_NAME, |
| 1711 |
OptRuntime.GeneratorState.resumptionPoint_TYPE); |
| 1712 |
} |
| 1713 |
|
| 1714 |
private void generateSetGeneratorResumptionPoint(int nextState) |
| 1715 |
{ |
| 1716 |
cfw.addALoad(generatorStateLocal); |
| 1717 |
cfw.addLoadConstant(nextState); |
| 1718 |
cfw.add(ByteCode.PUTFIELD, |
| 1719 |
OptRuntime.GeneratorState.CLASS_NAME, |
| 1720 |
OptRuntime.GeneratorState.resumptionPoint_NAME, |
| 1721 |
OptRuntime.GeneratorState.resumptionPoint_TYPE); |
| 1722 |
} |
| 1723 |
|
| 1724 |
private void generateGetGeneratorStackState() |
| 1725 |
{ |
| 1726 |
cfw.addALoad(generatorStateLocal); |
| 1727 |
addOptRuntimeInvoke("getGeneratorStackState", |
| 1728 |
"(Ljava/lang/Object;)[Ljava/lang/Object;"); |
| 1729 |
} |
| 1730 |
|
| 1731 |
private void generateEpilogue() |
| 1732 |
{ |
| 1733 |
if (compilerEnv.isGenerateObserverCount()) |
| 1734 |
addInstructionCount(); |
| 1735 |
if (isGenerator) { |
| 1736 |
|
| 1737 |
Map<Node,int[]> liveLocals = ((FunctionNode)scriptOrFn).getLiveLocals(); |
| 1738 |
if (liveLocals != null) { |
| 1739 |
List<Node> nodes = ((FunctionNode)scriptOrFn).getResumptionPoints(); |
| 1740 |
for (int i = 0; i < nodes.size(); i++) { |
| 1741 |
Node node = nodes.get(i); |
| 1742 |
int[] live = liveLocals.get(node); |
| 1743 |
if (live != null) { |
| 1744 |
cfw.markTableSwitchCase(generatorSwitch, |
| 1745 |
getNextGeneratorState(node)); |
| 1746 |
generateGetGeneratorLocalsState(); |
| 1747 |
for (int j = 0; j < live.length; j++) { |
| 1748 |
cfw.add(ByteCode.DUP); |
| 1749 |
cfw.addLoadConstant(j); |
| 1750 |
cfw.add(ByteCode.AALOAD); |
| 1751 |
cfw.addAStore(live[j]); |
| 1752 |
} |
| 1753 |
cfw.add(ByteCode.POP); |
| 1754 |
cfw.add(ByteCode.GOTO, getTargetLabel(node)); |
| 1755 |
} |
| 1756 |
} |
| 1757 |
} |
| 1758 |
|
| 1759 |
|
| 1760 |
if (finallys != null) { |
| 1761 |
for (Node n: finallys.keySet()) { |
| 1762 |
if (n.getType() == Token.FINALLY) { |
| 1763 |
FinallyReturnPoint ret = finallys.get(n); |
| 1764 |
|
| 1765 |
cfw.markLabel(ret.tableLabel, (short)1); |
| 1766 |
|
| 1767 |
|
| 1768 |
int startSwitch = cfw.addTableSwitch(0, |
| 1769 |
ret.jsrPoints.size() - 1); |
| 1770 |
int c = 0; |
| 1771 |
cfw.markTableSwitchDefault(startSwitch); |
| 1772 |
for (int i = 0; i < ret.jsrPoints.size(); i++) { |
| 1773 |
|
| 1774 |
cfw.markTableSwitchCase(startSwitch, c); |
| 1775 |
cfw.add(ByteCode.GOTO, |
| 1776 |
ret.jsrPoints.get(i).intValue()); |
| 1777 |
c++; |
| 1778 |
} |
| 1779 |
} |
| 1780 |
} |
| 1781 |
} |
| 1782 |
} |
| 1783 |
|
| 1784 |
if (epilogueLabel != -1) { |
| 1785 |
cfw.markLabel(epilogueLabel); |
| 1786 |
} |
| 1787 |
|
| 1788 |
if (hasVarsInRegs) { |
| 1789 |
cfw.add(ByteCode.ARETURN); |
| 1790 |
return; |
| 1791 |
} else if (isGenerator) { |
| 1792 |
if (((FunctionNode)scriptOrFn).getResumptionPoints() != null) { |
| 1793 |
cfw.markTableSwitchDefault(generatorSwitch); |
| 1794 |
} |
| 1795 |
|
| 1796 |
|
| 1797 |
generateSetGeneratorResumptionPoint(GENERATOR_TERMINATE); |
| 1798 |
|
| 1799 |
|
| 1800 |
cfw.addALoad(variableObjectLocal); |
| 1801 |
addOptRuntimeInvoke("throwStopIteration", |
| 1802 |
"(Ljava/lang/Object;)V"); |
| 1803 |
|
| 1804 |
Codegen.pushUndefined(cfw); |
| 1805 |
cfw.add(ByteCode.ARETURN); |
| 1806 |
|
| 1807 |
} else if (fnCurrent == null) { |
| 1808 |
cfw.addALoad(popvLocal); |
| 1809 |
cfw.add(ByteCode.ARETURN); |
| 1810 |
} else { |
| 1811 |
generateActivationExit(); |
| 1812 |
cfw.add(ByteCode.ARETURN); |
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
int finallyHandler = cfw.acquireLabel(); |
| 1818 |
cfw.markHandler(finallyHandler); |
| 1819 |
short exceptionObject = getNewWordLocal(); |
| 1820 |
cfw.addAStore(exceptionObject); |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
generateActivationExit(); |
| 1825 |
|
| 1826 |
cfw.addALoad(exceptionObject); |
| 1827 |
releaseWordLocal(exceptionObject); |
| 1828 |
|
| 1829 |
cfw.add(ByteCode.ATHROW); |
| 1830 |
|
| 1831 |
|
| 1832 |
cfw.addExceptionHandler(enterAreaStartLabel, epilogueLabel, |
| 1833 |
finallyHandler, null); |
| 1834 |
} |
| 1835 |
} |
| 1836 |
|
| 1837 |
private void generateGetGeneratorLocalsState() { |
| 1838 |
cfw.addALoad(generatorStateLocal); |
| 1839 |
addOptRuntimeInvoke("getGeneratorLocalsState", |
| 1840 |
"(Ljava/lang/Object;)[Ljava/lang/Object;"); |
| 1841 |
} |
| 1842 |
|
| 1843 |
private void generateActivationExit() |
| 1844 |
{ |
| 1845 |
if (fnCurrent == null || hasVarsInRegs) throw Kit.codeBug(); |
| 1846 |
cfw.addALoad(contextLocal); |
| 1847 |
addScriptRuntimeInvoke("exitActivationFunction", |
| 1848 |
"(Lorg/mozilla/javascript/Context;)V"); |
| 1849 |
} |
| 1850 |
|
| 1851 |
private void generateStatement(Node node) |
| 1852 |
{ |
| 1853 |
updateLineNumber(node); |
| 1854 |
int type = node.getType(); |
| 1855 |
Node child = node.getFirstChild(); |
| 1856 |
switch (type) { |
| 1857 |
case Token.LOOP: |
| 1858 |
case Token.LABEL: |
| 1859 |
case Token.WITH: |
| 1860 |
case Token.SCRIPT: |
| 1861 |
case Token.BLOCK: |
| 1862 |
case Token.EMPTY: |
| 1863 |
|
| 1864 |
if (compilerEnv.isGenerateObserverCount()) { |
| 1865 |
|
| 1866 |
|
| 1867 |
addInstructionCount(1); |
| 1868 |
} |
| 1869 |
while (child != null) { |
| 1870 |
generateStatement(child); |
| 1871 |
child = child.getNext(); |
| 1872 |
} |
| 1873 |
break; |
| 1874 |
|
| 1875 |
case Token.LOCAL_BLOCK: { |
| 1876 |
boolean prevLocal = inLocalBlock; |
| 1877 |
inLocalBlock = true; |
| 1878 |
int local = getNewWordLocal(); |
| 1879 |
if (isGenerator) { |
| 1880 |
cfw.add(ByteCode.ACONST_NULL); |
| 1881 |
cfw.addAStore(local); |
| 1882 |
} |
| 1883 |
node.putIntProp(Node.LOCAL_PROP, local); |
| 1884 |
while (child != null) { |
| 1885 |
generateStatement(child); |
| 1886 |
child = child.getNext(); |
| 1887 |
} |
| 1888 |
releaseWordLocal((short)local); |
| 1889 |
node.removeProp(Node.LOCAL_PROP); |
| 1890 |
inLocalBlock = prevLocal; |
| 1891 |
break; |
| 1892 |
} |
| 1893 |
|
| 1894 |
case Token.FUNCTION: { |
| 1895 |
int fnIndex = node.getExistingIntProp(Node.FUNCTION_PROP); |
| 1896 |
OptFunctionNode ofn = OptFunctionNode.get(scriptOrFn, fnIndex); |
| 1897 |
int t = ofn.fnode.getFunctionType(); |
| 1898 |
if (t == FunctionNode.FUNCTION_EXPRESSION_STATEMENT) { |
| 1899 |
visitFunction(ofn, t); |
| 1900 |
} else { |
| 1901 |
if (t != FunctionNode.FUNCTION_STATEMENT) { |
| 1902 |
throw Codegen.badTree(); |
| 1903 |
} |
| 1904 |
} |
| 1905 |
break; |
| 1906 |
} |
| 1907 |
|
| 1908 |
case Token.TRY: |
| 1909 |
visitTryCatchFinally((Jump)node, child); |
| 1910 |
break; |
| 1911 |
|
| 1912 |
case Token.CATCH_SCOPE: |
| 1913 |
{ |
| 1914 |
|
| 1915 |
cfw.setStackTop((short) 0); |
| 1916 |
|
| 1917 |
int local = getLocalBlockRegister(node); |
| 1918 |
int scopeIndex |
| 1919 |
= node.getExistingIntProp(Node.CATCH_SCOPE_PROP); |
| 1920 |
|
| 1921 |
String name = child.getString(); |
| 1922 |
child = child.getNext(); |
| 1923 |
generateExpression(child, node); |
| 1924 |
if (scopeIndex == 0) { |
| 1925 |
cfw.add(ByteCode.ACONST_NULL); |
| 1926 |
} else { |
| 1927 |
|
| 1928 |
cfw.addALoad(local); |
| 1929 |
} |
| 1930 |
cfw.addPush(name); |
| 1931 |
cfw.addALoad(contextLocal); |
| 1932 |
cfw.addALoad(variableObjectLocal); |
| 1933 |
|
| 1934 |
addScriptRuntimeInvoke( |
| 1935 |
"newCatchScope", |
| 1936 |
"(Ljava/lang/Throwable;" |
| 1937 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1938 |
+"Ljava/lang/String;" |
| 1939 |
+"Lorg/mozilla/javascript/Context;" |
| 1940 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1941 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 1942 |
cfw.addAStore(local); |
| 1943 |
} |
| 1944 |
break; |
| 1945 |
|
| 1946 |
case Token.THROW: |
| 1947 |
generateExpression(child, node); |
| 1948 |
if (compilerEnv.isGenerateObserverCount()) |
| 1949 |
addInstructionCount(); |
| 1950 |
generateThrowJavaScriptException(); |
| 1951 |
break; |
| 1952 |
|
| 1953 |
case Token.RETHROW: |
| 1954 |
if (compilerEnv.isGenerateObserverCount()) |
| 1955 |
addInstructionCount(); |
| 1956 |
cfw.addALoad(getLocalBlockRegister(node)); |
| 1957 |
cfw.add(ByteCode.ATHROW); |
| 1958 |
break; |
| 1959 |
|
| 1960 |
case Token.RETURN_RESULT: |
| 1961 |
case Token.RETURN: |
| 1962 |
if (!isGenerator) { |
| 1963 |
if (child != null) { |
| 1964 |
generateExpression(child, node); |
| 1965 |
} else if (type == Token.RETURN) { |
| 1966 |
Codegen.pushUndefined(cfw); |
| 1967 |
} else { |
| 1968 |
if (popvLocal < 0) throw Codegen.badTree(); |
| 1969 |
cfw.addALoad(popvLocal); |
| 1970 |
} |
| 1971 |
} |
| 1972 |
if (compilerEnv.isGenerateObserverCount()) |
| 1973 |
addInstructionCount(); |
| 1974 |
if (epilogueLabel == -1) { |
| 1975 |
if (!hasVarsInRegs) throw Codegen.badTree(); |
| 1976 |
epilogueLabel = cfw.acquireLabel(); |
| 1977 |
} |
| 1978 |
cfw.add(ByteCode.GOTO, epilogueLabel); |
| 1979 |
break; |
| 1980 |
|
| 1981 |
case Token.SWITCH: |
| 1982 |
if (compilerEnv.isGenerateObserverCount()) |
| 1983 |
addInstructionCount(); |
| 1984 |
visitSwitch((Jump)node, child); |
| 1985 |
break; |
| 1986 |
|
| 1987 |
case Token.ENTERWITH: |
| 1988 |
generateExpression(child, node); |
| 1989 |
cfw.addALoad(contextLocal); |
| 1990 |
cfw.addALoad(variableObjectLocal); |
| 1991 |
addScriptRuntimeInvoke( |
| 1992 |
"enterWith", |
| 1993 |
"(Ljava/lang/Object;" |
| 1994 |
+"Lorg/mozilla/javascript/Context;" |
| 1995 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 1996 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 1997 |
cfw.addAStore(variableObjectLocal); |
| 1998 |
incReferenceWordLocal(variableObjectLocal); |
| 1999 |
break; |
| 2000 |
|
| 2001 |
case Token.LEAVEWITH: |
| 2002 |
cfw.addALoad(variableObjectLocal); |
| 2003 |
addScriptRuntimeInvoke( |
| 2004 |
"leaveWith", |
| 2005 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 2006 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 2007 |
cfw.addAStore(variableObjectLocal); |
| 2008 |
decReferenceWordLocal(variableObjectLocal); |
| 2009 |
break; |
| 2010 |
|
| 2011 |
case Token.ENUM_INIT_KEYS: |
| 2012 |
case Token.ENUM_INIT_VALUES: |
| 2013 |
case Token.ENUM_INIT_ARRAY: |
| 2014 |
case Token.ENUM_INIT_VALUES_IN_ORDER: |
| 2015 |
generateExpression(child, node); |
| 2016 |
cfw.addALoad(contextLocal); |
| 2017 |
cfw.addALoad(variableObjectLocal); |
| 2018 |
int enumType = type == Token.ENUM_INIT_KEYS |
| 2019 |
? ScriptRuntime.ENUMERATE_KEYS : |
| 2020 |
type == Token.ENUM_INIT_VALUES |
| 2021 |
? ScriptRuntime.ENUMERATE_VALUES : |
| 2022 |
type == Token.ENUM_INIT_VALUES_IN_ORDER |
| 2023 |
? ScriptRuntime.ENUMERATE_VALUES_IN_ORDER : |
| 2024 |
ScriptRuntime.ENUMERATE_ARRAY; |
| 2025 |
cfw.addPush(enumType); |
| 2026 |
addScriptRuntimeInvoke("enumInit", |
| 2027 |
"(Ljava/lang/Object;" |
| 2028 |
+"Lorg/mozilla/javascript/Context;" |
| 2029 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2030 |
+"I" |
| 2031 |
+")Ljava/lang/Object;"); |
| 2032 |
cfw.addAStore(getLocalBlockRegister(node)); |
| 2033 |
break; |
| 2034 |
|
| 2035 |
case Token.EXPR_VOID: |
| 2036 |
if (child.getType() == Token.SETVAR) { |
| 2037 |
|
| 2038 |
|
| 2039 |
visitSetVar(child, child.getFirstChild(), false); |
| 2040 |
} |
| 2041 |
else if (child.getType() == Token.SETCONSTVAR) { |
| 2042 |
|
| 2043 |
|
| 2044 |
visitSetConstVar(child, child.getFirstChild(), false); |
| 2045 |
} |
| 2046 |
else if (child.getType() == Token.YIELD) { |
| 2047 |
generateYieldPoint(child, false); |
| 2048 |
} |
| 2049 |
else { |
| 2050 |
generateExpression(child, node); |
| 2051 |
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) |
| 2052 |
cfw.add(ByteCode.POP2); |
| 2053 |
else |
| 2054 |
cfw.add(ByteCode.POP); |
| 2055 |
} |
| 2056 |
break; |
| 2057 |
|
| 2058 |
case Token.EXPR_RESULT: |
| 2059 |
generateExpression(child, node); |
| 2060 |
if (popvLocal < 0) { |
| 2061 |
popvLocal = getNewWordLocal(); |
| 2062 |
} |
| 2063 |
cfw.addAStore(popvLocal); |
| 2064 |
break; |
| 2065 |
|
| 2066 |
case Token.TARGET: |
| 2067 |
{ |
| 2068 |
if (compilerEnv.isGenerateObserverCount()) |
| 2069 |
addInstructionCount(); |
| 2070 |
int label = getTargetLabel(node); |
| 2071 |
cfw.markLabel(label); |
| 2072 |
if (compilerEnv.isGenerateObserverCount()) |
| 2073 |
saveCurrentCodeOffset(); |
| 2074 |
} |
| 2075 |
break; |
| 2076 |
|
| 2077 |
case Token.JSR: |
| 2078 |
case Token.GOTO: |
| 2079 |
case Token.IFEQ: |
| 2080 |
case Token.IFNE: |
| 2081 |
if (compilerEnv.isGenerateObserverCount()) |
| 2082 |
addInstructionCount(); |
| 2083 |
visitGoto((Jump)node, type, child); |
| 2084 |
break; |
| 2085 |
|
| 2086 |
case Token.FINALLY: |
| 2087 |
{ |
| 2088 |
|
| 2089 |
|
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
if (!isGenerator) { |
| 2094 |
break; |
| 2095 |
} |
| 2096 |
|
| 2097 |
if (compilerEnv.isGenerateObserverCount()) |
| 2098 |
saveCurrentCodeOffset(); |
| 2099 |
|
| 2100 |
|
| 2101 |
cfw.setStackTop((short)1); |
| 2102 |
|
| 2103 |
|
| 2104 |
int finallyRegister = getNewWordLocal(); |
| 2105 |
|
| 2106 |
int finallyStart = cfw.acquireLabel(); |
| 2107 |
int finallyEnd = cfw.acquireLabel(); |
| 2108 |
cfw.markLabel(finallyStart); |
| 2109 |
|
| 2110 |
generateIntegerWrap(); |
| 2111 |
cfw.addAStore(finallyRegister); |
| 2112 |
|
| 2113 |
while (child != null) { |
| 2114 |
generateStatement(child); |
| 2115 |
child = child.getNext(); |
| 2116 |
} |
| 2117 |
|
| 2118 |
cfw.addALoad(finallyRegister); |
| 2119 |
cfw.add(ByteCode.CHECKCAST, "java/lang/Integer"); |
| 2120 |
generateIntegerUnwrap(); |
| 2121 |
FinallyReturnPoint ret = finallys.get(node); |
| 2122 |
ret.tableLabel = cfw.acquireLabel(); |
| 2123 |
cfw.add(ByteCode.GOTO, ret.tableLabel); |
| 2124 |
|
| 2125 |
releaseWordLocal((short)finallyRegister); |
| 2126 |
cfw.markLabel(finallyEnd); |
| 2127 |
} |
| 2128 |
break; |
| 2129 |
|
| 2130 |
case Token.DEBUGGER: |
| 2131 |
break; |
| 2132 |
|
| 2133 |
default: |
| 2134 |
throw Codegen.badTree(); |
| 2135 |
} |
| 2136 |
|
| 2137 |
} |
| 2138 |
|
| 2139 |
private void generateIntegerWrap() |
| 2140 |
{ |
| 2141 |
cfw.addInvoke(ByteCode.INVOKESTATIC, "java/lang/Integer", "valueOf", |
| 2142 |
"(I)Ljava/lang/Integer;"); |
| 2143 |
} |
| 2144 |
|
| 2145 |
|
| 2146 |
private void generateIntegerUnwrap() |
| 2147 |
{ |
| 2148 |
cfw.addInvoke(ByteCode.INVOKEVIRTUAL, "java/lang/Integer", |
| 2149 |
"intValue", "()I"); |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
private void generateThrowJavaScriptException() |
| 2154 |
{ |
| 2155 |
cfw.add(ByteCode.NEW, |
| 2156 |
"org/mozilla/javascript/JavaScriptException"); |
| 2157 |
cfw.add(ByteCode.DUP_X1); |
| 2158 |
cfw.add(ByteCode.SWAP); |
| 2159 |
cfw.addPush(scriptOrFn.getSourceName()); |
| 2160 |
cfw.addPush(itsLineNumber); |
| 2161 |
cfw.addInvoke( |
| 2162 |
ByteCode.INVOKESPECIAL, |
| 2163 |
"org/mozilla/javascript/JavaScriptException", |
| 2164 |
"<init>", |
| 2165 |
"(Ljava/lang/Object;Ljava/lang/String;I)V"); |
| 2166 |
cfw.add(ByteCode.ATHROW); |
| 2167 |
} |
| 2168 |
|
| 2169 |
private int getNextGeneratorState(Node node) |
| 2170 |
{ |
| 2171 |
int nodeIndex = ((FunctionNode)scriptOrFn).getResumptionPoints() |
| 2172 |
.indexOf(node); |
| 2173 |
return nodeIndex + GENERATOR_YIELD_START; |
| 2174 |
} |
| 2175 |
|
| 2176 |
private void generateExpression(Node node, Node parent) |
| 2177 |
{ |
| 2178 |
int type = node.getType(); |
| 2179 |
Node child = node.getFirstChild(); |
| 2180 |
switch (type) { |
| 2181 |
case Token.USE_STACK: |
| 2182 |
break; |
| 2183 |
|
| 2184 |
case Token.FUNCTION: |
| 2185 |
if (fnCurrent != null || parent.getType() != Token.SCRIPT) { |
| 2186 |
int fnIndex = node.getExistingIntProp(Node.FUNCTION_PROP); |
| 2187 |
OptFunctionNode ofn = OptFunctionNode.get(scriptOrFn, |
| 2188 |
fnIndex); |
| 2189 |
int t = ofn.fnode.getFunctionType(); |
| 2190 |
if (t != FunctionNode.FUNCTION_EXPRESSION && |
| 2191 |
t != FunctionNode.ARROW_FUNCTION) { |
| 2192 |
throw Codegen.badTree(); |
| 2193 |
} |
| 2194 |
visitFunction(ofn, t); |
| 2195 |
} |
| 2196 |
break; |
| 2197 |
|
| 2198 |
case Token.NAME: |
| 2199 |
{ |
| 2200 |
cfw.addALoad(contextLocal); |
| 2201 |
cfw.addALoad(variableObjectLocal); |
| 2202 |
cfw.addPush(node.getString()); |
| 2203 |
addScriptRuntimeInvoke( |
| 2204 |
"name", |
| 2205 |
"(Lorg/mozilla/javascript/Context;" |
| 2206 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2207 |
+"Ljava/lang/String;" |
| 2208 |
+")Ljava/lang/Object;"); |
| 2209 |
} |
| 2210 |
break; |
| 2211 |
|
| 2212 |
case Token.CALL: |
| 2213 |
case Token.NEW: |
| 2214 |
{ |
| 2215 |
int specialType = node.getIntProp(Node.SPECIALCALL_PROP, |
| 2216 |
Node.NON_SPECIALCALL); |
| 2217 |
if (specialType == Node.NON_SPECIALCALL) { |
| 2218 |
OptFunctionNode target; |
| 2219 |
target = (OptFunctionNode)node.getProp( |
| 2220 |
Node.DIRECTCALL_PROP); |
| 2221 |
|
| 2222 |
if (target != null) { |
| 2223 |
visitOptimizedCall(node, target, type, child); |
| 2224 |
} else if (type == Token.CALL) { |
| 2225 |
visitStandardCall(node, child); |
| 2226 |
} else { |
| 2227 |
visitStandardNew(node, child); |
| 2228 |
} |
| 2229 |
} else { |
| 2230 |
visitSpecialCall(node, type, specialType, child); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
break; |
| 2234 |
|
| 2235 |
case Token.REF_CALL: |
| 2236 |
generateFunctionAndThisObj(child, node); |
| 2237 |
|
| 2238 |
child = child.getNext(); |
| 2239 |
generateCallArgArray(node, child, false); |
| 2240 |
cfw.addALoad(contextLocal); |
| 2241 |
addScriptRuntimeInvoke( |
| 2242 |
"callRef", |
| 2243 |
"(Lorg/mozilla/javascript/Callable;" |
| 2244 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2245 |
+"[Ljava/lang/Object;" |
| 2246 |
+"Lorg/mozilla/javascript/Context;" |
| 2247 |
+")Lorg/mozilla/javascript/Ref;"); |
| 2248 |
break; |
| 2249 |
|
| 2250 |
case Token.NUMBER: |
| 2251 |
{ |
| 2252 |
double num = node.getDouble(); |
| 2253 |
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) { |
| 2254 |
cfw.addPush(num); |
| 2255 |
} else { |
| 2256 |
codegen.pushNumberAsObject(cfw, num); |
| 2257 |
} |
| 2258 |
} |
| 2259 |
break; |
| 2260 |
|
| 2261 |
case Token.STRING: |
| 2262 |
cfw.addPush(node.getString()); |
| 2263 |
break; |
| 2264 |
|
| 2265 |
case Token.THIS: |
| 2266 |
cfw.addALoad(thisObjLocal); |
| 2267 |
break; |
| 2268 |
|
| 2269 |
case Token.THISFN: |
| 2270 |
cfw.add(ByteCode.ALOAD_0); |
| 2271 |
break; |
| 2272 |
|
| 2273 |
case Token.NULL: |
| 2274 |
cfw.add(ByteCode.ACONST_NULL); |
| 2275 |
break; |
| 2276 |
|
| 2277 |
case Token.TRUE: |
| 2278 |
cfw.add(ByteCode.GETSTATIC, "java/lang/Boolean", |
| 2279 |
"TRUE", "Ljava/lang/Boolean;"); |
| 2280 |
break; |
| 2281 |
|
| 2282 |
case Token.FALSE: |
| 2283 |
cfw.add(ByteCode.GETSTATIC, "java/lang/Boolean", |
| 2284 |
"FALSE", "Ljava/lang/Boolean;"); |
| 2285 |
break; |
| 2286 |
|
| 2287 |
case Token.REGEXP: |
| 2288 |
{ |
| 2289 |
|
| 2290 |
cfw.addALoad(contextLocal); |
| 2291 |
cfw.addALoad(variableObjectLocal); |
| 2292 |
int i = node.getExistingIntProp(Node.REGEXP_PROP); |
| 2293 |
cfw.add(ByteCode.GETSTATIC, codegen.mainClassName, |
| 2294 |
codegen.getCompiledRegexpName(scriptOrFn, i), |
| 2295 |
"Ljava/lang/Object;"); |
| 2296 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 2297 |
"org/mozilla/javascript/ScriptRuntime", |
| 2298 |
"wrapRegExp", |
| 2299 |
"(Lorg/mozilla/javascript/Context;" |
| 2300 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2301 |
+"Ljava/lang/Object;" |
| 2302 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 2303 |
} |
| 2304 |
break; |
| 2305 |
|
| 2306 |
case Token.COMMA: { |
| 2307 |
Node next = child.getNext(); |
| 2308 |
while (next != null) { |
| 2309 |
generateExpression(child, node); |
| 2310 |
cfw.add(ByteCode.POP); |
| 2311 |
child = next; |
| 2312 |
next = next.getNext(); |
| 2313 |
} |
| 2314 |
generateExpression(child, node); |
| 2315 |
break; |
| 2316 |
} |
| 2317 |
|
| 2318 |
case Token.ENUM_NEXT: |
| 2319 |
case Token.ENUM_ID: { |
| 2320 |
int local = getLocalBlockRegister(node); |
| 2321 |
cfw.addALoad(local); |
| 2322 |
if (type == Token.ENUM_NEXT) { |
| 2323 |
addScriptRuntimeInvoke( |
| 2324 |
"enumNext", "(Ljava/lang/Object;)Ljava/lang/Boolean;"); |
| 2325 |
} else { |
| 2326 |
cfw.addALoad(contextLocal); |
| 2327 |
addScriptRuntimeInvoke("enumId", |
| 2328 |
"(Ljava/lang/Object;" |
| 2329 |
+"Lorg/mozilla/javascript/Context;" |
| 2330 |
+")Ljava/lang/Object;"); |
| 2331 |
} |
| 2332 |
break; |
| 2333 |
} |
| 2334 |
|
| 2335 |
case Token.ARRAYLIT: |
| 2336 |
visitArrayLiteral(node, child, false); |
| 2337 |
break; |
| 2338 |
|
| 2339 |
case Token.OBJECTLIT: |
| 2340 |
visitObjectLiteral(node, child, false); |
| 2341 |
break; |
| 2342 |
|
| 2343 |
case Token.NOT: { |
| 2344 |
int trueTarget = cfw.acquireLabel(); |
| 2345 |
int falseTarget = cfw.acquireLabel(); |
| 2346 |
int beyond = cfw.acquireLabel(); |
| 2347 |
generateIfJump(child, node, trueTarget, falseTarget); |
| 2348 |
|
| 2349 |
cfw.markLabel(trueTarget); |
| 2350 |
cfw.add(ByteCode.GETSTATIC, "java/lang/Boolean", |
| 2351 |
"FALSE", "Ljava/lang/Boolean;"); |
| 2352 |
cfw.add(ByteCode.GOTO, beyond); |
| 2353 |
cfw.markLabel(falseTarget); |
| 2354 |
cfw.add(ByteCode.GETSTATIC, "java/lang/Boolean", |
| 2355 |
"TRUE", "Ljava/lang/Boolean;"); |
| 2356 |
cfw.markLabel(beyond); |
| 2357 |
cfw.adjustStackTop(-1); |
| 2358 |
break; |
| 2359 |
} |
| 2360 |
|
| 2361 |
case Token.BITNOT: |
| 2362 |
generateExpression(child, node); |
| 2363 |
addScriptRuntimeInvoke("toInt32", "(Ljava/lang/Object;)I"); |
| 2364 |
cfw.addPush(-1); |
| 2365 |
cfw.add(ByteCode.IXOR); |
| 2366 |
cfw.add(ByteCode.I2D); |
| 2367 |
addDoubleWrap(); |
| 2368 |
break; |
| 2369 |
|
| 2370 |
case Token.VOID: |
| 2371 |
generateExpression(child, node); |
| 2372 |
cfw.add(ByteCode.POP); |
| 2373 |
Codegen.pushUndefined(cfw); |
| 2374 |
break; |
| 2375 |
|
| 2376 |
case Token.TYPEOF: |
| 2377 |
generateExpression(child, node); |
| 2378 |
addScriptRuntimeInvoke("typeof", |
| 2379 |
"(Ljava/lang/Object;" |
| 2380 |
+")Ljava/lang/String;"); |
| 2381 |
break; |
| 2382 |
|
| 2383 |
case Token.TYPEOFNAME: |
| 2384 |
visitTypeofname(node); |
| 2385 |
break; |
| 2386 |
|
| 2387 |
case Token.INC: |
| 2388 |
case Token.DEC: |
| 2389 |
visitIncDec(node); |
| 2390 |
break; |
| 2391 |
|
| 2392 |
case Token.OR: |
| 2393 |
case Token.AND: { |
| 2394 |
generateExpression(child, node); |
| 2395 |
cfw.add(ByteCode.DUP); |
| 2396 |
addScriptRuntimeInvoke("toBoolean", |
| 2397 |
"(Ljava/lang/Object;)Z"); |
| 2398 |
int falseTarget = cfw.acquireLabel(); |
| 2399 |
if (type == Token.AND) |
| 2400 |
cfw.add(ByteCode.IFEQ, falseTarget); |
| 2401 |
else |
| 2402 |
cfw.add(ByteCode.IFNE, falseTarget); |
| 2403 |
cfw.add(ByteCode.POP); |
| 2404 |
generateExpression(child.getNext(), node); |
| 2405 |
cfw.markLabel(falseTarget); |
| 2406 |
} |
| 2407 |
break; |
| 2408 |
|
| 2409 |
case Token.HOOK : { |
| 2410 |
Node ifThen = child.getNext(); |
| 2411 |
Node ifElse = ifThen.getNext(); |
| 2412 |
generateExpression(child, node); |
| 2413 |
addScriptRuntimeInvoke("toBoolean", |
| 2414 |
"(Ljava/lang/Object;)Z"); |
| 2415 |
int elseTarget = cfw.acquireLabel(); |
| 2416 |
cfw.add(ByteCode.IFEQ, elseTarget); |
| 2417 |
short stack = cfw.getStackTop(); |
| 2418 |
generateExpression(ifThen, node); |
| 2419 |
int afterHook = cfw.acquireLabel(); |
| 2420 |
cfw.add(ByteCode.GOTO, afterHook); |
| 2421 |
cfw.markLabel(elseTarget, stack); |
| 2422 |
generateExpression(ifElse, node); |
| 2423 |
cfw.markLabel(afterHook); |
| 2424 |
} |
| 2425 |
break; |
| 2426 |
|
| 2427 |
case Token.ADD: { |
| 2428 |
generateExpression(child, node); |
| 2429 |
generateExpression(child.getNext(), node); |
| 2430 |
switch (node.getIntProp(Node.ISNUMBER_PROP, -1)) { |
| 2431 |
case Node.BOTH: |
| 2432 |
cfw.add(ByteCode.DADD); |
| 2433 |
break; |
| 2434 |
case Node.LEFT: |
| 2435 |
addOptRuntimeInvoke("add", |
| 2436 |
"(DLjava/lang/Object;)Ljava/lang/Object;"); |
| 2437 |
break; |
| 2438 |
case Node.RIGHT: |
| 2439 |
addOptRuntimeInvoke("add", |
| 2440 |
"(Ljava/lang/Object;D)Ljava/lang/Object;"); |
| 2441 |
break; |
| 2442 |
default: |
| 2443 |
if (child.getType() == Token.STRING) { |
| 2444 |
addScriptRuntimeInvoke("add", |
| 2445 |
"(Ljava/lang/CharSequence;" |
| 2446 |
+"Ljava/lang/Object;" |
| 2447 |
+")Ljava/lang/CharSequence;"); |
| 2448 |
} else if (child.getNext().getType() == Token.STRING) { |
| 2449 |
addScriptRuntimeInvoke("add", |
| 2450 |
"(Ljava/lang/Object;" |
| 2451 |
+"Ljava/lang/CharSequence;" |
| 2452 |
+")Ljava/lang/CharSequence;"); |
| 2453 |
} else { |
| 2454 |
cfw.addALoad(contextLocal); |
| 2455 |
addScriptRuntimeInvoke("add", |
| 2456 |
"(Ljava/lang/Object;" |
| 2457 |
+"Ljava/lang/Object;" |
| 2458 |
+"Lorg/mozilla/javascript/Context;" |
| 2459 |
+")Ljava/lang/Object;"); |
| 2460 |
} |
| 2461 |
} |
| 2462 |
} |
| 2463 |
break; |
| 2464 |
|
| 2465 |
case Token.MUL: |
| 2466 |
visitArithmetic(node, ByteCode.DMUL, child, parent); |
| 2467 |
break; |
| 2468 |
|
| 2469 |
case Token.SUB: |
| 2470 |
visitArithmetic(node, ByteCode.DSUB, child, parent); |
| 2471 |
break; |
| 2472 |
|
| 2473 |
case Token.DIV: |
| 2474 |
case Token.MOD: |
| 2475 |
visitArithmetic(node, type == Token.DIV |
| 2476 |
? ByteCode.DDIV |
| 2477 |
: ByteCode.DREM, child, parent); |
| 2478 |
break; |
| 2479 |
|
| 2480 |
case Token.BITOR: |
| 2481 |
case Token.BITXOR: |
| 2482 |
case Token.BITAND: |
| 2483 |
case Token.LSH: |
| 2484 |
case Token.RSH: |
| 2485 |
case Token.URSH: |
| 2486 |
visitBitOp(node, type, child); |
| 2487 |
break; |
| 2488 |
|
| 2489 |
case Token.POS: |
| 2490 |
case Token.NEG: |
| 2491 |
generateExpression(child, node); |
| 2492 |
addObjectToDouble(); |
| 2493 |
if (type == Token.NEG) { |
| 2494 |
cfw.add(ByteCode.DNEG); |
| 2495 |
} |
| 2496 |
addDoubleWrap(); |
| 2497 |
break; |
| 2498 |
|
| 2499 |
case Token.TO_DOUBLE: |
| 2500 |
|
| 2501 |
generateExpression(child, node); |
| 2502 |
addObjectToDouble(); |
| 2503 |
break; |
| 2504 |
|
| 2505 |
case Token.TO_OBJECT: { |
| 2506 |
|
| 2507 |
int prop = -1; |
| 2508 |
if (child.getType() == Token.NUMBER) { |
| 2509 |
prop = child.getIntProp(Node.ISNUMBER_PROP, -1); |
| 2510 |
} |
| 2511 |
if (prop != -1) { |
| 2512 |
child.removeProp(Node.ISNUMBER_PROP); |
| 2513 |
generateExpression(child, node); |
| 2514 |
child.putIntProp(Node.ISNUMBER_PROP, prop); |
| 2515 |
} else { |
| 2516 |
generateExpression(child, node); |
| 2517 |
addDoubleWrap(); |
| 2518 |
} |
| 2519 |
break; |
| 2520 |
} |
| 2521 |
|
| 2522 |
case Token.IN: |
| 2523 |
case Token.INSTANCEOF: |
| 2524 |
case Token.LE: |
| 2525 |
case Token.LT: |
| 2526 |
case Token.GE: |
| 2527 |
case Token.GT: { |
| 2528 |
int trueGOTO = cfw.acquireLabel(); |
| 2529 |
int falseGOTO = cfw.acquireLabel(); |
| 2530 |
visitIfJumpRelOp(node, child, trueGOTO, falseGOTO); |
| 2531 |
addJumpedBooleanWrap(trueGOTO, falseGOTO); |
| 2532 |
break; |
| 2533 |
} |
| 2534 |
|
| 2535 |
case Token.EQ: |
| 2536 |
case Token.NE: |
| 2537 |
case Token.SHEQ: |
| 2538 |
case Token.SHNE: { |
| 2539 |
int trueGOTO = cfw.acquireLabel(); |
| 2540 |
int falseGOTO = cfw.acquireLabel(); |
| 2541 |
visitIfJumpEqOp(node, child, trueGOTO, falseGOTO); |
| 2542 |
addJumpedBooleanWrap(trueGOTO, falseGOTO); |
| 2543 |
break; |
| 2544 |
} |
| 2545 |
|
| 2546 |
case Token.GETPROP: |
| 2547 |
case Token.GETPROPNOWARN: |
| 2548 |
visitGetProp(node, child); |
| 2549 |
break; |
| 2550 |
|
| 2551 |
case Token.GETELEM: |
| 2552 |
generateExpression(child, node); |
| 2553 |
generateExpression(child.getNext(), node); |
| 2554 |
cfw.addALoad(contextLocal); |
| 2555 |
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) { |
| 2556 |
addScriptRuntimeInvoke( |
| 2557 |
"getObjectIndex", |
| 2558 |
"(Ljava/lang/Object;D" |
| 2559 |
+"Lorg/mozilla/javascript/Context;" |
| 2560 |
+")Ljava/lang/Object;"); |
| 2561 |
} |
| 2562 |
else { |
| 2563 |
cfw.addALoad(variableObjectLocal); |
| 2564 |
addScriptRuntimeInvoke( |
| 2565 |
"getObjectElem", |
| 2566 |
"(Ljava/lang/Object;" |
| 2567 |
+"Ljava/lang/Object;" |
| 2568 |
+"Lorg/mozilla/javascript/Context;" |
| 2569 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2570 |
+")Ljava/lang/Object;"); |
| 2571 |
} |
| 2572 |
break; |
| 2573 |
|
| 2574 |
case Token.GET_REF: |
| 2575 |
generateExpression(child, node); |
| 2576 |
cfw.addALoad(contextLocal); |
| 2577 |
addScriptRuntimeInvoke( |
| 2578 |
"refGet", |
| 2579 |
"(Lorg/mozilla/javascript/Ref;" |
| 2580 |
+"Lorg/mozilla/javascript/Context;" |
| 2581 |
+")Ljava/lang/Object;"); |
| 2582 |
break; |
| 2583 |
|
| 2584 |
case Token.GETVAR: |
| 2585 |
visitGetVar(node); |
| 2586 |
break; |
| 2587 |
|
| 2588 |
case Token.SETVAR: |
| 2589 |
visitSetVar(node, child, true); |
| 2590 |
break; |
| 2591 |
|
| 2592 |
case Token.SETNAME: |
| 2593 |
visitSetName(node, child); |
| 2594 |
break; |
| 2595 |
|
| 2596 |
case Token.STRICT_SETNAME: |
| 2597 |
visitStrictSetName(node, child); |
| 2598 |
break; |
| 2599 |
|
| 2600 |
case Token.SETCONST: |
| 2601 |
visitSetConst(node, child); |
| 2602 |
break; |
| 2603 |
|
| 2604 |
case Token.SETCONSTVAR: |
| 2605 |
visitSetConstVar(node, child, true); |
| 2606 |
break; |
| 2607 |
|
| 2608 |
case Token.SETPROP: |
| 2609 |
case Token.SETPROP_OP: |
| 2610 |
visitSetProp(type, node, child); |
| 2611 |
break; |
| 2612 |
|
| 2613 |
case Token.SETELEM: |
| 2614 |
case Token.SETELEM_OP: |
| 2615 |
visitSetElem(type, node, child); |
| 2616 |
break; |
| 2617 |
|
| 2618 |
case Token.SET_REF: |
| 2619 |
case Token.SET_REF_OP: |
| 2620 |
{ |
| 2621 |
generateExpression(child, node); |
| 2622 |
child = child.getNext(); |
| 2623 |
if (type == Token.SET_REF_OP) { |
| 2624 |
cfw.add(ByteCode.DUP); |
| 2625 |
cfw.addALoad(contextLocal); |
| 2626 |
addScriptRuntimeInvoke( |
| 2627 |
"refGet", |
| 2628 |
"(Lorg/mozilla/javascript/Ref;" |
| 2629 |
+"Lorg/mozilla/javascript/Context;" |
| 2630 |
+")Ljava/lang/Object;"); |
| 2631 |
} |
| 2632 |
generateExpression(child, node); |
| 2633 |
cfw.addALoad(contextLocal); |
| 2634 |
cfw.addALoad(variableObjectLocal); |
| 2635 |
addScriptRuntimeInvoke( |
| 2636 |
"refSet", |
| 2637 |
"(Lorg/mozilla/javascript/Ref;" |
| 2638 |
+"Ljava/lang/Object;" |
| 2639 |
+"Lorg/mozilla/javascript/Context;" |
| 2640 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2641 |
+")Ljava/lang/Object;"); |
| 2642 |
} |
| 2643 |
break; |
| 2644 |
|
| 2645 |
case Token.DEL_REF: |
| 2646 |
generateExpression(child, node); |
| 2647 |
cfw.addALoad(contextLocal); |
| 2648 |
addScriptRuntimeInvoke("refDel", |
| 2649 |
"(Lorg/mozilla/javascript/Ref;" |
| 2650 |
+"Lorg/mozilla/javascript/Context;" |
| 2651 |
+")Ljava/lang/Object;"); |
| 2652 |
break; |
| 2653 |
|
| 2654 |
case Token.DELPROP: |
| 2655 |
boolean isName = child.getType() == Token.BINDNAME; |
| 2656 |
generateExpression(child, node); |
| 2657 |
child = child.getNext(); |
| 2658 |
generateExpression(child, node); |
| 2659 |
cfw.addALoad(contextLocal); |
| 2660 |
cfw.addPush(isName); |
| 2661 |
addScriptRuntimeInvoke("delete", |
| 2662 |
"(Ljava/lang/Object;" |
| 2663 |
+"Ljava/lang/Object;" |
| 2664 |
+"Lorg/mozilla/javascript/Context;" |
| 2665 |
+"Z)Ljava/lang/Object;"); |
| 2666 |
break; |
| 2667 |
|
| 2668 |
case Token.BINDNAME: |
| 2669 |
{ |
| 2670 |
while (child != null) { |
| 2671 |
generateExpression(child, node); |
| 2672 |
child = child.getNext(); |
| 2673 |
} |
| 2674 |
|
| 2675 |
cfw.addALoad(contextLocal); |
| 2676 |
cfw.addALoad(variableObjectLocal); |
| 2677 |
cfw.addPush(node.getString()); |
| 2678 |
addScriptRuntimeInvoke( |
| 2679 |
"bind", |
| 2680 |
"(Lorg/mozilla/javascript/Context;" |
| 2681 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2682 |
+"Ljava/lang/String;" |
| 2683 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 2684 |
} |
| 2685 |
break; |
| 2686 |
|
| 2687 |
case Token.LOCAL_LOAD: |
| 2688 |
cfw.addALoad(getLocalBlockRegister(node)); |
| 2689 |
break; |
| 2690 |
|
| 2691 |
case Token.REF_SPECIAL: |
| 2692 |
{ |
| 2693 |
String special = (String)node.getProp(Node.NAME_PROP); |
| 2694 |
generateExpression(child, node); |
| 2695 |
cfw.addPush(special); |
| 2696 |
cfw.addALoad(contextLocal); |
| 2697 |
cfw.addALoad(variableObjectLocal); |
| 2698 |
addScriptRuntimeInvoke( |
| 2699 |
"specialRef", |
| 2700 |
"(Ljava/lang/Object;" |
| 2701 |
+"Ljava/lang/String;" |
| 2702 |
+"Lorg/mozilla/javascript/Context;" |
| 2703 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2704 |
+")Lorg/mozilla/javascript/Ref;"); |
| 2705 |
} |
| 2706 |
break; |
| 2707 |
|
| 2708 |
case Token.REF_MEMBER: |
| 2709 |
case Token.REF_NS_MEMBER: |
| 2710 |
case Token.REF_NAME: |
| 2711 |
case Token.REF_NS_NAME: |
| 2712 |
{ |
| 2713 |
int memberTypeFlags |
| 2714 |
= node.getIntProp(Node.MEMBER_TYPE_PROP, 0); |
| 2715 |
|
| 2716 |
do { |
| 2717 |
generateExpression(child, node); |
| 2718 |
child = child.getNext(); |
| 2719 |
} while (child != null); |
| 2720 |
cfw.addALoad(contextLocal); |
| 2721 |
String methodName, signature; |
| 2722 |
switch (type) { |
| 2723 |
case Token.REF_MEMBER: |
| 2724 |
methodName = "memberRef"; |
| 2725 |
signature = "(Ljava/lang/Object;" |
| 2726 |
+"Ljava/lang/Object;" |
| 2727 |
+"Lorg/mozilla/javascript/Context;" |
| 2728 |
+"I" |
| 2729 |
+")Lorg/mozilla/javascript/Ref;"; |
| 2730 |
break; |
| 2731 |
case Token.REF_NS_MEMBER: |
| 2732 |
methodName = "memberRef"; |
| 2733 |
signature = "(Ljava/lang/Object;" |
| 2734 |
+"Ljava/lang/Object;" |
| 2735 |
+"Ljava/lang/Object;" |
| 2736 |
+"Lorg/mozilla/javascript/Context;" |
| 2737 |
+"I" |
| 2738 |
+")Lorg/mozilla/javascript/Ref;"; |
| 2739 |
break; |
| 2740 |
case Token.REF_NAME: |
| 2741 |
methodName = "nameRef"; |
| 2742 |
signature = "(Ljava/lang/Object;" |
| 2743 |
+"Lorg/mozilla/javascript/Context;" |
| 2744 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2745 |
+"I" |
| 2746 |
+")Lorg/mozilla/javascript/Ref;"; |
| 2747 |
cfw.addALoad(variableObjectLocal); |
| 2748 |
break; |
| 2749 |
case Token.REF_NS_NAME: |
| 2750 |
methodName = "nameRef"; |
| 2751 |
signature = "(Ljava/lang/Object;" |
| 2752 |
+"Ljava/lang/Object;" |
| 2753 |
+"Lorg/mozilla/javascript/Context;" |
| 2754 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2755 |
+"I" |
| 2756 |
+")Lorg/mozilla/javascript/Ref;"; |
| 2757 |
cfw.addALoad(variableObjectLocal); |
| 2758 |
break; |
| 2759 |
default: |
| 2760 |
throw Kit.codeBug(); |
| 2761 |
} |
| 2762 |
cfw.addPush(memberTypeFlags); |
| 2763 |
addScriptRuntimeInvoke(methodName, signature); |
| 2764 |
} |
| 2765 |
break; |
| 2766 |
|
| 2767 |
case Token.DOTQUERY: |
| 2768 |
visitDotQuery(node, child); |
| 2769 |
break; |
| 2770 |
|
| 2771 |
case Token.ESCXMLATTR: |
| 2772 |
generateExpression(child, node); |
| 2773 |
cfw.addALoad(contextLocal); |
| 2774 |
addScriptRuntimeInvoke("escapeAttributeValue", |
| 2775 |
"(Ljava/lang/Object;" |
| 2776 |
+"Lorg/mozilla/javascript/Context;" |
| 2777 |
+")Ljava/lang/String;"); |
| 2778 |
break; |
| 2779 |
|
| 2780 |
case Token.ESCXMLTEXT: |
| 2781 |
generateExpression(child, node); |
| 2782 |
cfw.addALoad(contextLocal); |
| 2783 |
addScriptRuntimeInvoke("escapeTextValue", |
| 2784 |
"(Ljava/lang/Object;" |
| 2785 |
+"Lorg/mozilla/javascript/Context;" |
| 2786 |
+")Ljava/lang/String;"); |
| 2787 |
break; |
| 2788 |
|
| 2789 |
case Token.DEFAULTNAMESPACE: |
| 2790 |
generateExpression(child, node); |
| 2791 |
cfw.addALoad(contextLocal); |
| 2792 |
addScriptRuntimeInvoke("setDefaultNamespace", |
| 2793 |
"(Ljava/lang/Object;" |
| 2794 |
+"Lorg/mozilla/javascript/Context;" |
| 2795 |
+")Ljava/lang/Object;"); |
| 2796 |
break; |
| 2797 |
|
| 2798 |
case Token.YIELD: |
| 2799 |
generateYieldPoint(node, true); |
| 2800 |
break; |
| 2801 |
|
| 2802 |
case Token.WITHEXPR: { |
| 2803 |
Node enterWith = child; |
| 2804 |
Node with = enterWith.getNext(); |
| 2805 |
Node leaveWith = with.getNext(); |
| 2806 |
generateStatement(enterWith); |
| 2807 |
generateExpression(with.getFirstChild(), with); |
| 2808 |
generateStatement(leaveWith); |
| 2809 |
break; |
| 2810 |
} |
| 2811 |
|
| 2812 |
case Token.ARRAYCOMP: { |
| 2813 |
Node initStmt = child; |
| 2814 |
Node expr = child.getNext(); |
| 2815 |
generateStatement(initStmt); |
| 2816 |
generateExpression(expr, node); |
| 2817 |
break; |
| 2818 |
} |
| 2819 |
|
| 2820 |
default: |
| 2821 |
throw new RuntimeException("Unexpected node type "+type); |
| 2822 |
} |
| 2823 |
|
| 2824 |
} |
| 2825 |
|
| 2826 |
private void generateYieldPoint(Node node, boolean exprContext) { |
| 2827 |
|
| 2828 |
int top = cfw.getStackTop(); |
| 2829 |
maxStack = maxStack > top ? maxStack : top; |
| 2830 |
if (cfw.getStackTop() != 0) { |
| 2831 |
generateGetGeneratorStackState(); |
| 2832 |
for (int i = 0; i < top; i++) { |
| 2833 |
cfw.add(ByteCode.DUP_X1); |
| 2834 |
cfw.add(ByteCode.SWAP); |
| 2835 |
cfw.addLoadConstant(i); |
| 2836 |
cfw.add(ByteCode.SWAP); |
| 2837 |
cfw.add(ByteCode.AASTORE); |
| 2838 |
} |
| 2839 |
|
| 2840 |
cfw.add(ByteCode.POP); |
| 2841 |
} |
| 2842 |
|
| 2843 |
|
| 2844 |
Node child = node.getFirstChild(); |
| 2845 |
if (child != null) |
| 2846 |
generateExpression(child, node); |
| 2847 |
else |
| 2848 |
Codegen.pushUndefined(cfw); |
| 2849 |
|
| 2850 |
|
| 2851 |
int nextState = getNextGeneratorState(node); |
| 2852 |
generateSetGeneratorResumptionPoint(nextState); |
| 2853 |
|
| 2854 |
boolean hasLocals = generateSaveLocals(node); |
| 2855 |
|
| 2856 |
cfw.add(ByteCode.ARETURN); |
| 2857 |
|
| 2858 |
generateCheckForThrowOrClose(getTargetLabel(node), |
| 2859 |
hasLocals, nextState); |
| 2860 |
|
| 2861 |
|
| 2862 |
if (top != 0) { |
| 2863 |
generateGetGeneratorStackState(); |
| 2864 |
for (int i = 0; i < top; i++) { |
| 2865 |
cfw.add(ByteCode.DUP); |
| 2866 |
cfw.addLoadConstant(top - i - 1); |
| 2867 |
cfw.add(ByteCode.AALOAD); |
| 2868 |
cfw.add(ByteCode.SWAP); |
| 2869 |
} |
| 2870 |
cfw.add(ByteCode.POP); |
| 2871 |
} |
| 2872 |
|
| 2873 |
|
| 2874 |
if (exprContext) { |
| 2875 |
cfw.addALoad(argsLocal); |
| 2876 |
} |
| 2877 |
} |
| 2878 |
|
| 2879 |
private void generateCheckForThrowOrClose(int label, |
| 2880 |
boolean hasLocals, |
| 2881 |
int nextState) { |
| 2882 |
int throwLabel = cfw.acquireLabel(); |
| 2883 |
int closeLabel = cfw.acquireLabel(); |
| 2884 |
|
| 2885 |
|
| 2886 |
cfw.markLabel(throwLabel); |
| 2887 |
cfw.addALoad(argsLocal); |
| 2888 |
generateThrowJavaScriptException(); |
| 2889 |
|
| 2890 |
|
| 2891 |
cfw.markLabel(closeLabel); |
| 2892 |
cfw.addALoad(argsLocal); |
| 2893 |
cfw.add(ByteCode.CHECKCAST, "java/lang/Throwable"); |
| 2894 |
cfw.add(ByteCode.ATHROW); |
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
if (label != -1) |
| 2899 |
cfw.markLabel(label); |
| 2900 |
if (!hasLocals) { |
| 2901 |
|
| 2902 |
cfw.markTableSwitchCase(generatorSwitch, nextState); |
| 2903 |
} |
| 2904 |
|
| 2905 |
|
| 2906 |
cfw.addILoad(operationLocal); |
| 2907 |
cfw.addLoadConstant(NativeGenerator.GENERATOR_CLOSE); |
| 2908 |
cfw.add(ByteCode.IF_ICMPEQ, closeLabel); |
| 2909 |
cfw.addILoad(operationLocal); |
| 2910 |
cfw.addLoadConstant(NativeGenerator.GENERATOR_THROW); |
| 2911 |
cfw.add(ByteCode.IF_ICMPEQ, throwLabel); |
| 2912 |
} |
| 2913 |
|
| 2914 |
private void generateIfJump(Node node, Node parent, |
| 2915 |
int trueLabel, int falseLabel) |
| 2916 |
{ |
| 2917 |
|
| 2918 |
|
| 2919 |
int type = node.getType(); |
| 2920 |
Node child = node.getFirstChild(); |
| 2921 |
|
| 2922 |
switch (type) { |
| 2923 |
case Token.NOT: |
| 2924 |
generateIfJump(child, node, falseLabel, trueLabel); |
| 2925 |
break; |
| 2926 |
|
| 2927 |
case Token.OR: |
| 2928 |
case Token.AND: { |
| 2929 |
int interLabel = cfw.acquireLabel(); |
| 2930 |
if (type == Token.AND) { |
| 2931 |
generateIfJump(child, node, interLabel, falseLabel); |
| 2932 |
} |
| 2933 |
else { |
| 2934 |
generateIfJump(child, node, trueLabel, interLabel); |
| 2935 |
} |
| 2936 |
cfw.markLabel(interLabel); |
| 2937 |
child = child.getNext(); |
| 2938 |
generateIfJump(child, node, trueLabel, falseLabel); |
| 2939 |
break; |
| 2940 |
} |
| 2941 |
|
| 2942 |
case Token.IN: |
| 2943 |
case Token.INSTANCEOF: |
| 2944 |
case Token.LE: |
| 2945 |
case Token.LT: |
| 2946 |
case Token.GE: |
| 2947 |
case Token.GT: |
| 2948 |
visitIfJumpRelOp(node, child, trueLabel, falseLabel); |
| 2949 |
break; |
| 2950 |
|
| 2951 |
case Token.EQ: |
| 2952 |
case Token.NE: |
| 2953 |
case Token.SHEQ: |
| 2954 |
case Token.SHNE: |
| 2955 |
visitIfJumpEqOp(node, child, trueLabel, falseLabel); |
| 2956 |
break; |
| 2957 |
|
| 2958 |
default: |
| 2959 |
|
| 2960 |
generateExpression(node, parent); |
| 2961 |
addScriptRuntimeInvoke("toBoolean", "(Ljava/lang/Object;)Z"); |
| 2962 |
cfw.add(ByteCode.IFNE, trueLabel); |
| 2963 |
cfw.add(ByteCode.GOTO, falseLabel); |
| 2964 |
} |
| 2965 |
} |
| 2966 |
|
| 2967 |
private void visitFunction(OptFunctionNode ofn, int functionType) |
| 2968 |
{ |
| 2969 |
int fnIndex = codegen.getIndex(ofn.fnode); |
| 2970 |
cfw.add(ByteCode.NEW, codegen.mainClassName); |
| 2971 |
|
| 2972 |
cfw.add(ByteCode.DUP); |
| 2973 |
cfw.addALoad(variableObjectLocal); |
| 2974 |
cfw.addALoad(contextLocal); |
| 2975 |
cfw.addPush(fnIndex); |
| 2976 |
cfw.addInvoke(ByteCode.INVOKESPECIAL, codegen.mainClassName, |
| 2977 |
"<init>", Codegen.FUNCTION_CONSTRUCTOR_SIGNATURE); |
| 2978 |
|
| 2979 |
if (functionType == FunctionNode.ARROW_FUNCTION) { |
| 2980 |
cfw.addALoad(contextLocal); |
| 2981 |
cfw.addALoad(variableObjectLocal); |
| 2982 |
cfw.addALoad(thisObjLocal); |
| 2983 |
addOptRuntimeInvoke("bindThis", |
| 2984 |
"(Lorg/mozilla/javascript/NativeFunction;" |
| 2985 |
+"Lorg/mozilla/javascript/Context;" |
| 2986 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2987 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 2988 |
+")Lorg/mozilla/javascript/Function;"); |
| 2989 |
} |
| 2990 |
|
| 2991 |
if (functionType == FunctionNode.FUNCTION_EXPRESSION || |
| 2992 |
functionType == FunctionNode.ARROW_FUNCTION) { |
| 2993 |
|
| 2994 |
|
| 2995 |
return; |
| 2996 |
} |
| 2997 |
cfw.addPush(functionType); |
| 2998 |
cfw.addALoad(variableObjectLocal); |
| 2999 |
cfw.addALoad(contextLocal); |
| 3000 |
addOptRuntimeInvoke("initFunction", |
| 3001 |
"(Lorg/mozilla/javascript/NativeFunction;" |
| 3002 |
+"I" |
| 3003 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3004 |
+"Lorg/mozilla/javascript/Context;" |
| 3005 |
+")V"); |
| 3006 |
} |
| 3007 |
|
| 3008 |
private int getTargetLabel(Node target) |
| 3009 |
{ |
| 3010 |
int labelId = target.labelId(); |
| 3011 |
if (labelId == -1) { |
| 3012 |
labelId = cfw.acquireLabel(); |
| 3013 |
target.labelId(labelId); |
| 3014 |
} |
| 3015 |
return labelId; |
| 3016 |
} |
| 3017 |
|
| 3018 |
private void visitGoto(Jump node, int type, Node child) |
| 3019 |
{ |
| 3020 |
Node target = node.target; |
| 3021 |
if (type == Token.IFEQ || type == Token.IFNE) { |
| 3022 |
if (child == null) throw Codegen.badTree(); |
| 3023 |
int targetLabel = getTargetLabel(target); |
| 3024 |
int fallThruLabel = cfw.acquireLabel(); |
| 3025 |
if (type == Token.IFEQ) |
| 3026 |
generateIfJump(child, node, targetLabel, fallThruLabel); |
| 3027 |
else |
| 3028 |
generateIfJump(child, node, fallThruLabel, targetLabel); |
| 3029 |
cfw.markLabel(fallThruLabel); |
| 3030 |
} else { |
| 3031 |
if (type == Token.JSR) { |
| 3032 |
if (isGenerator) { |
| 3033 |
addGotoWithReturn(target); |
| 3034 |
} else { |
| 3035 |
|
| 3036 |
inlineFinally(target); |
| 3037 |
} |
| 3038 |
} else { |
| 3039 |
addGoto(target, ByteCode.GOTO); |
| 3040 |
} |
| 3041 |
} |
| 3042 |
} |
| 3043 |
|
| 3044 |
private void addGotoWithReturn(Node target) { |
| 3045 |
FinallyReturnPoint ret = finallys.get(target); |
| 3046 |
cfw.addLoadConstant(ret.jsrPoints.size()); |
| 3047 |
addGoto(target, ByteCode.GOTO); |
| 3048 |
int retLabel = cfw.acquireLabel(); |
| 3049 |
cfw.markLabel(retLabel); |
| 3050 |
ret.jsrPoints.add(Integer.valueOf(retLabel)); |
| 3051 |
} |
| 3052 |
|
| 3053 |
private void generateArrayLiteralFactory(Node node, int count) { |
| 3054 |
String methodName = codegen.getBodyMethodName(scriptOrFn) + "_literal" + count; |
| 3055 |
initBodyGeneration(); |
| 3056 |
argsLocal = firstFreeLocal++; |
| 3057 |
localsMax = firstFreeLocal; |
| 3058 |
cfw.startMethod(methodName, "(Lorg/mozilla/javascript/Context;" |
| 3059 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3060 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3061 |
+"[Ljava/lang/Object;" |
| 3062 |
+")Lorg/mozilla/javascript/Scriptable;", |
| 3063 |
ACC_PRIVATE); |
| 3064 |
visitArrayLiteral(node, node.getFirstChild(), true); |
| 3065 |
cfw.add(ByteCode.ARETURN); |
| 3066 |
cfw.stopMethod((short)(localsMax + 1)); |
| 3067 |
} |
| 3068 |
|
| 3069 |
private void generateObjectLiteralFactory(Node node, int count) { |
| 3070 |
String methodName = codegen.getBodyMethodName(scriptOrFn) + "_literal" + count; |
| 3071 |
initBodyGeneration(); |
| 3072 |
argsLocal = firstFreeLocal++; |
| 3073 |
localsMax = firstFreeLocal; |
| 3074 |
cfw.startMethod(methodName, "(Lorg/mozilla/javascript/Context;" |
| 3075 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3076 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3077 |
+"[Ljava/lang/Object;" |
| 3078 |
+")Lorg/mozilla/javascript/Scriptable;", |
| 3079 |
ACC_PRIVATE); |
| 3080 |
visitObjectLiteral(node, node.getFirstChild(), true); |
| 3081 |
cfw.add(ByteCode.ARETURN); |
| 3082 |
cfw.stopMethod((short)(localsMax + 1)); |
| 3083 |
} |
| 3084 |
|
| 3085 |
|
| 3086 |
private void visitArrayLiteral(Node node, Node child, boolean topLevel) |
| 3087 |
{ |
| 3088 |
int count = 0; |
| 3089 |
for (Node cursor = child; cursor != null; cursor = cursor.getNext()) { |
| 3090 |
++count; |
| 3091 |
} |
| 3092 |
|
| 3093 |
|
| 3094 |
if (!topLevel && (count > 10 || cfw.getCurrentCodeOffset() > 30000) |
| 3095 |
&& !hasVarsInRegs && !isGenerator && !inLocalBlock) { |
| 3096 |
if (literals == null) { |
| 3097 |
literals = new LinkedList<Node>(); |
| 3098 |
} |
| 3099 |
literals.add(node); |
| 3100 |
String methodName = codegen.getBodyMethodName(scriptOrFn) + "_literal" + literals.size(); |
| 3101 |
cfw.addALoad(funObjLocal); |
| 3102 |
cfw.addALoad(contextLocal); |
| 3103 |
cfw.addALoad(variableObjectLocal); |
| 3104 |
cfw.addALoad(thisObjLocal); |
| 3105 |
cfw.addALoad(argsLocal); |
| 3106 |
cfw.addInvoke(ByteCode.INVOKEVIRTUAL, codegen.mainClassName, methodName, |
| 3107 |
"(Lorg/mozilla/javascript/Context;" |
| 3108 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3109 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3110 |
+"[Ljava/lang/Object;" |
| 3111 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 3112 |
return; |
| 3113 |
} |
| 3114 |
|
| 3115 |
|
| 3116 |
if (isGenerator) { |
| 3117 |
|
| 3118 |
|
| 3119 |
for (int i = 0; i != count; ++i) { |
| 3120 |
generateExpression(child, node); |
| 3121 |
child = child.getNext(); |
| 3122 |
} |
| 3123 |
addNewObjectArray(count); |
| 3124 |
for (int i = 0; i != count; ++i) { |
| 3125 |
cfw.add(ByteCode.DUP_X1); |
| 3126 |
cfw.add(ByteCode.SWAP); |
| 3127 |
cfw.addPush(count - i - 1); |
| 3128 |
cfw.add(ByteCode.SWAP); |
| 3129 |
cfw.add(ByteCode.AASTORE); |
| 3130 |
} |
| 3131 |
} else { |
| 3132 |
addNewObjectArray(count); |
| 3133 |
for (int i = 0; i != count; ++i) { |
| 3134 |
cfw.add(ByteCode.DUP); |
| 3135 |
cfw.addPush(i); |
| 3136 |
generateExpression(child, node); |
| 3137 |
cfw.add(ByteCode.AASTORE); |
| 3138 |
child = child.getNext(); |
| 3139 |
} |
| 3140 |
} |
| 3141 |
int[] skipIndexes = (int[])node.getProp(Node.SKIP_INDEXES_PROP); |
| 3142 |
if (skipIndexes == null) { |
| 3143 |
cfw.add(ByteCode.ACONST_NULL); |
| 3144 |
cfw.add(ByteCode.ICONST_0); |
| 3145 |
} else { |
| 3146 |
cfw.addPush(OptRuntime.encodeIntArray(skipIndexes)); |
| 3147 |
cfw.addPush(skipIndexes.length); |
| 3148 |
} |
| 3149 |
cfw.addALoad(contextLocal); |
| 3150 |
cfw.addALoad(variableObjectLocal); |
| 3151 |
addOptRuntimeInvoke("newArrayLiteral", |
| 3152 |
"([Ljava/lang/Object;" |
| 3153 |
+"Ljava/lang/String;" |
| 3154 |
+"I" |
| 3155 |
+"Lorg/mozilla/javascript/Context;" |
| 3156 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3157 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 3158 |
} |
| 3159 |
|
| 3160 |
|
| 3161 |
private void addLoadPropertyIds(Object[] properties, int count) { |
| 3162 |
addNewObjectArray(count); |
| 3163 |
for (int i = 0; i != count; ++i) { |
| 3164 |
cfw.add(ByteCode.DUP); |
| 3165 |
cfw.addPush(i); |
| 3166 |
Object id = properties[i]; |
| 3167 |
if (id instanceof String) { |
| 3168 |
cfw.addPush((String)id); |
| 3169 |
} else { |
| 3170 |
cfw.addPush(((Integer)id).intValue()); |
| 3171 |
addScriptRuntimeInvoke("wrapInt", "(I)Ljava/lang/Integer;"); |
| 3172 |
} |
| 3173 |
cfw.add(ByteCode.AASTORE); |
| 3174 |
} |
| 3175 |
} |
| 3176 |
|
| 3177 |
|
| 3178 |
private void addLoadPropertyValues(Node node, Node child, int count) { |
| 3179 |
if (isGenerator) { |
| 3180 |
|
| 3181 |
for (int i = 0; i != count; ++i) { |
| 3182 |
int childType = child.getType(); |
| 3183 |
if (childType == Token.GET || childType == Token.SET || childType == Token.METHOD) { |
| 3184 |
generateExpression(child.getFirstChild(), node); |
| 3185 |
} else { |
| 3186 |
generateExpression(child, node); |
| 3187 |
} |
| 3188 |
child = child.getNext(); |
| 3189 |
} |
| 3190 |
addNewObjectArray(count); |
| 3191 |
for (int i = 0; i != count; ++i) { |
| 3192 |
cfw.add(ByteCode.DUP_X1); |
| 3193 |
cfw.add(ByteCode.SWAP); |
| 3194 |
cfw.addPush(count - i - 1); |
| 3195 |
cfw.add(ByteCode.SWAP); |
| 3196 |
cfw.add(ByteCode.AASTORE); |
| 3197 |
} |
| 3198 |
} else { |
| 3199 |
addNewObjectArray(count); |
| 3200 |
Node child2 = child; |
| 3201 |
for (int i = 0; i != count; ++i) { |
| 3202 |
cfw.add(ByteCode.DUP); |
| 3203 |
cfw.addPush(i); |
| 3204 |
int childType = child2.getType(); |
| 3205 |
if (childType == Token.GET || childType == Token.SET || childType == Token.METHOD) { |
| 3206 |
generateExpression(child2.getFirstChild(), node); |
| 3207 |
} else { |
| 3208 |
generateExpression(child2, node); |
| 3209 |
} |
| 3210 |
cfw.add(ByteCode.AASTORE); |
| 3211 |
child2 = child2.getNext(); |
| 3212 |
} |
| 3213 |
} |
| 3214 |
} |
| 3215 |
|
| 3216 |
private void visitObjectLiteral(Node node, Node child, boolean topLevel) |
| 3217 |
{ |
| 3218 |
Object[] properties = (Object[])node.getProp(Node.OBJECT_IDS_PROP); |
| 3219 |
int count = properties.length; |
| 3220 |
|
| 3221 |
|
| 3222 |
if (!topLevel && (count > 10 || cfw.getCurrentCodeOffset() > 30000) |
| 3223 |
&& !hasVarsInRegs && !isGenerator && !inLocalBlock) { |
| 3224 |
if (literals == null) { |
| 3225 |
literals = new LinkedList<Node>(); |
| 3226 |
} |
| 3227 |
literals.add(node); |
| 3228 |
String methodName = codegen.getBodyMethodName(scriptOrFn) + "_literal" + literals.size(); |
| 3229 |
cfw.addALoad(funObjLocal); |
| 3230 |
cfw.addALoad(contextLocal); |
| 3231 |
cfw.addALoad(variableObjectLocal); |
| 3232 |
cfw.addALoad(thisObjLocal); |
| 3233 |
cfw.addALoad(argsLocal); |
| 3234 |
cfw.addInvoke(ByteCode.INVOKEVIRTUAL, codegen.mainClassName, methodName, |
| 3235 |
"(Lorg/mozilla/javascript/Context;" |
| 3236 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3237 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3238 |
+"[Ljava/lang/Object;" |
| 3239 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 3240 |
return; |
| 3241 |
} |
| 3242 |
|
| 3243 |
if (isGenerator) { |
| 3244 |
|
| 3245 |
|
| 3246 |
addLoadPropertyValues(node, child, count); |
| 3247 |
addLoadPropertyIds(properties, count); |
| 3248 |
|
| 3249 |
cfw.add(ByteCode.SWAP); |
| 3250 |
} else { |
| 3251 |
addLoadPropertyIds(properties, count); |
| 3252 |
addLoadPropertyValues(node, child, count); |
| 3253 |
} |
| 3254 |
|
| 3255 |
|
| 3256 |
boolean hasGetterSetters = false; |
| 3257 |
Node child2 = child; |
| 3258 |
for (int i = 0; i != count; ++i) { |
| 3259 |
int childType = child2.getType(); |
| 3260 |
if (childType == Token.GET || childType == Token.SET) { |
| 3261 |
hasGetterSetters = true; |
| 3262 |
break; |
| 3263 |
} |
| 3264 |
child2 = child2.getNext(); |
| 3265 |
} |
| 3266 |
|
| 3267 |
if (hasGetterSetters) { |
| 3268 |
cfw.addPush(count); |
| 3269 |
cfw.add(ByteCode.NEWARRAY, ByteCode.T_INT); |
| 3270 |
child2 = child; |
| 3271 |
for (int i = 0; i != count; ++i) { |
| 3272 |
cfw.add(ByteCode.DUP); |
| 3273 |
cfw.addPush(i); |
| 3274 |
int childType = child2.getType(); |
| 3275 |
if (childType == Token.GET) { |
| 3276 |
cfw.add(ByteCode.ICONST_M1); |
| 3277 |
} else if (childType == Token.SET) { |
| 3278 |
cfw.add(ByteCode.ICONST_1); |
| 3279 |
} else { |
| 3280 |
cfw.add(ByteCode.ICONST_0); |
| 3281 |
} |
| 3282 |
cfw.add(ByteCode.IASTORE); |
| 3283 |
child2 = child2.getNext(); |
| 3284 |
} |
| 3285 |
} else { |
| 3286 |
cfw.add(ByteCode.ACONST_NULL); |
| 3287 |
} |
| 3288 |
|
| 3289 |
cfw.addALoad(contextLocal); |
| 3290 |
cfw.addALoad(variableObjectLocal); |
| 3291 |
addScriptRuntimeInvoke("newObjectLiteral", |
| 3292 |
"([Ljava/lang/Object;" |
| 3293 |
+"[Ljava/lang/Object;" |
| 3294 |
+"[I" |
| 3295 |
+"Lorg/mozilla/javascript/Context;" |
| 3296 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3297 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 3298 |
} |
| 3299 |
|
| 3300 |
private void visitSpecialCall(Node node, int type, int specialType, |
| 3301 |
Node child) |
| 3302 |
{ |
| 3303 |
cfw.addALoad(contextLocal); |
| 3304 |
|
| 3305 |
if (type == Token.NEW) { |
| 3306 |
generateExpression(child, node); |
| 3307 |
|
| 3308 |
} else { |
| 3309 |
generateFunctionAndThisObj(child, node); |
| 3310 |
|
| 3311 |
} |
| 3312 |
child = child.getNext(); |
| 3313 |
|
| 3314 |
generateCallArgArray(node, child, false); |
| 3315 |
|
| 3316 |
String methodName; |
| 3317 |
String callSignature; |
| 3318 |
|
| 3319 |
if (type == Token.NEW) { |
| 3320 |
methodName = "newObjectSpecial"; |
| 3321 |
callSignature = "(Lorg/mozilla/javascript/Context;" |
| 3322 |
+"Ljava/lang/Object;" |
| 3323 |
+"[Ljava/lang/Object;" |
| 3324 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3325 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3326 |
+"I" |
| 3327 |
+")Ljava/lang/Object;"; |
| 3328 |
cfw.addALoad(variableObjectLocal); |
| 3329 |
cfw.addALoad(thisObjLocal); |
| 3330 |
cfw.addPush(specialType); |
| 3331 |
} else { |
| 3332 |
methodName = "callSpecial"; |
| 3333 |
callSignature = "(Lorg/mozilla/javascript/Context;" |
| 3334 |
+"Lorg/mozilla/javascript/Callable;" |
| 3335 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3336 |
+"[Ljava/lang/Object;" |
| 3337 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3338 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3339 |
+"I" |
| 3340 |
+"Ljava/lang/String;I" |
| 3341 |
+")Ljava/lang/Object;"; |
| 3342 |
cfw.addALoad(variableObjectLocal); |
| 3343 |
cfw.addALoad(thisObjLocal); |
| 3344 |
cfw.addPush(specialType); |
| 3345 |
String sourceName = scriptOrFn.getSourceName(); |
| 3346 |
cfw.addPush(sourceName == null ? "" : sourceName); |
| 3347 |
cfw.addPush(itsLineNumber); |
| 3348 |
} |
| 3349 |
|
| 3350 |
addOptRuntimeInvoke(methodName, callSignature); |
| 3351 |
} |
| 3352 |
|
| 3353 |
private void visitStandardCall(Node node, Node child) |
| 3354 |
{ |
| 3355 |
if (node.getType() != Token.CALL) throw Codegen.badTree(); |
| 3356 |
|
| 3357 |
Node firstArgChild = child.getNext(); |
| 3358 |
int childType = child.getType(); |
| 3359 |
|
| 3360 |
String methodName; |
| 3361 |
String signature; |
| 3362 |
|
| 3363 |
if (firstArgChild == null) { |
| 3364 |
if (childType == Token.NAME) { |
| 3365 |
|
| 3366 |
String name = child.getString(); |
| 3367 |
cfw.addPush(name); |
| 3368 |
methodName = "callName0"; |
| 3369 |
signature = "(Ljava/lang/String;" |
| 3370 |
+"Lorg/mozilla/javascript/Context;" |
| 3371 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3372 |
+")Ljava/lang/Object;"; |
| 3373 |
} else if (childType == Token.GETPROP) { |
| 3374 |
|
| 3375 |
Node propTarget = child.getFirstChild(); |
| 3376 |
generateExpression(propTarget, node); |
| 3377 |
Node id = propTarget.getNext(); |
| 3378 |
String property = id.getString(); |
| 3379 |
cfw.addPush(property); |
| 3380 |
methodName = "callProp0"; |
| 3381 |
signature = "(Ljava/lang/Object;" |
| 3382 |
+"Ljava/lang/String;" |
| 3383 |
+"Lorg/mozilla/javascript/Context;" |
| 3384 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3385 |
+")Ljava/lang/Object;"; |
| 3386 |
} else if (childType == Token.GETPROPNOWARN) { |
| 3387 |
throw Kit.codeBug(); |
| 3388 |
} else { |
| 3389 |
generateFunctionAndThisObj(child, node); |
| 3390 |
methodName = "call0"; |
| 3391 |
signature = "(Lorg/mozilla/javascript/Callable;" |
| 3392 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3393 |
+"Lorg/mozilla/javascript/Context;" |
| 3394 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3395 |
+")Ljava/lang/Object;"; |
| 3396 |
} |
| 3397 |
|
| 3398 |
} else if (childType == Token.NAME) { |
| 3399 |
|
| 3400 |
|
| 3401 |
|
| 3402 |
|
| 3403 |
String name = child.getString(); |
| 3404 |
generateCallArgArray(node, firstArgChild, false); |
| 3405 |
cfw.addPush(name); |
| 3406 |
methodName = "callName"; |
| 3407 |
signature = "([Ljava/lang/Object;" |
| 3408 |
+"Ljava/lang/String;" |
| 3409 |
+"Lorg/mozilla/javascript/Context;" |
| 3410 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3411 |
+")Ljava/lang/Object;"; |
| 3412 |
} else { |
| 3413 |
int argCount = 0; |
| 3414 |
for (Node arg = firstArgChild; arg != null; arg = arg.getNext()) { |
| 3415 |
++argCount; |
| 3416 |
} |
| 3417 |
generateFunctionAndThisObj(child, node); |
| 3418 |
|
| 3419 |
if (argCount == 1) { |
| 3420 |
generateExpression(firstArgChild, node); |
| 3421 |
methodName = "call1"; |
| 3422 |
signature = "(Lorg/mozilla/javascript/Callable;" |
| 3423 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3424 |
+"Ljava/lang/Object;" |
| 3425 |
+"Lorg/mozilla/javascript/Context;" |
| 3426 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3427 |
+")Ljava/lang/Object;"; |
| 3428 |
} else if (argCount == 2) { |
| 3429 |
generateExpression(firstArgChild, node); |
| 3430 |
generateExpression(firstArgChild.getNext(), node); |
| 3431 |
methodName = "call2"; |
| 3432 |
signature = "(Lorg/mozilla/javascript/Callable;" |
| 3433 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3434 |
+"Ljava/lang/Object;" |
| 3435 |
+"Ljava/lang/Object;" |
| 3436 |
+"Lorg/mozilla/javascript/Context;" |
| 3437 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3438 |
+")Ljava/lang/Object;"; |
| 3439 |
} else { |
| 3440 |
generateCallArgArray(node, firstArgChild, false); |
| 3441 |
methodName = "callN"; |
| 3442 |
signature = "(Lorg/mozilla/javascript/Callable;" |
| 3443 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3444 |
+"[Ljava/lang/Object;" |
| 3445 |
+"Lorg/mozilla/javascript/Context;" |
| 3446 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3447 |
+")Ljava/lang/Object;"; |
| 3448 |
} |
| 3449 |
} |
| 3450 |
|
| 3451 |
cfw.addALoad(contextLocal); |
| 3452 |
cfw.addALoad(variableObjectLocal); |
| 3453 |
addOptRuntimeInvoke(methodName, signature); |
| 3454 |
} |
| 3455 |
|
| 3456 |
private void visitStandardNew(Node node, Node child) |
| 3457 |
{ |
| 3458 |
if (node.getType() != Token.NEW) throw Codegen.badTree(); |
| 3459 |
|
| 3460 |
Node firstArgChild = child.getNext(); |
| 3461 |
|
| 3462 |
generateExpression(child, node); |
| 3463 |
|
| 3464 |
cfw.addALoad(contextLocal); |
| 3465 |
cfw.addALoad(variableObjectLocal); |
| 3466 |
|
| 3467 |
generateCallArgArray(node, firstArgChild, false); |
| 3468 |
addScriptRuntimeInvoke( |
| 3469 |
"newObject", |
| 3470 |
"(Ljava/lang/Object;" |
| 3471 |
+"Lorg/mozilla/javascript/Context;" |
| 3472 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3473 |
+"[Ljava/lang/Object;" |
| 3474 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 3475 |
} |
| 3476 |
|
| 3477 |
private void visitOptimizedCall(Node node, OptFunctionNode target, |
| 3478 |
int type, Node child) |
| 3479 |
{ |
| 3480 |
Node firstArgChild = child.getNext(); |
| 3481 |
String className = codegen.mainClassName; |
| 3482 |
|
| 3483 |
short thisObjLocal = 0; |
| 3484 |
if (type == Token.NEW) { |
| 3485 |
generateExpression(child, node); |
| 3486 |
} else { |
| 3487 |
generateFunctionAndThisObj(child, node); |
| 3488 |
thisObjLocal = getNewWordLocal(); |
| 3489 |
cfw.addAStore(thisObjLocal); |
| 3490 |
} |
| 3491 |
|
| 3492 |
|
| 3493 |
int beyond = cfw.acquireLabel(); |
| 3494 |
int regularCall = cfw.acquireLabel(); |
| 3495 |
|
| 3496 |
cfw.add(ByteCode.DUP); |
| 3497 |
cfw.add(ByteCode.INSTANCEOF, className); |
| 3498 |
cfw.add(ByteCode.IFEQ, regularCall); |
| 3499 |
cfw.add(ByteCode.CHECKCAST, className); |
| 3500 |
cfw.add(ByteCode.DUP); |
| 3501 |
cfw.add(ByteCode.GETFIELD, className, Codegen.ID_FIELD_NAME, "I"); |
| 3502 |
cfw.addPush(codegen.getIndex(target.fnode)); |
| 3503 |
cfw.add(ByteCode.IF_ICMPNE, regularCall); |
| 3504 |
|
| 3505 |
|
| 3506 |
cfw.addALoad(contextLocal); |
| 3507 |
cfw.addALoad(variableObjectLocal); |
| 3508 |
|
| 3509 |
|
| 3510 |
if (type == Token.NEW) { |
| 3511 |
cfw.add(ByteCode.ACONST_NULL); |
| 3512 |
} else { |
| 3513 |
cfw.addALoad(thisObjLocal); |
| 3514 |
} |
| 3515 |
|
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
|
| 3520 |
|
| 3521 |
|
| 3522 |
|
| 3523 |
Node argChild = firstArgChild; |
| 3524 |
while (argChild != null) { |
| 3525 |
int dcp_register = nodeIsDirectCallParameter(argChild); |
| 3526 |
if (dcp_register >= 0) { |
| 3527 |
cfw.addALoad(dcp_register); |
| 3528 |
cfw.addDLoad(dcp_register + 1); |
| 3529 |
} else if (argChild.getIntProp(Node.ISNUMBER_PROP, -1) |
| 3530 |
== Node.BOTH) |
| 3531 |
{ |
| 3532 |
cfw.add(ByteCode.GETSTATIC, |
| 3533 |
"java/lang/Void", |
| 3534 |
"TYPE", |
| 3535 |
"Ljava/lang/Class;"); |
| 3536 |
generateExpression(argChild, node); |
| 3537 |
} else { |
| 3538 |
generateExpression(argChild, node); |
| 3539 |
cfw.addPush(0.0); |
| 3540 |
} |
| 3541 |
argChild = argChild.getNext(); |
| 3542 |
} |
| 3543 |
|
| 3544 |
cfw.add(ByteCode.GETSTATIC, |
| 3545 |
"org/mozilla/javascript/ScriptRuntime", |
| 3546 |
"emptyArgs", "[Ljava/lang/Object;"); |
| 3547 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 3548 |
codegen.mainClassName, |
| 3549 |
(type == Token.NEW) |
| 3550 |
? codegen.getDirectCtorName(target.fnode) |
| 3551 |
: codegen.getBodyMethodName(target.fnode), |
| 3552 |
codegen.getBodyMethodSignature(target.fnode)); |
| 3553 |
|
| 3554 |
cfw.add(ByteCode.GOTO, beyond); |
| 3555 |
|
| 3556 |
cfw.markLabel(regularCall); |
| 3557 |
|
| 3558 |
cfw.addALoad(contextLocal); |
| 3559 |
cfw.addALoad(variableObjectLocal); |
| 3560 |
|
| 3561 |
if (type != Token.NEW) { |
| 3562 |
cfw.addALoad(thisObjLocal); |
| 3563 |
releaseWordLocal(thisObjLocal); |
| 3564 |
|
| 3565 |
} |
| 3566 |
|
| 3567 |
|
| 3568 |
generateCallArgArray(node, firstArgChild, true); |
| 3569 |
|
| 3570 |
if (type == Token.NEW) { |
| 3571 |
addScriptRuntimeInvoke( |
| 3572 |
"newObject", |
| 3573 |
"(Ljava/lang/Object;" |
| 3574 |
+"Lorg/mozilla/javascript/Context;" |
| 3575 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3576 |
+"[Ljava/lang/Object;" |
| 3577 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 3578 |
} else { |
| 3579 |
cfw.addInvoke(ByteCode.INVOKEINTERFACE, |
| 3580 |
"org/mozilla/javascript/Callable", |
| 3581 |
"call", |
| 3582 |
"(Lorg/mozilla/javascript/Context;" |
| 3583 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3584 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3585 |
+"[Ljava/lang/Object;" |
| 3586 |
+")Ljava/lang/Object;"); |
| 3587 |
} |
| 3588 |
|
| 3589 |
cfw.markLabel(beyond); |
| 3590 |
} |
| 3591 |
|
| 3592 |
private void generateCallArgArray(Node node, Node argChild, boolean directCall) |
| 3593 |
{ |
| 3594 |
int argCount = 0; |
| 3595 |
for (Node child = argChild; child != null; child = child.getNext()) { |
| 3596 |
++argCount; |
| 3597 |
} |
| 3598 |
|
| 3599 |
if (argCount == 1 && itsOneArgArray >= 0) { |
| 3600 |
cfw.addALoad(itsOneArgArray); |
| 3601 |
} else { |
| 3602 |
addNewObjectArray(argCount); |
| 3603 |
} |
| 3604 |
|
| 3605 |
for (int i = 0; i != argCount; ++i) { |
| 3606 |
|
| 3607 |
|
| 3608 |
|
| 3609 |
if (!isGenerator) { |
| 3610 |
cfw.add(ByteCode.DUP); |
| 3611 |
cfw.addPush(i); |
| 3612 |
} |
| 3613 |
|
| 3614 |
if (!directCall) { |
| 3615 |
generateExpression(argChild, node); |
| 3616 |
} else { |
| 3617 |
|
| 3618 |
|
| 3619 |
|
| 3620 |
|
| 3621 |
|
| 3622 |
int dcp_register = nodeIsDirectCallParameter(argChild); |
| 3623 |
if (dcp_register >= 0) { |
| 3624 |
dcpLoadAsObject(dcp_register); |
| 3625 |
} else { |
| 3626 |
generateExpression(argChild, node); |
| 3627 |
int childNumberFlag |
| 3628 |
= argChild.getIntProp(Node.ISNUMBER_PROP, -1); |
| 3629 |
if (childNumberFlag == Node.BOTH) { |
| 3630 |
addDoubleWrap(); |
| 3631 |
} |
| 3632 |
} |
| 3633 |
} |
| 3634 |
|
| 3635 |
|
| 3636 |
|
| 3637 |
|
| 3638 |
if (isGenerator) { |
| 3639 |
short tempLocal = getNewWordLocal(); |
| 3640 |
cfw.addAStore(tempLocal); |
| 3641 |
cfw.add(ByteCode.CHECKCAST, "[Ljava/lang/Object;"); |
| 3642 |
cfw.add(ByteCode.DUP); |
| 3643 |
cfw.addPush(i); |
| 3644 |
cfw.addALoad(tempLocal); |
| 3645 |
releaseWordLocal(tempLocal); |
| 3646 |
} |
| 3647 |
|
| 3648 |
cfw.add(ByteCode.AASTORE); |
| 3649 |
|
| 3650 |
argChild = argChild.getNext(); |
| 3651 |
} |
| 3652 |
} |
| 3653 |
|
| 3654 |
private void generateFunctionAndThisObj(Node node, Node parent) |
| 3655 |
{ |
| 3656 |
|
| 3657 |
int type = node.getType(); |
| 3658 |
switch (node.getType()) { |
| 3659 |
case Token.GETPROPNOWARN: |
| 3660 |
throw Kit.codeBug(); |
| 3661 |
|
| 3662 |
case Token.GETPROP: |
| 3663 |
case Token.GETELEM: { |
| 3664 |
Node target = node.getFirstChild(); |
| 3665 |
generateExpression(target, node); |
| 3666 |
Node id = target.getNext(); |
| 3667 |
if (type == Token.GETPROP) { |
| 3668 |
String property = id.getString(); |
| 3669 |
cfw.addPush(property); |
| 3670 |
cfw.addALoad(contextLocal); |
| 3671 |
cfw.addALoad(variableObjectLocal); |
| 3672 |
addScriptRuntimeInvoke( |
| 3673 |
"getPropFunctionAndThis", |
| 3674 |
"(Ljava/lang/Object;" |
| 3675 |
+"Ljava/lang/String;" |
| 3676 |
+"Lorg/mozilla/javascript/Context;" |
| 3677 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3678 |
+")Lorg/mozilla/javascript/Callable;"); |
| 3679 |
} else { |
| 3680 |
generateExpression(id, node); |
| 3681 |
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) |
| 3682 |
addDoubleWrap(); |
| 3683 |
cfw.addALoad(contextLocal); |
| 3684 |
cfw.addALoad(variableObjectLocal); |
| 3685 |
addScriptRuntimeInvoke( |
| 3686 |
"getElemFunctionAndThis", |
| 3687 |
"(Ljava/lang/Object;" |
| 3688 |
+"Ljava/lang/Object;" |
| 3689 |
+"Lorg/mozilla/javascript/Context;" |
| 3690 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3691 |
+")Lorg/mozilla/javascript/Callable;"); |
| 3692 |
} |
| 3693 |
break; |
| 3694 |
} |
| 3695 |
|
| 3696 |
case Token.NAME: { |
| 3697 |
String name = node.getString(); |
| 3698 |
cfw.addPush(name); |
| 3699 |
cfw.addALoad(contextLocal); |
| 3700 |
cfw.addALoad(variableObjectLocal); |
| 3701 |
addScriptRuntimeInvoke( |
| 3702 |
"getNameFunctionAndThis", |
| 3703 |
"(Ljava/lang/String;" |
| 3704 |
+"Lorg/mozilla/javascript/Context;" |
| 3705 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 3706 |
+")Lorg/mozilla/javascript/Callable;"); |
| 3707 |
break; |
| 3708 |
} |
| 3709 |
|
| 3710 |
default: |
| 3711 |
generateExpression(node, parent); |
| 3712 |
cfw.addALoad(contextLocal); |
| 3713 |
addScriptRuntimeInvoke( |
| 3714 |
"getValueFunctionAndThis", |
| 3715 |
"(Ljava/lang/Object;" |
| 3716 |
+"Lorg/mozilla/javascript/Context;" |
| 3717 |
+")Lorg/mozilla/javascript/Callable;"); |
| 3718 |
break; |
| 3719 |
} |
| 3720 |
|
| 3721 |
cfw.addALoad(contextLocal); |
| 3722 |
addScriptRuntimeInvoke( |
| 3723 |
"lastStoredScriptable", |
| 3724 |
"(Lorg/mozilla/javascript/Context;" |
| 3725 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 3726 |
} |
| 3727 |
|
| 3728 |
private void updateLineNumber(Node node) |
| 3729 |
{ |
| 3730 |
itsLineNumber = node.getLineno(); |
| 3731 |
if (itsLineNumber == -1) |
| 3732 |
return; |
| 3733 |
cfw.addLineNumberEntry((short)itsLineNumber); |
| 3734 |
} |
| 3735 |
|
| 3736 |
private void visitTryCatchFinally(Jump node, Node child) |
| 3737 |
{ |
| 3738 |
|
| 3739 |
|
| 3740 |
|
| 3741 |
|
| 3742 |
|
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
|
| 3747 |
|
| 3748 |
|
| 3749 |
|
| 3750 |
short savedVariableObject = getNewWordLocal(); |
| 3751 |
cfw.addALoad(variableObjectLocal); |
| 3752 |
cfw.addAStore(savedVariableObject); |
| 3753 |
|
| 3754 |
|
| 3755 |
|
| 3756 |
|
| 3757 |
|
| 3758 |
|
| 3759 |
int startLabel = cfw.acquireLabel(); |
| 3760 |
cfw.markLabel(startLabel, (short)0); |
| 3761 |
|
| 3762 |
Node catchTarget = node.target; |
| 3763 |
Node finallyTarget = node.getFinally(); |
| 3764 |
int[] handlerLabels = new int[EXCEPTION_MAX]; |
| 3765 |
|
| 3766 |
exceptionManager.pushExceptionInfo(node); |
| 3767 |
if (catchTarget != null) { |
| 3768 |
handlerLabels[JAVASCRIPT_EXCEPTION] = cfw.acquireLabel(); |
| 3769 |
handlerLabels[EVALUATOR_EXCEPTION] = cfw.acquireLabel(); |
| 3770 |
handlerLabels[ECMAERROR_EXCEPTION] = cfw.acquireLabel(); |
| 3771 |
Context cx = Context.getCurrentContext(); |
| 3772 |
if (cx != null && |
| 3773 |
cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS)) { |
| 3774 |
handlerLabels[THROWABLE_EXCEPTION] = cfw.acquireLabel(); |
| 3775 |
} |
| 3776 |
} |
| 3777 |
if (finallyTarget != null) { |
| 3778 |
handlerLabels[FINALLY_EXCEPTION] = cfw.acquireLabel(); |
| 3779 |
} |
| 3780 |
exceptionManager.setHandlers(handlerLabels, startLabel); |
| 3781 |
|
| 3782 |
|
| 3783 |
if (isGenerator && finallyTarget != null) { |
| 3784 |
FinallyReturnPoint ret = new FinallyReturnPoint(); |
| 3785 |
if (finallys == null) { |
| 3786 |
finallys = new HashMap<Node,FinallyReturnPoint>(); |
| 3787 |
} |
| 3788 |
|
| 3789 |
finallys.put(finallyTarget, ret); |
| 3790 |
|
| 3791 |
finallys.put(finallyTarget.getNext(), ret); |
| 3792 |
} |
| 3793 |
|
| 3794 |
while (child != null) { |
| 3795 |
if (child == catchTarget) { |
| 3796 |
int catchLabel = getTargetLabel(catchTarget); |
| 3797 |
exceptionManager.removeHandler(JAVASCRIPT_EXCEPTION, |
| 3798 |
catchLabel); |
| 3799 |
exceptionManager.removeHandler(EVALUATOR_EXCEPTION, |
| 3800 |
catchLabel); |
| 3801 |
exceptionManager.removeHandler(ECMAERROR_EXCEPTION, |
| 3802 |
catchLabel); |
| 3803 |
exceptionManager.removeHandler(THROWABLE_EXCEPTION, |
| 3804 |
catchLabel); |
| 3805 |
} |
| 3806 |
generateStatement(child); |
| 3807 |
child = child.getNext(); |
| 3808 |
} |
| 3809 |
|
| 3810 |
|
| 3811 |
int realEnd = cfw.acquireLabel(); |
| 3812 |
cfw.add(ByteCode.GOTO, realEnd); |
| 3813 |
|
| 3814 |
int exceptionLocal = getLocalBlockRegister(node); |
| 3815 |
|
| 3816 |
|
| 3817 |
if (catchTarget != null) { |
| 3818 |
|
| 3819 |
int catchLabel = catchTarget.labelId(); |
| 3820 |
|
| 3821 |
|
| 3822 |
|
| 3823 |
|
| 3824 |
|
| 3825 |
|
| 3826 |
generateCatchBlock(JAVASCRIPT_EXCEPTION, savedVariableObject, |
| 3827 |
catchLabel, exceptionLocal, |
| 3828 |
handlerLabels[JAVASCRIPT_EXCEPTION]); |
| 3829 |
|
| 3830 |
|
| 3831 |
|
| 3832 |
|
| 3833 |
generateCatchBlock(EVALUATOR_EXCEPTION, savedVariableObject, |
| 3834 |
catchLabel, exceptionLocal, |
| 3835 |
handlerLabels[EVALUATOR_EXCEPTION]); |
| 3836 |
|
| 3837 |
|
| 3838 |
|
| 3839 |
|
| 3840 |
|
| 3841 |
generateCatchBlock(ECMAERROR_EXCEPTION, savedVariableObject, |
| 3842 |
catchLabel, exceptionLocal, |
| 3843 |
handlerLabels[ECMAERROR_EXCEPTION]); |
| 3844 |
|
| 3845 |
Context cx = Context.getCurrentContext(); |
| 3846 |
if (cx != null && |
| 3847 |
cx.hasFeature(Context.FEATURE_ENHANCED_JAVA_ACCESS)) |
| 3848 |
{ |
| 3849 |
generateCatchBlock(THROWABLE_EXCEPTION, savedVariableObject, |
| 3850 |
catchLabel, exceptionLocal, |
| 3851 |
handlerLabels[THROWABLE_EXCEPTION]); |
| 3852 |
} |
| 3853 |
} |
| 3854 |
|
| 3855 |
|
| 3856 |
|
| 3857 |
if (finallyTarget != null) { |
| 3858 |
int finallyHandler = cfw.acquireLabel(); |
| 3859 |
int finallyEnd = cfw.acquireLabel(); |
| 3860 |
cfw.markHandler(finallyHandler); |
| 3861 |
if (!isGenerator) { |
| 3862 |
cfw.markLabel(handlerLabels[FINALLY_EXCEPTION]); |
| 3863 |
} |
| 3864 |
cfw.addAStore(exceptionLocal); |
| 3865 |
|
| 3866 |
|
| 3867 |
cfw.addALoad(savedVariableObject); |
| 3868 |
cfw.addAStore(variableObjectLocal); |
| 3869 |
|
| 3870 |
|
| 3871 |
int finallyLabel = finallyTarget.labelId(); |
| 3872 |
if (isGenerator) |
| 3873 |
addGotoWithReturn(finallyTarget); |
| 3874 |
else { |
| 3875 |
inlineFinally(finallyTarget, handlerLabels[FINALLY_EXCEPTION], |
| 3876 |
finallyEnd); |
| 3877 |
} |
| 3878 |
|
| 3879 |
|
| 3880 |
cfw.addALoad(exceptionLocal); |
| 3881 |
if (isGenerator) |
| 3882 |
cfw.add(ByteCode.CHECKCAST, "java/lang/Throwable"); |
| 3883 |
cfw.add(ByteCode.ATHROW); |
| 3884 |
|
| 3885 |
cfw.markLabel(finallyEnd); |
| 3886 |
|
| 3887 |
if (isGenerator) { |
| 3888 |
cfw.addExceptionHandler(startLabel, finallyLabel, |
| 3889 |
finallyHandler, null); |
| 3890 |
} |
| 3891 |
} |
| 3892 |
releaseWordLocal(savedVariableObject); |
| 3893 |
cfw.markLabel(realEnd); |
| 3894 |
|
| 3895 |
if (!isGenerator) { |
| 3896 |
exceptionManager.popExceptionInfo(); |
| 3897 |
} |
| 3898 |
} |
| 3899 |
|
| 3900 |
private static final int JAVASCRIPT_EXCEPTION = 0; |
| 3901 |
private static final int EVALUATOR_EXCEPTION = 1; |
| 3902 |
private static final int ECMAERROR_EXCEPTION = 2; |
| 3903 |
private static final int THROWABLE_EXCEPTION = 3; |
| 3904 |
|
| 3905 |
|
| 3906 |
|
| 3907 |
private static final int FINALLY_EXCEPTION = 4; |
| 3908 |
private static final int EXCEPTION_MAX = 5; |
| 3909 |
|
| 3910 |
private void generateCatchBlock(int exceptionType, |
| 3911 |
short savedVariableObject, |
| 3912 |
int catchLabel, |
| 3913 |
int exceptionLocal, |
| 3914 |
int handler) |
| 3915 |
{ |
| 3916 |
if (handler == 0) { |
| 3917 |
handler = cfw.acquireLabel(); |
| 3918 |
} |
| 3919 |
cfw.markHandler(handler); |
| 3920 |
|
| 3921 |
|
| 3922 |
cfw.addAStore(exceptionLocal); |
| 3923 |
|
| 3924 |
|
| 3925 |
cfw.addALoad(savedVariableObject); |
| 3926 |
cfw.addAStore(variableObjectLocal); |
| 3927 |
|
| 3928 |
String exceptionName = exceptionTypeToName(exceptionType); |
| 3929 |
|
| 3930 |
cfw.add(ByteCode.GOTO, catchLabel); |
| 3931 |
} |
| 3932 |
|
| 3933 |
private String exceptionTypeToName(int exceptionType) |
| 3934 |
{ |
| 3935 |
if (exceptionType == JAVASCRIPT_EXCEPTION) { |
| 3936 |
return "org/mozilla/javascript/JavaScriptException"; |
| 3937 |
} else if (exceptionType == EVALUATOR_EXCEPTION) { |
| 3938 |
return "org/mozilla/javascript/EvaluatorException"; |
| 3939 |
} else if (exceptionType == ECMAERROR_EXCEPTION) { |
| 3940 |
return "org/mozilla/javascript/EcmaError"; |
| 3941 |
} else if (exceptionType == THROWABLE_EXCEPTION) { |
| 3942 |
return "java/lang/Throwable"; |
| 3943 |
} else if (exceptionType == FINALLY_EXCEPTION) { |
| 3944 |
return null; |
| 3945 |
} else { |
| 3946 |
throw Kit.codeBug(); |
| 3947 |
} |
| 3948 |
} |
| 3949 |
|
| 3950 |
|
| 3951 |
|
| 3952 |
|
| 3953 |
|
| 3954 |
|
| 3955 |
|
| 3956 |
|
| 3957 |
|
| 3958 |
|
| 3959 |
|
| 3960 |
|
| 3961 |
|
| 3962 |
|
| 3963 |
|
| 3964 |
|
| 3965 |
|
| 3966 |
|
| 3967 |
|
| 3968 |
|
| 3969 |
|
| 3970 |
|
| 3971 |
|
| 3972 |
|
| 3973 |
private class ExceptionManager |
| 3974 |
{ |
| 3975 |
ExceptionManager() |
| 3976 |
{ |
| 3977 |
exceptionInfo = new LinkedList<ExceptionInfo>(); |
| 3978 |
} |
| 3979 |
|
| 3980 |
|
| 3981 |
|
| 3982 |
|
| 3983 |
|
| 3984 |
|
| 3985 |
|
| 3986 |
void pushExceptionInfo(Jump node) |
| 3987 |
{ |
| 3988 |
Node fBlock = getFinallyAtTarget(node.getFinally()); |
| 3989 |
ExceptionInfo ei = new ExceptionInfo(node, fBlock); |
| 3990 |
exceptionInfo.add(ei); |
| 3991 |
} |
| 3992 |
|
| 3993 |
|
| 3994 |
|
| 3995 |
|
| 3996 |
|
| 3997 |
|
| 3998 |
|
| 3999 |
|
| 4000 |
|
| 4001 |
|
| 4002 |
void addHandler(int exceptionType, int handlerLabel, int startLabel) |
| 4003 |
{ |
| 4004 |
ExceptionInfo top = getTop(); |
| 4005 |
top.handlerLabels[exceptionType] = handlerLabel; |
| 4006 |
top.exceptionStarts[exceptionType] = startLabel; |
| 4007 |
} |
| 4008 |
|
| 4009 |
|
| 4010 |
|
| 4011 |
|
| 4012 |
|
| 4013 |
|
| 4014 |
|
| 4015 |
|
| 4016 |
|
| 4017 |
|
| 4018 |
|
| 4019 |
void setHandlers(int[] handlerLabels, int startLabel) |
| 4020 |
{ |
| 4021 |
ExceptionInfo top = getTop(); |
| 4022 |
for (int i = 0; i < handlerLabels.length; i++) { |
| 4023 |
if (handlerLabels[i] != 0) { |
| 4024 |
addHandler(i, handlerLabels[i], startLabel); |
| 4025 |
} |
| 4026 |
} |
| 4027 |
} |
| 4028 |
|
| 4029 |
|
| 4030 |
|
| 4031 |
|
| 4032 |
|
| 4033 |
|
| 4034 |
|
| 4035 |
|
| 4036 |
|
| 4037 |
|
| 4038 |
|
| 4039 |
int removeHandler(int exceptionType, int endLabel) |
| 4040 |
{ |
| 4041 |
ExceptionInfo top = getTop(); |
| 4042 |
if (top.handlerLabels[exceptionType] != 0) { |
| 4043 |
int handlerLabel = top.handlerLabels[exceptionType]; |
| 4044 |
endCatch(top, exceptionType, endLabel); |
| 4045 |
top.handlerLabels[exceptionType] = 0; |
| 4046 |
return handlerLabel; |
| 4047 |
} |
| 4048 |
return 0; |
| 4049 |
} |
| 4050 |
|
| 4051 |
|
| 4052 |
|
| 4053 |
|
| 4054 |
void popExceptionInfo() |
| 4055 |
{ |
| 4056 |
exceptionInfo.removeLast(); |
| 4057 |
} |
| 4058 |
|
| 4059 |
|
| 4060 |
|
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
|
| 4065 |
|
| 4066 |
|
| 4067 |
|
| 4068 |
|
| 4069 |
|
| 4070 |
|
| 4071 |
|
| 4072 |
|
| 4073 |
|
| 4074 |
|
| 4075 |
void markInlineFinallyStart(Node finallyBlock, int finallyStart) |
| 4076 |
{ |
| 4077 |
|
| 4078 |
|
| 4079 |
|
| 4080 |
|
| 4081 |
|
| 4082 |
|
| 4083 |
ListIterator<ExceptionInfo> iter = |
| 4084 |
exceptionInfo.listIterator(exceptionInfo.size()); |
| 4085 |
while (iter.hasPrevious()) { |
| 4086 |
ExceptionInfo ei = iter.previous(); |
| 4087 |
for (int i = 0; i < EXCEPTION_MAX; i++) { |
| 4088 |
if (ei.handlerLabels[i] != 0 && ei.currentFinally == null) { |
| 4089 |
endCatch(ei, i, finallyStart); |
| 4090 |
ei.exceptionStarts[i] = 0; |
| 4091 |
ei.currentFinally = finallyBlock; |
| 4092 |
} |
| 4093 |
} |
| 4094 |
if (ei.finallyBlock == finallyBlock) { |
| 4095 |
break; |
| 4096 |
} |
| 4097 |
} |
| 4098 |
} |
| 4099 |
|
| 4100 |
|
| 4101 |
|
| 4102 |
|
| 4103 |
|
| 4104 |
|
| 4105 |
|
| 4106 |
|
| 4107 |
|
| 4108 |
|
| 4109 |
|
| 4110 |
void markInlineFinallyEnd(Node finallyBlock, int finallyEnd) |
| 4111 |
{ |
| 4112 |
ListIterator<ExceptionInfo> iter = |
| 4113 |
exceptionInfo.listIterator(exceptionInfo.size()); |
| 4114 |
while (iter.hasPrevious()) { |
| 4115 |
ExceptionInfo ei = iter.previous(); |
| 4116 |
for (int i = 0; i < EXCEPTION_MAX; i++) { |
| 4117 |
if (ei.handlerLabels[i] != 0 && |
| 4118 |
ei.currentFinally == finallyBlock) { |
| 4119 |
ei.exceptionStarts[i] = finallyEnd; |
| 4120 |
ei.currentFinally = null; |
| 4121 |
} |
| 4122 |
} |
| 4123 |
if (ei.finallyBlock == finallyBlock) { |
| 4124 |
break; |
| 4125 |
} |
| 4126 |
} |
| 4127 |
} |
| 4128 |
|
| 4129 |
|
| 4130 |
|
| 4131 |
|
| 4132 |
|
| 4133 |
|
| 4134 |
|
| 4135 |
|
| 4136 |
private void endCatch(ExceptionInfo ei, int exceptionType, int catchEnd) |
| 4137 |
{ |
| 4138 |
if (ei.exceptionStarts[exceptionType] == 0) { |
| 4139 |
throw new IllegalStateException("bad exception start"); |
| 4140 |
} |
| 4141 |
|
| 4142 |
int currentStart = ei.exceptionStarts[exceptionType]; |
| 4143 |
int currentStartPC = cfw.getLabelPC(currentStart); |
| 4144 |
int catchEndPC = cfw.getLabelPC(catchEnd); |
| 4145 |
if (currentStartPC != catchEndPC) { |
| 4146 |
cfw.addExceptionHandler(ei.exceptionStarts[exceptionType], |
| 4147 |
catchEnd, |
| 4148 |
ei.handlerLabels[exceptionType], |
| 4149 |
exceptionTypeToName(exceptionType)); |
| 4150 |
} |
| 4151 |
} |
| 4152 |
|
| 4153 |
private ExceptionInfo getTop() |
| 4154 |
{ |
| 4155 |
return exceptionInfo.getLast(); |
| 4156 |
} |
| 4157 |
|
| 4158 |
private class ExceptionInfo |
| 4159 |
{ |
| 4160 |
ExceptionInfo(Jump node, Node finallyBlock) |
| 4161 |
{ |
| 4162 |
this.node = node; |
| 4163 |
this.finallyBlock = finallyBlock; |
| 4164 |
handlerLabels = new int[EXCEPTION_MAX]; |
| 4165 |
exceptionStarts = new int[EXCEPTION_MAX]; |
| 4166 |
currentFinally = null; |
| 4167 |
} |
| 4168 |
|
| 4169 |
Jump node; |
| 4170 |
Node finallyBlock; |
| 4171 |
int[] handlerLabels; |
| 4172 |
int[] exceptionStarts; |
| 4173 |
|
| 4174 |
|
| 4175 |
Node currentFinally; |
| 4176 |
} |
| 4177 |
|
| 4178 |
|
| 4179 |
private LinkedList<ExceptionInfo> exceptionInfo; |
| 4180 |
} |
| 4181 |
|
| 4182 |
private ExceptionManager exceptionManager = new ExceptionManager(); |
| 4183 |
|
| 4184 |
|
| 4185 |
|
| 4186 |
|
| 4187 |
|
| 4188 |
|
| 4189 |
|
| 4190 |
|
| 4191 |
|
| 4192 |
|
| 4193 |
|
| 4194 |
|
| 4195 |
|
| 4196 |
|
| 4197 |
|
| 4198 |
|
| 4199 |
|
| 4200 |
|
| 4201 |
|
| 4202 |
|
| 4203 |
|
| 4204 |
|
| 4205 |
|
| 4206 |
|
| 4207 |
|
| 4208 |
private void inlineFinally(Node finallyTarget, int finallyStart, |
| 4209 |
int finallyEnd) { |
| 4210 |
Node fBlock = getFinallyAtTarget(finallyTarget); |
| 4211 |
fBlock.resetTargets(); |
| 4212 |
Node child = fBlock.getFirstChild(); |
| 4213 |
exceptionManager.markInlineFinallyStart(fBlock, finallyStart); |
| 4214 |
while (child != null) { |
| 4215 |
generateStatement(child); |
| 4216 |
child = child.getNext(); |
| 4217 |
} |
| 4218 |
exceptionManager.markInlineFinallyEnd(fBlock, finallyEnd); |
| 4219 |
} |
| 4220 |
|
| 4221 |
private void inlineFinally(Node finallyTarget) { |
| 4222 |
int finallyStart = cfw.acquireLabel(); |
| 4223 |
int finallyEnd = cfw.acquireLabel(); |
| 4224 |
cfw.markLabel(finallyStart); |
| 4225 |
inlineFinally(finallyTarget, finallyStart, finallyEnd); |
| 4226 |
cfw.markLabel(finallyEnd); |
| 4227 |
} |
| 4228 |
|
| 4229 |
|
| 4230 |
|
| 4231 |
|
| 4232 |
|
| 4233 |
|
| 4234 |
|
| 4235 |
private Node getFinallyAtTarget(Node node) { |
| 4236 |
if (node == null) { |
| 4237 |
return null; |
| 4238 |
} else if (node.getType() == Token.FINALLY) { |
| 4239 |
return node; |
| 4240 |
} else if (node != null && node.getType() == Token.TARGET) { |
| 4241 |
Node fBlock = node.getNext(); |
| 4242 |
if (fBlock != null && fBlock.getType() == Token.FINALLY) { |
| 4243 |
return fBlock; |
| 4244 |
} |
| 4245 |
} |
| 4246 |
throw Kit.codeBug("bad finally target"); |
| 4247 |
} |
| 4248 |
|
| 4249 |
private boolean generateSaveLocals(Node node) |
| 4250 |
{ |
| 4251 |
int count = 0; |
| 4252 |
for (int i = 0; i < firstFreeLocal; i++) { |
| 4253 |
if (locals[i] != 0) |
| 4254 |
count++; |
| 4255 |
} |
| 4256 |
|
| 4257 |
if (count == 0) { |
| 4258 |
((FunctionNode)scriptOrFn).addLiveLocals(node, null); |
| 4259 |
return false; |
| 4260 |
} |
| 4261 |
|
| 4262 |
|
| 4263 |
maxLocals = maxLocals > count ? maxLocals : count; |
| 4264 |
|
| 4265 |
|
| 4266 |
int[] ls = new int[count]; |
| 4267 |
int s = 0; |
| 4268 |
for (int i = 0; i < firstFreeLocal; i++) { |
| 4269 |
if (locals[i] != 0) { |
| 4270 |
ls[s] = i; |
| 4271 |
s++; |
| 4272 |
} |
| 4273 |
} |
| 4274 |
|
| 4275 |
|
| 4276 |
((FunctionNode)scriptOrFn).addLiveLocals(node, ls); |
| 4277 |
|
| 4278 |
|
| 4279 |
generateGetGeneratorLocalsState(); |
| 4280 |
for (int i = 0; i < count; i++) { |
| 4281 |
cfw.add(ByteCode.DUP); |
| 4282 |
cfw.addLoadConstant(i); |
| 4283 |
cfw.addALoad(ls[i]); |
| 4284 |
cfw.add(ByteCode.AASTORE); |
| 4285 |
} |
| 4286 |
|
| 4287 |
cfw.add(ByteCode.POP); |
| 4288 |
|
| 4289 |
return true; |
| 4290 |
} |
| 4291 |
|
| 4292 |
private void visitSwitch(Jump switchNode, Node child) |
| 4293 |
{ |
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
generateExpression(child, switchNode); |
| 4298 |
|
| 4299 |
short selector = getNewWordLocal(); |
| 4300 |
cfw.addAStore(selector); |
| 4301 |
|
| 4302 |
for (Jump caseNode = (Jump)child.getNext(); |
| 4303 |
caseNode != null; |
| 4304 |
caseNode = (Jump)caseNode.getNext()) |
| 4305 |
{ |
| 4306 |
if (caseNode.getType() != Token.CASE) |
| 4307 |
throw Codegen.badTree(); |
| 4308 |
Node test = caseNode.getFirstChild(); |
| 4309 |
generateExpression(test, caseNode); |
| 4310 |
cfw.addALoad(selector); |
| 4311 |
addScriptRuntimeInvoke("shallowEq", |
| 4312 |
"(Ljava/lang/Object;" |
| 4313 |
+"Ljava/lang/Object;" |
| 4314 |
+")Z"); |
| 4315 |
addGoto(caseNode.target, ByteCode.IFNE); |
| 4316 |
} |
| 4317 |
releaseWordLocal(selector); |
| 4318 |
} |
| 4319 |
|
| 4320 |
private void visitTypeofname(Node node) |
| 4321 |
{ |
| 4322 |
if (hasVarsInRegs) { |
| 4323 |
int varIndex = fnCurrent.fnode.getIndexForNameNode(node); |
| 4324 |
if (varIndex >= 0) { |
| 4325 |
if (fnCurrent.isNumberVar(varIndex)) { |
| 4326 |
cfw.addPush("number"); |
| 4327 |
} else if (varIsDirectCallParameter(varIndex)) { |
| 4328 |
int dcp_register = varRegisters[varIndex]; |
| 4329 |
cfw.addALoad(dcp_register); |
| 4330 |
cfw.add(ByteCode.GETSTATIC, "java/lang/Void", "TYPE", |
| 4331 |
"Ljava/lang/Class;"); |
| 4332 |
int isNumberLabel = cfw.acquireLabel(); |
| 4333 |
cfw.add(ByteCode.IF_ACMPEQ, isNumberLabel); |
| 4334 |
short stack = cfw.getStackTop(); |
| 4335 |
cfw.addALoad(dcp_register); |
| 4336 |
addScriptRuntimeInvoke("typeof", |
| 4337 |
"(Ljava/lang/Object;" |
| 4338 |
+")Ljava/lang/String;"); |
| 4339 |
int beyond = cfw.acquireLabel(); |
| 4340 |
cfw.add(ByteCode.GOTO, beyond); |
| 4341 |
cfw.markLabel(isNumberLabel, stack); |
| 4342 |
cfw.addPush("number"); |
| 4343 |
cfw.markLabel(beyond); |
| 4344 |
} else { |
| 4345 |
cfw.addALoad(varRegisters[varIndex]); |
| 4346 |
addScriptRuntimeInvoke("typeof", |
| 4347 |
"(Ljava/lang/Object;" |
| 4348 |
+")Ljava/lang/String;"); |
| 4349 |
} |
| 4350 |
return; |
| 4351 |
} |
| 4352 |
} |
| 4353 |
cfw.addALoad(variableObjectLocal); |
| 4354 |
cfw.addPush(node.getString()); |
| 4355 |
addScriptRuntimeInvoke("typeofName", |
| 4356 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 4357 |
+"Ljava/lang/String;" |
| 4358 |
+")Ljava/lang/String;"); |
| 4359 |
} |
| 4360 |
|
| 4361 |
|
| 4362 |
|
| 4363 |
|
| 4364 |
|
| 4365 |
|
| 4366 |
private void saveCurrentCodeOffset() { |
| 4367 |
savedCodeOffset = cfw.getCurrentCodeOffset(); |
| 4368 |
} |
| 4369 |
|
| 4370 |
|
| 4371 |
|
| 4372 |
|
| 4373 |
|
| 4374 |
|
| 4375 |
|
| 4376 |
private void addInstructionCount() { |
| 4377 |
int count = cfw.getCurrentCodeOffset() - savedCodeOffset; |
| 4378 |
|
| 4379 |
|
| 4380 |
|
| 4381 |
addInstructionCount(Math.max(count, 1)); |
| 4382 |
} |
| 4383 |
|
| 4384 |
|
| 4385 |
|
| 4386 |
|
| 4387 |
|
| 4388 |
|
| 4389 |
|
| 4390 |
|
| 4391 |
|
| 4392 |
private void addInstructionCount(int count) { |
| 4393 |
cfw.addALoad(contextLocal); |
| 4394 |
cfw.addPush(count); |
| 4395 |
addScriptRuntimeInvoke("addInstructionCount", |
| 4396 |
"(Lorg/mozilla/javascript/Context;" |
| 4397 |
+"I)V"); |
| 4398 |
} |
| 4399 |
|
| 4400 |
private void visitIncDec(Node node) |
| 4401 |
{ |
| 4402 |
int incrDecrMask = node.getExistingIntProp(Node.INCRDECR_PROP); |
| 4403 |
Node child = node.getFirstChild(); |
| 4404 |
switch (child.getType()) { |
| 4405 |
case Token.GETVAR: |
| 4406 |
if (!hasVarsInRegs) Kit.codeBug(); |
| 4407 |
boolean post = ((incrDecrMask & Node.POST_FLAG) != 0); |
| 4408 |
int varIndex = fnCurrent.getVarIndex(child); |
| 4409 |
short reg = varRegisters[varIndex]; |
| 4410 |
boolean[] constDeclarations = fnCurrent.fnode.getParamAndVarConst(); |
| 4411 |
if (constDeclarations[varIndex]) { |
| 4412 |
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) { |
| 4413 |
int offset = varIsDirectCallParameter(varIndex) ? 1 : 0; |
| 4414 |
cfw.addDLoad(reg + offset); |
| 4415 |
if (!post) { |
| 4416 |
cfw.addPush(1.0); |
| 4417 |
if ((incrDecrMask & Node.DECR_FLAG) == 0) { |
| 4418 |
cfw.add(ByteCode.DADD); |
| 4419 |
} else { |
| 4420 |
cfw.add(ByteCode.DSUB); |
| 4421 |
} |
| 4422 |
} |
| 4423 |
} else { |
| 4424 |
if (varIsDirectCallParameter(varIndex)) { |
| 4425 |
dcpLoadAsObject(reg); |
| 4426 |
} else { |
| 4427 |
cfw.addALoad(reg); |
| 4428 |
} |
| 4429 |
if (post) { |
| 4430 |
cfw.add(ByteCode.DUP); |
| 4431 |
addObjectToDouble(); |
| 4432 |
cfw.add(ByteCode.POP2); |
| 4433 |
} else { |
| 4434 |
addObjectToDouble(); |
| 4435 |
cfw.addPush(1.0); |
| 4436 |
if ((incrDecrMask & Node.DECR_FLAG) == 0) { |
| 4437 |
cfw.add(ByteCode.DADD); |
| 4438 |
} else { |
| 4439 |
cfw.add(ByteCode.DSUB); |
| 4440 |
} |
| 4441 |
addDoubleWrap(); |
| 4442 |
} |
| 4443 |
} |
| 4444 |
break; |
| 4445 |
} |
| 4446 |
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) { |
| 4447 |
int offset = varIsDirectCallParameter(varIndex) ? 1 : 0; |
| 4448 |
cfw.addDLoad(reg + offset); |
| 4449 |
if (post) { |
| 4450 |
cfw.add(ByteCode.DUP2); |
| 4451 |
} |
| 4452 |
cfw.addPush(1.0); |
| 4453 |
if ((incrDecrMask & Node.DECR_FLAG) == 0) { |
| 4454 |
cfw.add(ByteCode.DADD); |
| 4455 |
} else { |
| 4456 |
cfw.add(ByteCode.DSUB); |
| 4457 |
} |
| 4458 |
if (!post) { |
| 4459 |
cfw.add(ByteCode.DUP2); |
| 4460 |
} |
| 4461 |
cfw.addDStore(reg + offset); |
| 4462 |
} else { |
| 4463 |
if (varIsDirectCallParameter(varIndex)) { |
| 4464 |
dcpLoadAsObject(reg); |
| 4465 |
} else { |
| 4466 |
cfw.addALoad(reg); |
| 4467 |
} |
| 4468 |
addObjectToDouble(); |
| 4469 |
if (post) { |
| 4470 |
cfw.add(ByteCode.DUP2); |
| 4471 |
} |
| 4472 |
cfw.addPush(1.0); |
| 4473 |
if ((incrDecrMask & Node.DECR_FLAG) == 0) { |
| 4474 |
cfw.add(ByteCode.DADD); |
| 4475 |
} else { |
| 4476 |
cfw.add(ByteCode.DSUB); |
| 4477 |
} |
| 4478 |
addDoubleWrap(); |
| 4479 |
if (!post) { |
| 4480 |
cfw.add(ByteCode.DUP); |
| 4481 |
} |
| 4482 |
cfw.addAStore(reg); |
| 4483 |
if (post) { |
| 4484 |
addDoubleWrap(); |
| 4485 |
} |
| 4486 |
} |
| 4487 |
break; |
| 4488 |
case Token.NAME: |
| 4489 |
cfw.addALoad(variableObjectLocal); |
| 4490 |
cfw.addPush(child.getString()); |
| 4491 |
cfw.addALoad(contextLocal); |
| 4492 |
cfw.addPush(incrDecrMask); |
| 4493 |
addScriptRuntimeInvoke("nameIncrDecr", |
| 4494 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 4495 |
+"Ljava/lang/String;" |
| 4496 |
+"Lorg/mozilla/javascript/Context;" |
| 4497 |
+"I)Ljava/lang/Object;"); |
| 4498 |
break; |
| 4499 |
case Token.GETPROPNOWARN: |
| 4500 |
throw Kit.codeBug(); |
| 4501 |
case Token.GETPROP: { |
| 4502 |
Node getPropChild = child.getFirstChild(); |
| 4503 |
generateExpression(getPropChild, node); |
| 4504 |
generateExpression(getPropChild.getNext(), node); |
| 4505 |
cfw.addALoad(contextLocal); |
| 4506 |
cfw.addALoad(variableObjectLocal); |
| 4507 |
cfw.addPush(incrDecrMask); |
| 4508 |
addScriptRuntimeInvoke("propIncrDecr", |
| 4509 |
"(Ljava/lang/Object;" |
| 4510 |
+"Ljava/lang/String;" |
| 4511 |
+"Lorg/mozilla/javascript/Context;" |
| 4512 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 4513 |
+"I)Ljava/lang/Object;"); |
| 4514 |
break; |
| 4515 |
} |
| 4516 |
case Token.GETELEM: { |
| 4517 |
Node elemChild = child.getFirstChild(); |
| 4518 |
generateExpression(elemChild, node); |
| 4519 |
generateExpression(elemChild.getNext(), node); |
| 4520 |
cfw.addALoad(contextLocal); |
| 4521 |
cfw.addALoad(variableObjectLocal); |
| 4522 |
cfw.addPush(incrDecrMask); |
| 4523 |
if (elemChild.getNext().getIntProp(Node.ISNUMBER_PROP, -1) != -1) { |
| 4524 |
addOptRuntimeInvoke("elemIncrDecr", |
| 4525 |
"(Ljava/lang/Object;" |
| 4526 |
+"D" |
| 4527 |
+"Lorg/mozilla/javascript/Context;" |
| 4528 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 4529 |
+"I" |
| 4530 |
+")Ljava/lang/Object;"); |
| 4531 |
} else { |
| 4532 |
addScriptRuntimeInvoke("elemIncrDecr", |
| 4533 |
"(Ljava/lang/Object;" |
| 4534 |
+"Ljava/lang/Object;" |
| 4535 |
+"Lorg/mozilla/javascript/Context;" |
| 4536 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 4537 |
+"I" |
| 4538 |
+")Ljava/lang/Object;"); |
| 4539 |
} |
| 4540 |
break; |
| 4541 |
} |
| 4542 |
case Token.GET_REF: { |
| 4543 |
Node refChild = child.getFirstChild(); |
| 4544 |
generateExpression(refChild, node); |
| 4545 |
cfw.addALoad(contextLocal); |
| 4546 |
cfw.addALoad(variableObjectLocal); |
| 4547 |
cfw.addPush(incrDecrMask); |
| 4548 |
addScriptRuntimeInvoke( |
| 4549 |
"refIncrDecr", |
| 4550 |
"(Lorg/mozilla/javascript/Ref;" |
| 4551 |
+"Lorg/mozilla/javascript/Context;" |
| 4552 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 4553 |
+"I)Ljava/lang/Object;"); |
| 4554 |
break; |
| 4555 |
} |
| 4556 |
default: |
| 4557 |
Codegen.badTree(); |
| 4558 |
} |
| 4559 |
} |
| 4560 |
|
| 4561 |
private static boolean isArithmeticNode(Node node) |
| 4562 |
{ |
| 4563 |
int type = node.getType(); |
| 4564 |
return (type == Token.SUB) |
| 4565 |
|| (type == Token.MOD) |
| 4566 |
|| (type == Token.DIV) |
| 4567 |
|| (type == Token.MUL); |
| 4568 |
} |
| 4569 |
|
| 4570 |
private void visitArithmetic(Node node, int opCode, Node child, |
| 4571 |
Node parent) |
| 4572 |
{ |
| 4573 |
int childNumberFlag = node.getIntProp(Node.ISNUMBER_PROP, -1); |
| 4574 |
if (childNumberFlag != -1) { |
| 4575 |
generateExpression(child, node); |
| 4576 |
generateExpression(child.getNext(), node); |
| 4577 |
cfw.add(opCode); |
| 4578 |
} |
| 4579 |
else { |
| 4580 |
boolean childOfArithmetic = isArithmeticNode(parent); |
| 4581 |
generateExpression(child, node); |
| 4582 |
if (!isArithmeticNode(child)) |
| 4583 |
addObjectToDouble(); |
| 4584 |
generateExpression(child.getNext(), node); |
| 4585 |
if (!isArithmeticNode(child.getNext())) |
| 4586 |
addObjectToDouble(); |
| 4587 |
cfw.add(opCode); |
| 4588 |
if (!childOfArithmetic) { |
| 4589 |
addDoubleWrap(); |
| 4590 |
} |
| 4591 |
} |
| 4592 |
} |
| 4593 |
|
| 4594 |
private void visitBitOp(Node node, int type, Node child) |
| 4595 |
{ |
| 4596 |
int childNumberFlag = node.getIntProp(Node.ISNUMBER_PROP, -1); |
| 4597 |
generateExpression(child, node); |
| 4598 |
|
| 4599 |
|
| 4600 |
|
| 4601 |
|
| 4602 |
if (type == Token.URSH) { |
| 4603 |
addScriptRuntimeInvoke("toUint32", "(Ljava/lang/Object;)J"); |
| 4604 |
generateExpression(child.getNext(), node); |
| 4605 |
addScriptRuntimeInvoke("toInt32", "(Ljava/lang/Object;)I"); |
| 4606 |
|
| 4607 |
|
| 4608 |
cfw.addPush(31); |
| 4609 |
cfw.add(ByteCode.IAND); |
| 4610 |
cfw.add(ByteCode.LUSHR); |
| 4611 |
cfw.add(ByteCode.L2D); |
| 4612 |
addDoubleWrap(); |
| 4613 |
return; |
| 4614 |
} |
| 4615 |
if (childNumberFlag == -1) { |
| 4616 |
addScriptRuntimeInvoke("toInt32", "(Ljava/lang/Object;)I"); |
| 4617 |
generateExpression(child.getNext(), node); |
| 4618 |
addScriptRuntimeInvoke("toInt32", "(Ljava/lang/Object;)I"); |
| 4619 |
} |
| 4620 |
else { |
| 4621 |
addScriptRuntimeInvoke("toInt32", "(D)I"); |
| 4622 |
generateExpression(child.getNext(), node); |
| 4623 |
addScriptRuntimeInvoke("toInt32", "(D)I"); |
| 4624 |
} |
| 4625 |
switch (type) { |
| 4626 |
case Token.BITOR: |
| 4627 |
cfw.add(ByteCode.IOR); |
| 4628 |
break; |
| 4629 |
case Token.BITXOR: |
| 4630 |
cfw.add(ByteCode.IXOR); |
| 4631 |
break; |
| 4632 |
case Token.BITAND: |
| 4633 |
cfw.add(ByteCode.IAND); |
| 4634 |
break; |
| 4635 |
case Token.RSH: |
| 4636 |
cfw.add(ByteCode.ISHR); |
| 4637 |
break; |
| 4638 |
case Token.LSH: |
| 4639 |
cfw.add(ByteCode.ISHL); |
| 4640 |
break; |
| 4641 |
default: |
| 4642 |
throw Codegen.badTree(); |
| 4643 |
} |
| 4644 |
cfw.add(ByteCode.I2D); |
| 4645 |
if (childNumberFlag == -1) { |
| 4646 |
addDoubleWrap(); |
| 4647 |
} |
| 4648 |
} |
| 4649 |
|
| 4650 |
private int nodeIsDirectCallParameter(Node node) |
| 4651 |
{ |
| 4652 |
if (node.getType() == Token.GETVAR |
| 4653 |
&& inDirectCallFunction && !itsForcedObjectParameters) |
| 4654 |
{ |
| 4655 |
int varIndex = fnCurrent.getVarIndex(node); |
| 4656 |
if (fnCurrent.isParameter(varIndex)) { |
| 4657 |
return varRegisters[varIndex]; |
| 4658 |
} |
| 4659 |
} |
| 4660 |
return -1; |
| 4661 |
} |
| 4662 |
|
| 4663 |
private boolean varIsDirectCallParameter(int varIndex) |
| 4664 |
{ |
| 4665 |
return fnCurrent.isParameter(varIndex) |
| 4666 |
&& inDirectCallFunction && !itsForcedObjectParameters; |
| 4667 |
} |
| 4668 |
|
| 4669 |
private void genSimpleCompare(int type, int trueGOTO, int falseGOTO) |
| 4670 |
{ |
| 4671 |
if (trueGOTO == -1) throw Codegen.badTree(); |
| 4672 |
switch (type) { |
| 4673 |
case Token.LE : |
| 4674 |
cfw.add(ByteCode.DCMPG); |
| 4675 |
cfw.add(ByteCode.IFLE, trueGOTO); |
| 4676 |
break; |
| 4677 |
case Token.GE : |
| 4678 |
cfw.add(ByteCode.DCMPL); |
| 4679 |
cfw.add(ByteCode.IFGE, trueGOTO); |
| 4680 |
break; |
| 4681 |
case Token.LT : |
| 4682 |
cfw.add(ByteCode.DCMPG); |
| 4683 |
cfw.add(ByteCode.IFLT, trueGOTO); |
| 4684 |
break; |
| 4685 |
case Token.GT : |
| 4686 |
cfw.add(ByteCode.DCMPL); |
| 4687 |
cfw.add(ByteCode.IFGT, trueGOTO); |
| 4688 |
break; |
| 4689 |
default : |
| 4690 |
throw Codegen.badTree(); |
| 4691 |
|
| 4692 |
} |
| 4693 |
if (falseGOTO != -1) |
| 4694 |
cfw.add(ByteCode.GOTO, falseGOTO); |
| 4695 |
} |
| 4696 |
|
| 4697 |
private void visitIfJumpRelOp(Node node, Node child, |
| 4698 |
int trueGOTO, int falseGOTO) |
| 4699 |
{ |
| 4700 |
if (trueGOTO == -1 || falseGOTO == -1) throw Codegen.badTree(); |
| 4701 |
int type = node.getType(); |
| 4702 |
Node rChild = child.getNext(); |
| 4703 |
if (type == Token.INSTANCEOF || type == Token.IN) { |
| 4704 |
generateExpression(child, node); |
| 4705 |
generateExpression(rChild, node); |
| 4706 |
cfw.addALoad(contextLocal); |
| 4707 |
addScriptRuntimeInvoke( |
| 4708 |
(type == Token.INSTANCEOF) ? "instanceOf" : "in", |
| 4709 |
"(Ljava/lang/Object;" |
| 4710 |
+"Ljava/lang/Object;" |
| 4711 |
+"Lorg/mozilla/javascript/Context;" |
| 4712 |
+")Z"); |
| 4713 |
cfw.add(ByteCode.IFNE, trueGOTO); |
| 4714 |
cfw.add(ByteCode.GOTO, falseGOTO); |
| 4715 |
return; |
| 4716 |
} |
| 4717 |
int childNumberFlag = node.getIntProp(Node.ISNUMBER_PROP, -1); |
| 4718 |
int left_dcp_register = nodeIsDirectCallParameter(child); |
| 4719 |
int right_dcp_register = nodeIsDirectCallParameter(rChild); |
| 4720 |
if (childNumberFlag != -1) { |
| 4721 |
|
| 4722 |
|
| 4723 |
|
| 4724 |
if (childNumberFlag != Node.RIGHT) { |
| 4725 |
|
| 4726 |
generateExpression(child, node); |
| 4727 |
} else if (left_dcp_register != -1) { |
| 4728 |
dcpLoadAsNumber(left_dcp_register); |
| 4729 |
} else { |
| 4730 |
generateExpression(child, node); |
| 4731 |
addObjectToDouble(); |
| 4732 |
} |
| 4733 |
|
| 4734 |
if (childNumberFlag != Node.LEFT) { |
| 4735 |
|
| 4736 |
generateExpression(rChild, node); |
| 4737 |
} else if (right_dcp_register != -1) { |
| 4738 |
dcpLoadAsNumber(right_dcp_register); |
| 4739 |
} else { |
| 4740 |
generateExpression(rChild, node); |
| 4741 |
addObjectToDouble(); |
| 4742 |
} |
| 4743 |
|
| 4744 |
genSimpleCompare(type, trueGOTO, falseGOTO); |
| 4745 |
|
| 4746 |
} else { |
| 4747 |
if (left_dcp_register != -1 && right_dcp_register != -1) { |
| 4748 |
|
| 4749 |
|
| 4750 |
short stack = cfw.getStackTop(); |
| 4751 |
int leftIsNotNumber = cfw.acquireLabel(); |
| 4752 |
cfw.addALoad(left_dcp_register); |
| 4753 |
cfw.add(ByteCode.GETSTATIC, |
| 4754 |
"java/lang/Void", |
| 4755 |
"TYPE", |
| 4756 |
"Ljava/lang/Class;"); |
| 4757 |
cfw.add(ByteCode.IF_ACMPNE, leftIsNotNumber); |
| 4758 |
cfw.addDLoad(left_dcp_register + 1); |
| 4759 |
dcpLoadAsNumber(right_dcp_register); |
| 4760 |
genSimpleCompare(type, trueGOTO, falseGOTO); |
| 4761 |
if (stack != cfw.getStackTop()) throw Codegen.badTree(); |
| 4762 |
|
| 4763 |
cfw.markLabel(leftIsNotNumber); |
| 4764 |
int rightIsNotNumber = cfw.acquireLabel(); |
| 4765 |
cfw.addALoad(right_dcp_register); |
| 4766 |
cfw.add(ByteCode.GETSTATIC, |
| 4767 |
"java/lang/Void", |
| 4768 |
"TYPE", |
| 4769 |
"Ljava/lang/Class;"); |
| 4770 |
cfw.add(ByteCode.IF_ACMPNE, rightIsNotNumber); |
| 4771 |
cfw.addALoad(left_dcp_register); |
| 4772 |
addObjectToDouble(); |
| 4773 |
cfw.addDLoad(right_dcp_register + 1); |
| 4774 |
genSimpleCompare(type, trueGOTO, falseGOTO); |
| 4775 |
if (stack != cfw.getStackTop()) throw Codegen.badTree(); |
| 4776 |
|
| 4777 |
cfw.markLabel(rightIsNotNumber); |
| 4778 |
|
| 4779 |
cfw.addALoad(left_dcp_register); |
| 4780 |
cfw.addALoad(right_dcp_register); |
| 4781 |
|
| 4782 |
} else { |
| 4783 |
generateExpression(child, node); |
| 4784 |
generateExpression(rChild, node); |
| 4785 |
} |
| 4786 |
|
| 4787 |
if (type == Token.GE || type == Token.GT) { |
| 4788 |
cfw.add(ByteCode.SWAP); |
| 4789 |
} |
| 4790 |
String routine = ((type == Token.LT) |
| 4791 |
|| (type == Token.GT)) ? "cmp_LT" : "cmp_LE"; |
| 4792 |
addScriptRuntimeInvoke(routine, |
| 4793 |
"(Ljava/lang/Object;" |
| 4794 |
+"Ljava/lang/Object;" |
| 4795 |
+")Z"); |
| 4796 |
cfw.add(ByteCode.IFNE, trueGOTO); |
| 4797 |
cfw.add(ByteCode.GOTO, falseGOTO); |
| 4798 |
} |
| 4799 |
} |
| 4800 |
|
| 4801 |
private void visitIfJumpEqOp(Node node, Node child, |
| 4802 |
int trueGOTO, int falseGOTO) |
| 4803 |
{ |
| 4804 |
if (trueGOTO == -1 || falseGOTO == -1) throw Codegen.badTree(); |
| 4805 |
|
| 4806 |
short stackInitial = cfw.getStackTop(); |
| 4807 |
int type = node.getType(); |
| 4808 |
Node rChild = child.getNext(); |
| 4809 |
|
| 4810 |
|
| 4811 |
if (child.getType() == Token.NULL || rChild.getType() == Token.NULL) { |
| 4812 |
|
| 4813 |
if (child.getType() == Token.NULL) { |
| 4814 |
child = rChild; |
| 4815 |
} |
| 4816 |
generateExpression(child, node); |
| 4817 |
if (type == Token.SHEQ || type == Token.SHNE) { |
| 4818 |
int testCode = (type == Token.SHEQ) |
| 4819 |
? ByteCode.IFNULL : ByteCode.IFNONNULL; |
| 4820 |
cfw.add(testCode, trueGOTO); |
| 4821 |
} else { |
| 4822 |
if (type != Token.EQ) { |
| 4823 |
|
| 4824 |
if (type != Token.NE) throw Codegen.badTree(); |
| 4825 |
int tmp = trueGOTO; |
| 4826 |
trueGOTO = falseGOTO; |
| 4827 |
falseGOTO = tmp; |
| 4828 |
} |
| 4829 |
cfw.add(ByteCode.DUP); |
| 4830 |
int undefCheckLabel = cfw.acquireLabel(); |
| 4831 |
cfw.add(ByteCode.IFNONNULL, undefCheckLabel); |
| 4832 |
short stack = cfw.getStackTop(); |
| 4833 |
cfw.add(ByteCode.POP); |
| 4834 |
cfw.add(ByteCode.GOTO, trueGOTO); |
| 4835 |
cfw.markLabel(undefCheckLabel, stack); |
| 4836 |
Codegen.pushUndefined(cfw); |
| 4837 |
cfw.add(ByteCode.IF_ACMPEQ, trueGOTO); |
| 4838 |
} |
| 4839 |
cfw.add(ByteCode.GOTO, falseGOTO); |
| 4840 |
} else { |
| 4841 |
int child_dcp_register = nodeIsDirectCallParameter(child); |
| 4842 |
if (child_dcp_register != -1 |
| 4843 |
&& rChild.getType() == Token.TO_OBJECT) |
| 4844 |
{ |
| 4845 |
Node convertChild = rChild.getFirstChild(); |
| 4846 |
if (convertChild.getType() == Token.NUMBER) { |
| 4847 |
cfw.addALoad(child_dcp_register); |
| 4848 |
cfw.add(ByteCode.GETSTATIC, |
| 4849 |
"java/lang/Void", |
| 4850 |
"TYPE", |
| 4851 |
"Ljava/lang/Class;"); |
| 4852 |
int notNumbersLabel = cfw.acquireLabel(); |
| 4853 |
cfw.add(ByteCode.IF_ACMPNE, notNumbersLabel); |
| 4854 |
cfw.addDLoad(child_dcp_register + 1); |
| 4855 |
cfw.addPush(convertChild.getDouble()); |
| 4856 |
cfw.add(ByteCode.DCMPL); |
| 4857 |
if (type == Token.EQ) |
| 4858 |
cfw.add(ByteCode.IFEQ, trueGOTO); |
| 4859 |
else |
| 4860 |
cfw.add(ByteCode.IFNE, trueGOTO); |
| 4861 |
cfw.add(ByteCode.GOTO, falseGOTO); |
| 4862 |
cfw.markLabel(notNumbersLabel); |
| 4863 |
|
| 4864 |
} |
| 4865 |
} |
| 4866 |
|
| 4867 |
generateExpression(child, node); |
| 4868 |
generateExpression(rChild, node); |
| 4869 |
|
| 4870 |
String name; |
| 4871 |
int testCode; |
| 4872 |
switch (type) { |
| 4873 |
case Token.EQ: |
| 4874 |
name = "eq"; |
| 4875 |
testCode = ByteCode.IFNE; |
| 4876 |
break; |
| 4877 |
case Token.NE: |
| 4878 |
name = "eq"; |
| 4879 |
testCode = ByteCode.IFEQ; |
| 4880 |
break; |
| 4881 |
case Token.SHEQ: |
| 4882 |
name = "shallowEq"; |
| 4883 |
testCode = ByteCode.IFNE; |
| 4884 |
break; |
| 4885 |
case Token.SHNE: |
| 4886 |
name = "shallowEq"; |
| 4887 |
testCode = ByteCode.IFEQ; |
| 4888 |
break; |
| 4889 |
default: |
| 4890 |
throw Codegen.badTree(); |
| 4891 |
} |
| 4892 |
addScriptRuntimeInvoke(name, |
| 4893 |
"(Ljava/lang/Object;" |
| 4894 |
+"Ljava/lang/Object;" |
| 4895 |
+")Z"); |
| 4896 |
cfw.add(testCode, trueGOTO); |
| 4897 |
cfw.add(ByteCode.GOTO, falseGOTO); |
| 4898 |
} |
| 4899 |
if (stackInitial != cfw.getStackTop()) throw Codegen.badTree(); |
| 4900 |
} |
| 4901 |
|
| 4902 |
private void visitSetName(Node node, Node child) |
| 4903 |
{ |
| 4904 |
String name = node.getFirstChild().getString(); |
| 4905 |
while (child != null) { |
| 4906 |
generateExpression(child, node); |
| 4907 |
child = child.getNext(); |
| 4908 |
} |
| 4909 |
cfw.addALoad(contextLocal); |
| 4910 |
cfw.addALoad(variableObjectLocal); |
| 4911 |
cfw.addPush(name); |
| 4912 |
addScriptRuntimeInvoke( |
| 4913 |
"setName", |
| 4914 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 4915 |
+"Ljava/lang/Object;" |
| 4916 |
+"Lorg/mozilla/javascript/Context;" |
| 4917 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 4918 |
+"Ljava/lang/String;" |
| 4919 |
+")Ljava/lang/Object;"); |
| 4920 |
} |
| 4921 |
|
| 4922 |
private void visitStrictSetName(Node node, Node child) |
| 4923 |
{ |
| 4924 |
String name = node.getFirstChild().getString(); |
| 4925 |
while (child != null) { |
| 4926 |
generateExpression(child, node); |
| 4927 |
child = child.getNext(); |
| 4928 |
} |
| 4929 |
cfw.addALoad(contextLocal); |
| 4930 |
cfw.addALoad(variableObjectLocal); |
| 4931 |
cfw.addPush(name); |
| 4932 |
addScriptRuntimeInvoke( |
| 4933 |
"strictSetName", |
| 4934 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 4935 |
+"Ljava/lang/Object;" |
| 4936 |
+"Lorg/mozilla/javascript/Context;" |
| 4937 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 4938 |
+"Ljava/lang/String;" |
| 4939 |
+")Ljava/lang/Object;"); |
| 4940 |
} |
| 4941 |
|
| 4942 |
private void visitSetConst(Node node, Node child) |
| 4943 |
{ |
| 4944 |
String name = node.getFirstChild().getString(); |
| 4945 |
while (child != null) { |
| 4946 |
generateExpression(child, node); |
| 4947 |
child = child.getNext(); |
| 4948 |
} |
| 4949 |
cfw.addALoad(contextLocal); |
| 4950 |
cfw.addPush(name); |
| 4951 |
addScriptRuntimeInvoke( |
| 4952 |
"setConst", |
| 4953 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 4954 |
+"Ljava/lang/Object;" |
| 4955 |
+"Lorg/mozilla/javascript/Context;" |
| 4956 |
+"Ljava/lang/String;" |
| 4957 |
+")Ljava/lang/Object;"); |
| 4958 |
} |
| 4959 |
|
| 4960 |
private void visitGetVar(Node node) |
| 4961 |
{ |
| 4962 |
if (!hasVarsInRegs) Kit.codeBug(); |
| 4963 |
int varIndex = fnCurrent.getVarIndex(node); |
| 4964 |
short reg = varRegisters[varIndex]; |
| 4965 |
if (varIsDirectCallParameter(varIndex)) { |
| 4966 |
|
| 4967 |
|
| 4968 |
|
| 4969 |
|
| 4970 |
if (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1) { |
| 4971 |
dcpLoadAsNumber(reg); |
| 4972 |
} else { |
| 4973 |
dcpLoadAsObject(reg); |
| 4974 |
} |
| 4975 |
} else if (fnCurrent.isNumberVar(varIndex)) { |
| 4976 |
cfw.addDLoad(reg); |
| 4977 |
} else { |
| 4978 |
cfw.addALoad(reg); |
| 4979 |
} |
| 4980 |
} |
| 4981 |
|
| 4982 |
private void visitSetVar(Node node, Node child, boolean needValue) |
| 4983 |
{ |
| 4984 |
if (!hasVarsInRegs) Kit.codeBug(); |
| 4985 |
int varIndex = fnCurrent.getVarIndex(node); |
| 4986 |
generateExpression(child.getNext(), node); |
| 4987 |
boolean isNumber = (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1); |
| 4988 |
short reg = varRegisters[varIndex]; |
| 4989 |
boolean [] constDeclarations = fnCurrent.fnode.getParamAndVarConst(); |
| 4990 |
if (constDeclarations[varIndex]) { |
| 4991 |
if (!needValue) { |
| 4992 |
if (isNumber) |
| 4993 |
cfw.add(ByteCode.POP2); |
| 4994 |
else |
| 4995 |
cfw.add(ByteCode.POP); |
| 4996 |
} |
| 4997 |
} |
| 4998 |
else if (varIsDirectCallParameter(varIndex)) { |
| 4999 |
if (isNumber) { |
| 5000 |
if (needValue) cfw.add(ByteCode.DUP2); |
| 5001 |
cfw.addALoad(reg); |
| 5002 |
cfw.add(ByteCode.GETSTATIC, |
| 5003 |
"java/lang/Void", |
| 5004 |
"TYPE", |
| 5005 |
"Ljava/lang/Class;"); |
| 5006 |
int isNumberLabel = cfw.acquireLabel(); |
| 5007 |
int beyond = cfw.acquireLabel(); |
| 5008 |
cfw.add(ByteCode.IF_ACMPEQ, isNumberLabel); |
| 5009 |
short stack = cfw.getStackTop(); |
| 5010 |
addDoubleWrap(); |
| 5011 |
cfw.addAStore(reg); |
| 5012 |
cfw.add(ByteCode.GOTO, beyond); |
| 5013 |
cfw.markLabel(isNumberLabel, stack); |
| 5014 |
cfw.addDStore(reg + 1); |
| 5015 |
cfw.markLabel(beyond); |
| 5016 |
} |
| 5017 |
else { |
| 5018 |
if (needValue) cfw.add(ByteCode.DUP); |
| 5019 |
cfw.addAStore(reg); |
| 5020 |
} |
| 5021 |
} else { |
| 5022 |
boolean isNumberVar = fnCurrent.isNumberVar(varIndex); |
| 5023 |
if (isNumber) { |
| 5024 |
if (isNumberVar) { |
| 5025 |
cfw.addDStore(reg); |
| 5026 |
if (needValue) cfw.addDLoad(reg); |
| 5027 |
} else { |
| 5028 |
if (needValue) cfw.add(ByteCode.DUP2); |
| 5029 |
|
| 5030 |
|
| 5031 |
addDoubleWrap(); |
| 5032 |
cfw.addAStore(reg); |
| 5033 |
} |
| 5034 |
} else { |
| 5035 |
if (isNumberVar) Kit.codeBug(); |
| 5036 |
cfw.addAStore(reg); |
| 5037 |
if (needValue) cfw.addALoad(reg); |
| 5038 |
} |
| 5039 |
} |
| 5040 |
} |
| 5041 |
|
| 5042 |
private void visitSetConstVar(Node node, Node child, boolean needValue) |
| 5043 |
{ |
| 5044 |
if (!hasVarsInRegs) Kit.codeBug(); |
| 5045 |
int varIndex = fnCurrent.getVarIndex(node); |
| 5046 |
generateExpression(child.getNext(), node); |
| 5047 |
boolean isNumber = (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1); |
| 5048 |
short reg = varRegisters[varIndex]; |
| 5049 |
int beyond = cfw.acquireLabel(); |
| 5050 |
int noAssign = cfw.acquireLabel(); |
| 5051 |
if (isNumber) { |
| 5052 |
cfw.addILoad(reg + 2); |
| 5053 |
cfw.add(ByteCode.IFNE, noAssign); |
| 5054 |
short stack = cfw.getStackTop(); |
| 5055 |
cfw.addPush(1); |
| 5056 |
cfw.addIStore(reg + 2); |
| 5057 |
cfw.addDStore(reg); |
| 5058 |
if (needValue) { |
| 5059 |
cfw.addDLoad(reg); |
| 5060 |
cfw.markLabel(noAssign, stack); |
| 5061 |
} else { |
| 5062 |
cfw.add(ByteCode.GOTO, beyond); |
| 5063 |
cfw.markLabel(noAssign, stack); |
| 5064 |
cfw.add(ByteCode.POP2); |
| 5065 |
} |
| 5066 |
} |
| 5067 |
else { |
| 5068 |
cfw.addILoad(reg + 1); |
| 5069 |
cfw.add(ByteCode.IFNE, noAssign); |
| 5070 |
short stack = cfw.getStackTop(); |
| 5071 |
cfw.addPush(1); |
| 5072 |
cfw.addIStore(reg + 1); |
| 5073 |
cfw.addAStore(reg); |
| 5074 |
if (needValue) { |
| 5075 |
cfw.addALoad(reg); |
| 5076 |
cfw.markLabel(noAssign, stack); |
| 5077 |
} else { |
| 5078 |
cfw.add(ByteCode.GOTO, beyond); |
| 5079 |
cfw.markLabel(noAssign, stack); |
| 5080 |
cfw.add(ByteCode.POP); |
| 5081 |
} |
| 5082 |
} |
| 5083 |
cfw.markLabel(beyond); |
| 5084 |
} |
| 5085 |
|
| 5086 |
private void visitGetProp(Node node, Node child) |
| 5087 |
{ |
| 5088 |
generateExpression(child, node); |
| 5089 |
Node nameChild = child.getNext(); |
| 5090 |
generateExpression(nameChild, node); |
| 5091 |
if (node.getType() == Token.GETPROPNOWARN) { |
| 5092 |
cfw.addALoad(contextLocal); |
| 5093 |
cfw.addALoad(variableObjectLocal); |
| 5094 |
addScriptRuntimeInvoke( |
| 5095 |
"getObjectPropNoWarn", |
| 5096 |
"(Ljava/lang/Object;" |
| 5097 |
+"Ljava/lang/String;" |
| 5098 |
+"Lorg/mozilla/javascript/Context;" |
| 5099 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5100 |
+")Ljava/lang/Object;"); |
| 5101 |
return; |
| 5102 |
} |
| 5103 |
|
| 5104 |
|
| 5105 |
|
| 5106 |
|
| 5107 |
int childType = child.getType(); |
| 5108 |
if (childType == Token.THIS && nameChild.getType() == Token.STRING) { |
| 5109 |
cfw.addALoad(contextLocal); |
| 5110 |
addScriptRuntimeInvoke( |
| 5111 |
"getObjectProp", |
| 5112 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 5113 |
+"Ljava/lang/String;" |
| 5114 |
+"Lorg/mozilla/javascript/Context;" |
| 5115 |
+")Ljava/lang/Object;"); |
| 5116 |
} else { |
| 5117 |
cfw.addALoad(contextLocal); |
| 5118 |
cfw.addALoad(variableObjectLocal); |
| 5119 |
addScriptRuntimeInvoke( |
| 5120 |
"getObjectProp", |
| 5121 |
"(Ljava/lang/Object;" |
| 5122 |
+"Ljava/lang/String;" |
| 5123 |
+"Lorg/mozilla/javascript/Context;" |
| 5124 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5125 |
+")Ljava/lang/Object;"); |
| 5126 |
} |
| 5127 |
} |
| 5128 |
|
| 5129 |
private void visitSetProp(int type, Node node, Node child) |
| 5130 |
{ |
| 5131 |
Node objectChild = child; |
| 5132 |
generateExpression(child, node); |
| 5133 |
child = child.getNext(); |
| 5134 |
if (type == Token.SETPROP_OP) { |
| 5135 |
cfw.add(ByteCode.DUP); |
| 5136 |
} |
| 5137 |
Node nameChild = child; |
| 5138 |
generateExpression(child, node); |
| 5139 |
child = child.getNext(); |
| 5140 |
if (type == Token.SETPROP_OP) { |
| 5141 |
|
| 5142 |
cfw.add(ByteCode.DUP_X1); |
| 5143 |
|
| 5144 |
|
| 5145 |
if (objectChild.getType() == Token.THIS |
| 5146 |
&& nameChild.getType() == Token.STRING) |
| 5147 |
{ |
| 5148 |
cfw.addALoad(contextLocal); |
| 5149 |
addScriptRuntimeInvoke( |
| 5150 |
"getObjectProp", |
| 5151 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 5152 |
+"Ljava/lang/String;" |
| 5153 |
+"Lorg/mozilla/javascript/Context;" |
| 5154 |
+")Ljava/lang/Object;"); |
| 5155 |
} else { |
| 5156 |
cfw.addALoad(contextLocal); |
| 5157 |
cfw.addALoad(variableObjectLocal); |
| 5158 |
addScriptRuntimeInvoke( |
| 5159 |
"getObjectProp", |
| 5160 |
"(Ljava/lang/Object;" |
| 5161 |
+"Ljava/lang/String;" |
| 5162 |
+"Lorg/mozilla/javascript/Context;" |
| 5163 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5164 |
+")Ljava/lang/Object;"); |
| 5165 |
} |
| 5166 |
} |
| 5167 |
generateExpression(child, node); |
| 5168 |
cfw.addALoad(contextLocal); |
| 5169 |
cfw.addALoad(variableObjectLocal); |
| 5170 |
addScriptRuntimeInvoke( |
| 5171 |
"setObjectProp", |
| 5172 |
"(Ljava/lang/Object;" |
| 5173 |
+"Ljava/lang/String;" |
| 5174 |
+"Ljava/lang/Object;" |
| 5175 |
+"Lorg/mozilla/javascript/Context;" |
| 5176 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5177 |
+")Ljava/lang/Object;"); |
| 5178 |
} |
| 5179 |
|
| 5180 |
private void visitSetElem(int type, Node node, Node child) |
| 5181 |
{ |
| 5182 |
generateExpression(child, node); |
| 5183 |
child = child.getNext(); |
| 5184 |
if (type == Token.SETELEM_OP) { |
| 5185 |
cfw.add(ByteCode.DUP); |
| 5186 |
} |
| 5187 |
generateExpression(child, node); |
| 5188 |
child = child.getNext(); |
| 5189 |
boolean indexIsNumber = (node.getIntProp(Node.ISNUMBER_PROP, -1) != -1); |
| 5190 |
if (type == Token.SETELEM_OP) { |
| 5191 |
if (indexIsNumber) { |
| 5192 |
|
| 5193 |
|
| 5194 |
cfw.add(ByteCode.DUP2_X1); |
| 5195 |
cfw.addALoad(contextLocal); |
| 5196 |
cfw.addALoad(variableObjectLocal); |
| 5197 |
addScriptRuntimeInvoke( |
| 5198 |
"getObjectIndex", |
| 5199 |
"(Ljava/lang/Object;D" |
| 5200 |
+"Lorg/mozilla/javascript/Context;" |
| 5201 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5202 |
+")Ljava/lang/Object;"); |
| 5203 |
} else { |
| 5204 |
|
| 5205 |
|
| 5206 |
cfw.add(ByteCode.DUP_X1); |
| 5207 |
cfw.addALoad(contextLocal); |
| 5208 |
cfw.addALoad(variableObjectLocal); |
| 5209 |
addScriptRuntimeInvoke( |
| 5210 |
"getObjectElem", |
| 5211 |
"(Ljava/lang/Object;" |
| 5212 |
+"Ljava/lang/Object;" |
| 5213 |
+"Lorg/mozilla/javascript/Context;" |
| 5214 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5215 |
+")Ljava/lang/Object;"); |
| 5216 |
} |
| 5217 |
} |
| 5218 |
generateExpression(child, node); |
| 5219 |
cfw.addALoad(contextLocal); |
| 5220 |
cfw.addALoad(variableObjectLocal); |
| 5221 |
if (indexIsNumber) { |
| 5222 |
addScriptRuntimeInvoke( |
| 5223 |
"setObjectIndex", |
| 5224 |
"(Ljava/lang/Object;" |
| 5225 |
+"D" |
| 5226 |
+"Ljava/lang/Object;" |
| 5227 |
+"Lorg/mozilla/javascript/Context;" |
| 5228 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5229 |
+")Ljava/lang/Object;"); |
| 5230 |
} else { |
| 5231 |
addScriptRuntimeInvoke( |
| 5232 |
"setObjectElem", |
| 5233 |
"(Ljava/lang/Object;" |
| 5234 |
+"Ljava/lang/Object;" |
| 5235 |
+"Ljava/lang/Object;" |
| 5236 |
+"Lorg/mozilla/javascript/Context;" |
| 5237 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5238 |
+")Ljava/lang/Object;"); |
| 5239 |
} |
| 5240 |
} |
| 5241 |
|
| 5242 |
private void visitDotQuery(Node node, Node child) |
| 5243 |
{ |
| 5244 |
updateLineNumber(node); |
| 5245 |
generateExpression(child, node); |
| 5246 |
cfw.addALoad(variableObjectLocal); |
| 5247 |
addScriptRuntimeInvoke("enterDotQuery", |
| 5248 |
"(Ljava/lang/Object;" |
| 5249 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5250 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 5251 |
cfw.addAStore(variableObjectLocal); |
| 5252 |
|
| 5253 |
|
| 5254 |
|
| 5255 |
|
| 5256 |
cfw.add(ByteCode.ACONST_NULL); |
| 5257 |
int queryLoopStart = cfw.acquireLabel(); |
| 5258 |
cfw.markLabel(queryLoopStart); |
| 5259 |
cfw.add(ByteCode.POP); |
| 5260 |
|
| 5261 |
generateExpression(child.getNext(), node); |
| 5262 |
addScriptRuntimeInvoke("toBoolean", "(Ljava/lang/Object;)Z"); |
| 5263 |
cfw.addALoad(variableObjectLocal); |
| 5264 |
addScriptRuntimeInvoke("updateDotQuery", |
| 5265 |
"(Z" |
| 5266 |
+"Lorg/mozilla/javascript/Scriptable;" |
| 5267 |
+")Ljava/lang/Object;"); |
| 5268 |
cfw.add(ByteCode.DUP); |
| 5269 |
cfw.add(ByteCode.IFNULL, queryLoopStart); |
| 5270 |
|
| 5271 |
cfw.addALoad(variableObjectLocal); |
| 5272 |
addScriptRuntimeInvoke("leaveDotQuery", |
| 5273 |
"(Lorg/mozilla/javascript/Scriptable;" |
| 5274 |
+")Lorg/mozilla/javascript/Scriptable;"); |
| 5275 |
cfw.addAStore(variableObjectLocal); |
| 5276 |
} |
| 5277 |
|
| 5278 |
private int getLocalBlockRegister(Node node) |
| 5279 |
{ |
| 5280 |
Node localBlock = (Node)node.getProp(Node.LOCAL_BLOCK_PROP); |
| 5281 |
int localSlot = localBlock.getExistingIntProp(Node.LOCAL_PROP); |
| 5282 |
return localSlot; |
| 5283 |
} |
| 5284 |
|
| 5285 |
private void dcpLoadAsNumber(int dcp_register) |
| 5286 |
{ |
| 5287 |
cfw.addALoad(dcp_register); |
| 5288 |
cfw.add(ByteCode.GETSTATIC, |
| 5289 |
"java/lang/Void", |
| 5290 |
"TYPE", |
| 5291 |
"Ljava/lang/Class;"); |
| 5292 |
int isNumberLabel = cfw.acquireLabel(); |
| 5293 |
cfw.add(ByteCode.IF_ACMPEQ, isNumberLabel); |
| 5294 |
short stack = cfw.getStackTop(); |
| 5295 |
cfw.addALoad(dcp_register); |
| 5296 |
addObjectToDouble(); |
| 5297 |
int beyond = cfw.acquireLabel(); |
| 5298 |
cfw.add(ByteCode.GOTO, beyond); |
| 5299 |
cfw.markLabel(isNumberLabel, stack); |
| 5300 |
cfw.addDLoad(dcp_register + 1); |
| 5301 |
cfw.markLabel(beyond); |
| 5302 |
} |
| 5303 |
|
| 5304 |
private void dcpLoadAsObject(int dcp_register) |
| 5305 |
{ |
| 5306 |
cfw.addALoad(dcp_register); |
| 5307 |
cfw.add(ByteCode.GETSTATIC, |
| 5308 |
"java/lang/Void", |
| 5309 |
"TYPE", |
| 5310 |
"Ljava/lang/Class;"); |
| 5311 |
int isNumberLabel = cfw.acquireLabel(); |
| 5312 |
cfw.add(ByteCode.IF_ACMPEQ, isNumberLabel); |
| 5313 |
short stack = cfw.getStackTop(); |
| 5314 |
cfw.addALoad(dcp_register); |
| 5315 |
int beyond = cfw.acquireLabel(); |
| 5316 |
cfw.add(ByteCode.GOTO, beyond); |
| 5317 |
cfw.markLabel(isNumberLabel, stack); |
| 5318 |
cfw.addDLoad(dcp_register + 1); |
| 5319 |
addDoubleWrap(); |
| 5320 |
cfw.markLabel(beyond); |
| 5321 |
} |
| 5322 |
|
| 5323 |
private void addGoto(Node target, int jumpcode) |
| 5324 |
{ |
| 5325 |
int targetLabel = getTargetLabel(target); |
| 5326 |
cfw.add(jumpcode, targetLabel); |
| 5327 |
} |
| 5328 |
|
| 5329 |
private void addObjectToDouble() |
| 5330 |
{ |
| 5331 |
addScriptRuntimeInvoke("toNumber", "(Ljava/lang/Object;)D"); |
| 5332 |
} |
| 5333 |
|
| 5334 |
private void addNewObjectArray(int size) |
| 5335 |
{ |
| 5336 |
if (size == 0) { |
| 5337 |
if (itsZeroArgArray >= 0) { |
| 5338 |
cfw.addALoad(itsZeroArgArray); |
| 5339 |
} else { |
| 5340 |
cfw.add(ByteCode.GETSTATIC, |
| 5341 |
"org/mozilla/javascript/ScriptRuntime", |
| 5342 |
"emptyArgs", "[Ljava/lang/Object;"); |
| 5343 |
} |
| 5344 |
} else { |
| 5345 |
cfw.addPush(size); |
| 5346 |
cfw.add(ByteCode.ANEWARRAY, "java/lang/Object"); |
| 5347 |
} |
| 5348 |
} |
| 5349 |
|
| 5350 |
private void addScriptRuntimeInvoke(String methodName, |
| 5351 |
String methodSignature) |
| 5352 |
{ |
| 5353 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 5354 |
"org.mozilla.javascript.ScriptRuntime", |
| 5355 |
methodName, |
| 5356 |
methodSignature); |
| 5357 |
} |
| 5358 |
|
| 5359 |
private void addOptRuntimeInvoke(String methodName, |
| 5360 |
String methodSignature) |
| 5361 |
{ |
| 5362 |
cfw.addInvoke(ByteCode.INVOKESTATIC, |
| 5363 |
"org/mozilla/javascript/optimizer/OptRuntime", |
| 5364 |
methodName, |
| 5365 |
methodSignature); |
| 5366 |
} |
| 5367 |
|
| 5368 |
private void addJumpedBooleanWrap(int trueLabel, int falseLabel) |
| 5369 |
{ |
| 5370 |
cfw.markLabel(falseLabel); |
| 5371 |
int skip = cfw.acquireLabel(); |
| 5372 |
cfw.add(ByteCode.GETSTATIC, "java/lang/Boolean", |
| 5373 |
"FALSE", "Ljava/lang/Boolean;"); |
| 5374 |
cfw.add(ByteCode.GOTO, skip); |
| 5375 |
cfw.markLabel(trueLabel); |
| 5376 |
cfw.add(ByteCode.GETSTATIC, "java/lang/Boolean", |
| 5377 |
"TRUE", "Ljava/lang/Boolean;"); |
| 5378 |
cfw.markLabel(skip); |
| 5379 |
cfw.adjustStackTop(-1); |
| 5380 |
} |
| 5381 |
|
| 5382 |
private void addDoubleWrap() |
| 5383 |
{ |
| 5384 |
addOptRuntimeInvoke("wrapDouble", "(D)Ljava/lang/Double;"); |
| 5385 |
} |
| 5386 |
|
| 5387 |
|
| 5388 |
|
| 5389 |
|
| 5390 |
|
| 5391 |
|
| 5392 |
|
| 5393 |
private short getNewWordPairLocal(boolean isConst) |
| 5394 |
{ |
| 5395 |
return getNewWordIntern(isConst ? 3 : 2); |
| 5396 |
} |
| 5397 |
|
| 5398 |
private short getNewWordLocal(boolean isConst) |
| 5399 |
{ |
| 5400 |
return getNewWordIntern(isConst ? 2 : 1); |
| 5401 |
} |
| 5402 |
|
| 5403 |
private short getNewWordLocal() |
| 5404 |
{ |
| 5405 |
return getNewWordIntern(1); |
| 5406 |
} |
| 5407 |
|
| 5408 |
private short getNewWordIntern(int count) |
| 5409 |
{ |
| 5410 |
assert count >= 1 && count <= 3; |
| 5411 |
|
| 5412 |
int[] locals = this.locals; |
| 5413 |
int result = -1; |
| 5414 |
if (count > 1) { |
| 5415 |
|
| 5416 |
OUTER: for (int i = firstFreeLocal; i + count <= MAX_LOCALS;) { |
| 5417 |
for (int j = 0; j < count; ++j) { |
| 5418 |
if (locals[i + j] != 0) { |
| 5419 |
i += j + 1; |
| 5420 |
continue OUTER; |
| 5421 |
} |
| 5422 |
} |
| 5423 |
result = i; |
| 5424 |
break; |
| 5425 |
} |
| 5426 |
} else { |
| 5427 |
result = firstFreeLocal; |
| 5428 |
} |
| 5429 |
|
| 5430 |
if (result != -1) { |
| 5431 |
locals[result] = 1; |
| 5432 |
if (count > 1) |
| 5433 |
locals[result + 1] = 1; |
| 5434 |
if (count > 2) |
| 5435 |
locals[result + 2] = 1; |
| 5436 |
|
| 5437 |
if (result == firstFreeLocal) { |
| 5438 |
for (int i = result + count; i < MAX_LOCALS; i++) { |
| 5439 |
if (locals[i] == 0) { |
| 5440 |
firstFreeLocal = (short) i; |
| 5441 |
if (localsMax < firstFreeLocal) |
| 5442 |
localsMax = firstFreeLocal; |
| 5443 |
return (short) result; |
| 5444 |
} |
| 5445 |
} |
| 5446 |
} else { |
| 5447 |
return (short) result; |
| 5448 |
} |
| 5449 |
} |
| 5450 |
|
| 5451 |
throw Context.reportRuntimeError("Program too complex (out of locals)"); |
| 5452 |
} |
| 5453 |
|
| 5454 |
|
| 5455 |
private void incReferenceWordLocal(short local) |
| 5456 |
{ |
| 5457 |
locals[local]++; |
| 5458 |
} |
| 5459 |
|
| 5460 |
|
| 5461 |
private void decReferenceWordLocal(short local) |
| 5462 |
{ |
| 5463 |
locals[local]--; |
| 5464 |
} |
| 5465 |
|
| 5466 |
private void releaseWordLocal(short local) |
| 5467 |
{ |
| 5468 |
if (local < firstFreeLocal) |
| 5469 |
firstFreeLocal = local; |
| 5470 |
locals[local] = 0; |
| 5471 |
} |
| 5472 |
|
| 5473 |
|
| 5474 |
static final int GENERATOR_TERMINATE = -1; |
| 5475 |
static final int GENERATOR_START = 0; |
| 5476 |
static final int GENERATOR_YIELD_START = 1; |
| 5477 |
|
| 5478 |
ClassFileWriter cfw; |
| 5479 |
Codegen codegen; |
| 5480 |
CompilerEnvirons compilerEnv; |
| 5481 |
ScriptNode scriptOrFn; |
| 5482 |
public int scriptOrFnIndex; |
| 5483 |
private int savedCodeOffset; |
| 5484 |
|
| 5485 |
private OptFunctionNode fnCurrent; |
| 5486 |
|
| 5487 |
private static final int MAX_LOCALS = 1024; |
| 5488 |
private int[] locals; |
| 5489 |
private short firstFreeLocal; |
| 5490 |
private short localsMax; |
| 5491 |
|
| 5492 |
private int itsLineNumber; |
| 5493 |
|
| 5494 |
private boolean hasVarsInRegs; |
| 5495 |
private short[] varRegisters; |
| 5496 |
private boolean inDirectCallFunction; |
| 5497 |
private boolean itsForcedObjectParameters; |
| 5498 |
private int enterAreaStartLabel; |
| 5499 |
private int epilogueLabel; |
| 5500 |
private boolean inLocalBlock; |
| 5501 |
|
| 5502 |
|
| 5503 |
|
| 5504 |
private short variableObjectLocal; |
| 5505 |
private short popvLocal; |
| 5506 |
private short contextLocal; |
| 5507 |
private short argsLocal; |
| 5508 |
private short operationLocal; |
| 5509 |
private short thisObjLocal; |
| 5510 |
private short funObjLocal; |
| 5511 |
private short itsZeroArgArray; |
| 5512 |
private short itsOneArgArray; |
| 5513 |
private short generatorStateLocal; |
| 5514 |
|
| 5515 |
private boolean isGenerator; |
| 5516 |
private int generatorSwitch; |
| 5517 |
private int maxLocals = 0; |
| 5518 |
private int maxStack = 0; |
| 5519 |
|
| 5520 |
private Map<Node,FinallyReturnPoint> finallys; |
| 5521 |
private List<Node> literals; |
| 5522 |
|
| 5523 |
static class FinallyReturnPoint { |
| 5524 |
public List<Integer> jsrPoints = new ArrayList<Integer>(); |
| 5525 |
public int tableLabel = 0; |
| 5526 |
} |
| 5527 |
} |