Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0a4337b7f7b067f750392452401b61881fb75c10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*******************************************************************************
 * Copyright (c) 2006, 2019 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.compiler.codegen;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ClassFile;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.Scope;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
import org.eclipse.jdt.internal.compiler.problem.AbortMethod;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class StackMapFrameCodeStream extends CodeStream {
	public static class ExceptionMarker implements Comparable {
		private TypeBinding binding;
		public int pc;

		public ExceptionMarker(int pc, TypeBinding typeBinding) {
			this.pc = pc;
			this.binding = typeBinding;
		}

		@Override
		public int compareTo(Object o) {
			if (o instanceof ExceptionMarker) {
				return this.pc - ((ExceptionMarker) o).pc;
			}
			return 0;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj instanceof ExceptionMarker) {
				ExceptionMarker marker = (ExceptionMarker) obj;
				return this.pc == marker.pc && this.binding.equals(marker.binding);
			}
			return false;
		}

		public TypeBinding getBinding() {
			return this.binding;
		}

		@Override
		public int hashCode() {
			return this.pc + CharOperation.hashCode(this.binding.constantPoolName());
		}

		@Override
		public String toString() {
			StringBuffer buffer = new StringBuffer();
			buffer.append('(').append(this.pc).append(',').append(this.binding.constantPoolName()).append(')');
			return String.valueOf(buffer);
		}
	}

	static class FramePosition {
		int counter;
	}

	public int[] stateIndexes;
	public int stateIndexesCounter;
	private HashMap framePositions;
	public Set exceptionMarkers;
	public ArrayList stackDepthMarkers;
	public ArrayList stackMarkers;

	public StackMapFrameCodeStream(ClassFile givenClassFile) {
		super(givenClassFile);
		this.generateAttributes |= ClassFileConstants.ATTR_STACK_MAP;
	}

	@Override
	public void addDefinitelyAssignedVariables(Scope scope, int initStateIndex) {
		// Required to fix 1PR0XVS: LFRE:WINNT - Compiler: variable table for method appears incorrect
		for (int i = 0; i < this.visibleLocalsCount; i++) {
			LocalVariableBinding localBinding = this.visibleLocals[i];
			if (localBinding != null) {
				// Check if the local is definitely assigned
				boolean isDefinitelyAssigned = isDefinitelyAssigned(scope, initStateIndex, localBinding);
				if (!isDefinitelyAssigned) {
					continue;
				} else {
					if ((localBinding.initializationCount == 0)
							|| (localBinding.initializationPCs[((localBinding.initializationCount - 1) << 1)
									+ 1] != -1)) {
						/*
						 * There are two cases: 1) there is no initialization interval opened ==> add an opened interval
						 * 2) there is already some initialization intervals but the last one is closed ==> add an
						 * opened interval An opened interval means that the value at
						 * localBinding.initializationPCs[localBinding.initializationCount - 1][1] is equals to -1.
						 * initializationPCs is a collection of pairs of int: first value is the startPC and second
						 * value is the endPC. -1 one for the last value means that the interval is not closed yet.
						 */
						localBinding.recordInitializationStartPC(this.position);
					}
				}
			}
		}
	}

	public void addExceptionMarker(int pc, TypeBinding typeBinding) {
		if (this.exceptionMarkers == null) {
			this.exceptionMarkers = new HashSet();
		}

		this.exceptionMarkers.add(new ExceptionMarker(pc, typeBinding));
	}

	public void addFramePosition(int pc) {
		Integer newEntry = Integer.valueOf(pc);
		FramePosition value;
		if ((value = (FramePosition) this.framePositions.get(newEntry)) != null) {
			value.counter++;
		} else {
			this.framePositions.put(newEntry, new FramePosition());
		}
	}

	@Override
	public void optimizeBranch(int oldPosition, BranchLabel lbl) {
		super.optimizeBranch(oldPosition, lbl);
		removeFramePosition(oldPosition);
	}

	public void removeFramePosition(int pc) {
		Integer entry = Integer.valueOf(pc);
		FramePosition value;
		if ((value = (FramePosition) this.framePositions.get(entry)) != null) {
			value.counter--;
			if (value.counter <= 0) {
				this.framePositions.remove(entry);
			}
		}
	}

	@Override
	public void addVariable(LocalVariableBinding localBinding) {
		if (localBinding.initializationPCs == null) {
			record(localBinding);
		}
		localBinding.recordInitializationStartPC(this.position);
	}

	@Override
	public void recordExpressionType(TypeBinding typeBinding, int delta, boolean adjustStackDepth) {
		if (adjustStackDepth) {
			// optimized goto
			// the break label already adjusted the stack depth (-1 or -2 depending on the return type)
			// we need to adjust back to what it was
			switch (typeBinding.id) {
				case TypeIds.T_long:
				case TypeIds.T_double:
					this.stackDepth += 2;
					break;
				case TypeIds.T_void:
					break;
				default:
					this.stackDepth++;
					break;
			}
		}
	}

	/**
	 * Macro for building a class descriptor object
	 */
	@Override
	public void generateClassLiteralAccessForType(Scope scope, TypeBinding accessedType,
			FieldBinding syntheticFieldBinding) {
		if (accessedType.isBaseType() && accessedType != TypeBinding.NULL) {
			getTYPE(accessedType.id);
			return;
		}

		if (this.targetLevel >= ClassFileConstants.JDK1_5) {
			// generation using the new ldc_w bytecode
			this.ldc(accessedType);
		} else {
			// use in CLDC mode
			BranchLabel endLabel = new BranchLabel(this);
			if (syntheticFieldBinding != null) { // non interface case
				fieldAccess(Opcodes.OPC_getstatic, syntheticFieldBinding, null /* default declaringClass */);
				dup();
				ifnonnull(endLabel);
				pop();
			}

			/*
			 * Macro for building a class descriptor object... using or not a field cache to store it into... this
			 * sequence is responsible for building the actual class descriptor.
			 *
			 * If the fieldCache is set, then it is supposed to be the body of a synthetic access method factoring the
			 * actual descriptor creation out of the invocation site (saving space). If the fieldCache is nil, then we
			 * are dumping the bytecode on the invocation site, since we have no way to get a hand on the field cache to
			 * do better.
			 */

			// Wrap the code in an exception handler to convert a ClassNotFoundException into a NoClassDefError

			ExceptionLabel classNotFoundExceptionHandler = new ExceptionLabel(this,
					TypeBinding.NULL /* represents ClassNotFoundException */);
			classNotFoundExceptionHandler.placeStart();
			this.ldc(accessedType == TypeBinding.NULL ? "java.lang.Object" //$NON-NLS-1$
					: String.valueOf(accessedType.constantPoolName()).replace('/', '.'));
			invokeClassForName();

			/*
			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=37565 if (accessedType == BaseTypes.NullBinding) {
			 * this.ldc("java.lang.Object"); //$NON-NLS-1$ } else if (accessedType.isArrayType()) {
			 * this.ldc(String.valueOf(accessedType.constantPoolName()).replace('/', '.')); } else { // we make it an
			 * array type (to avoid class initialization) this.ldc("[L" +
			 * String.valueOf(accessedType.constantPoolName()).replace('/', '.') + ";"); //$NON-NLS-1$//$NON-NLS-2$ }
			 * this.invokeClassForName(); if (!accessedType.isArrayType()) { // extract the component type, which
			 * doesn't initialize the class this.invokeJavaLangClassGetComponentType(); }
			 */
			/*
			 * We need to protect the runtime code from binary inconsistencies in case the accessedType is missing, the
			 * ClassNotFoundException has to be converted into a NoClassDefError(old ex message), we thus need to build
			 * an exception handler for this one.
			 */
			classNotFoundExceptionHandler.placeEnd();

			if (syntheticFieldBinding != null) { // non interface case
				dup();
				fieldAccess(Opcodes.OPC_putstatic, syntheticFieldBinding, null /* default declaringClass */);
			}
			goto_(endLabel);
			int savedStackDepth = this.stackDepth;
			// Generate the body of the exception handler
			/*
			 * ClassNotFoundException on stack -- the class literal could be doing more things on the stack, which means
			 * that the stack may not be empty at this point in the above code gen. So we save its state and restart it
			 * from 1.
			 */

			pushExceptionOnStack(scope.getJavaLangClassNotFoundException());
			classNotFoundExceptionHandler.place();

			// Transform the current exception, and repush and throw a
			// NoClassDefFoundError(ClassNotFound.getMessage())

			newNoClassDefFoundError();
			dup_x1();
			swap();

			// Retrieve the message from the old exception
			invokeThrowableGetMessage();

			// Send the constructor taking a message string as an argument
			invokeNoClassDefFoundErrorStringConstructor();
			athrow();
			endLabel.place();
			this.stackDepth = savedStackDepth;
		}
	}

	@Override
	public void generateOuterAccess(Object[] mappingSequence, ASTNode invocationSite, Binding target, Scope scope) {
		int currentPosition = this.position;
		super.generateOuterAccess(mappingSequence, invocationSite, target, scope);
		if (currentPosition == this.position) {
			// no code has been generate during outer access => no enclosing instance is available
			throw new AbortMethod(scope.referenceCompilationUnit().compilationResult, null);
		}
	}

	public ExceptionMarker[] getExceptionMarkers() {
		Set exceptionMarkerSet = this.exceptionMarkers;
		if (this.exceptionMarkers == null)
			return null;
		int size = exceptionMarkerSet.size();
		ExceptionMarker[] markers = new ExceptionMarker[size];
		int n = 0;
		for (Iterator iterator = exceptionMarkerSet.iterator(); iterator.hasNext();) {
			markers[n++] = (ExceptionMarker) iterator.next();
		}
		Arrays.sort(markers);
//  System.out.print('[');
//  for (int n = 0; n < size; n++) {
//  	if (n != 0) System.out.print(',');
//  	System.out.print(positions[n]);
//  }
//  System.out.println(']');
		return markers;
	}

	public int[] getFramePositions() {
		Set set = this.framePositions.keySet();
		int size = set.size();
		int[] positions = new int[size];
		int n = 0;
		for (Iterator iterator = set.iterator(); iterator.hasNext();) {
			positions[n++] = ((Integer) iterator.next()).intValue();
		}
		Arrays.sort(positions);
//  System.out.print('[');
//  for (int n = 0; n < size; n++) {
//  	if (n != 0) System.out.print(',');
//  	System.out.print(positions[n]);
//  }
//  System.out.println(']');
		return positions;
	}

	public boolean hasFramePositions() {
		return this.framePositions.size() != 0;
	}

	@Override
	public void init(ClassFile targetClassFile) {
		super.init(targetClassFile);
		this.stateIndexesCounter = 0;
		if (this.framePositions != null) {
			this.framePositions.clear();
		}
		if (this.exceptionMarkers != null) {
			this.exceptionMarkers.clear();
		}
		if (this.stackDepthMarkers != null) {
			this.stackDepthMarkers.clear();
		}
		if (this.stackMarkers != null) {
			this.stackMarkers.clear();
		}
	}

	@Override
	public void initializeMaxLocals(MethodBinding methodBinding) {
		super.initializeMaxLocals(methodBinding);
		if (this.framePositions == null) {
			this.framePositions = new HashMap();
		} else {
			this.framePositions.clear();
		}
	}

	public void popStateIndex() {
		this.stateIndexesCounter--;
	}

	public void pushStateIndex(int naturalExitMergeInitStateIndex) {
		if (this.stateIndexes == null) {
			this.stateIndexes = new int[3];
		}
		int length = this.stateIndexes.length;
		if (length == this.stateIndexesCounter) {
			// resize
			System.arraycopy(this.stateIndexes, 0, (this.stateIndexes = new int[length * 2]), 0, length);
		}
		this.stateIndexes[this.stateIndexesCounter++] = naturalExitMergeInitStateIndex;
	}

	@Override
	public void removeNotDefinitelyAssignedVariables(Scope scope, int initStateIndex) {
		int index = this.visibleLocalsCount;
		loop: for (int i = 0; i < index; i++) {
			LocalVariableBinding localBinding = this.visibleLocals[i];
			if (localBinding != null && localBinding.initializationCount > 0) {
				boolean isDefinitelyAssigned = isDefinitelyAssigned(scope, initStateIndex, localBinding);
				if (!isDefinitelyAssigned) {
					if (this.stateIndexes != null) {
						for (int j = 0, max = this.stateIndexesCounter; j < max; j++) {
							if (isDefinitelyAssigned(scope, this.stateIndexes[j], localBinding)) {
								continue loop;
							}
						}
					}
					localBinding.recordInitializationEndPC(this.position);
				}
			}
		}
	}

	@Override
	public void reset(ClassFile givenClassFile) {
		super.reset(givenClassFile);
		this.stateIndexesCounter = 0;
		if (this.framePositions != null) {
			this.framePositions.clear();
		}
		if (this.exceptionMarkers != null) {
			this.exceptionMarkers.clear();
		}
		if (this.stackDepthMarkers != null) {
			this.stackDepthMarkers.clear();
		}
		if (this.stackMarkers != null) {
			this.stackMarkers.clear();
		}
	}

	@Override
	protected void writePosition(BranchLabel label) {
		super.writePosition(label);
		addFramePosition(label.position);
	}

	@Override
	protected void writePosition(BranchLabel label, int forwardReference) {
		super.writePosition(label, forwardReference);
		addFramePosition(label.position);
	}

	@Override
	protected void writeSignedWord(int pos, int value) {
		super.writeSignedWord(pos, value);
		addFramePosition(this.position);
	}

	@Override
	protected void writeWidePosition(BranchLabel label) {
		super.writeWidePosition(label);
		addFramePosition(label.position);
	}

	@Override
	public void areturn() {
		super.areturn();
		addFramePosition(this.position);
	}

	@Override
	public void ireturn() {
		super.ireturn();
		addFramePosition(this.position);
	}

	@Override
	public void lreturn() {
		super.lreturn();
		addFramePosition(this.position);
	}

	@Override
	public void freturn() {
		super.freturn();
		addFramePosition(this.position);
	}

	@Override
	public void dreturn() {
		super.dreturn();
		addFramePosition(this.position);
	}

	@Override
	public void return_() {
		super.return_();
		addFramePosition(this.position);
	}

	@Override
	public void athrow() {
		super.athrow();
		addFramePosition(this.position);
	}

	@Override
	public void pushExceptionOnStack(TypeBinding binding) {
		super.pushExceptionOnStack(binding);
		addExceptionMarker(this.position, binding);
	}

	@Override
	public void goto_(BranchLabel label) {
		super.goto_(label);
		addFramePosition(this.position);
	}

	@Override
	public void goto_w(BranchLabel label) {
		super.goto_w(label);
		addFramePosition(this.position);
	}

	@Override
	public void resetInWideMode() {
		this.resetSecretLocals();
		super.resetInWideMode();
	}

	@Override
	public void resetForCodeGenUnusedLocals() {
		this.resetSecretLocals();
		super.resetForCodeGenUnusedLocals();
	}

	public void resetSecretLocals() {
		for (int i = 0, max = this.locals.length; i < max; i++) {
			LocalVariableBinding localVariableBinding = this.locals[i];
			if (localVariableBinding != null && localVariableBinding.isSecret()) {
				// all other locals are reinitialized inside the computation of their resolved positions
				localVariableBinding.resetInitializations();
			}
		}
	}
}

Back to the top