1 /*
2 * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
129 /** An output buffer for the constant pool.
130 */
131 ByteBuffer poolbuf = new ByteBuffer(CLASS_BUF_SIZE);
132
133 /** The constant pool writer.
134 */
135 final PoolWriter poolWriter;
136
137 /** The log to use for verbose output.
138 */
139 private final Log log;
140
141 /** The name table. */
142 private final Names names;
143
144 /** Access to files. */
145 private final JavaFileManager fileManager;
146
147 /** The tags and constants used in compressed stackmap. */
148 static final int SAME_FRAME_SIZE = 64;
149 static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
150 static final int SAME_FRAME_EXTENDED = 251;
151 static final int FULL_FRAME = 255;
152 static final int MAX_LOCAL_LENGTH_DIFF = 4;
153
154 /** Get the ClassWriter instance for this context. */
155 public static ClassWriter instance(Context context) {
156 ClassWriter instance = context.get(classWriterKey);
157 if (instance == null)
158 instance = new ClassWriter(context);
159 return instance;
160 }
161
162 /** Construct a class writer, given an options table.
163 */
164 @SuppressWarnings("this-escape")
165 protected ClassWriter(Context context) {
166 context.put(classWriterKey, this);
167
168 log = Log.instance(context);
205 * 'c' -- classes
206 * 'f' -- fields
207 * 'i' -- innerclass attributes
208 * 'm' -- methods
209 * For example, to dump everything:
210 * javac -XDdumpmodifiers=cifm MyProg.java
211 */
212 private boolean dumpClassModifiers; // -XDdumpmodifiers=c
213 private boolean dumpFieldModifiers; // -XDdumpmodifiers=f
214 private boolean dumpInnerClassModifiers; // -XDdumpmodifiers=i
215 private boolean dumpMethodModifiers; // -XDdumpmodifiers=m
216
217
218 /** Return flags as a string, separated by " ".
219 */
220 public static String flagNames(long flags) {
221 StringBuilder sbuf = new StringBuilder();
222 int i = 0;
223 long f = flags & StandardFlags;
224 while (f != 0) {
225 if ((f & 1) != 0) {
226 sbuf.append(" ");
227 sbuf.append(flagName[i]);
228 }
229 f = f >> 1;
230 i++;
231 }
232 return sbuf.toString();
233 }
234 //where
235 private static final String[] flagName = {
236 "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL",
237 "SUPER", "VOLATILE", "TRANSIENT", "NATIVE", "INTERFACE",
238 "ABSTRACT", "STRICTFP"};
239
240 /* ****************************************************************
241 * Output routines
242 ******************************************************************/
243
244 /** Write a character into given byte buffer;
245 * byte buffer will not be grown.
246 */
247 void putChar(ByteBuffer buf, int op, int x) {
248 buf.elems[op ] = (byte)((x >> 8) & 0xFF);
249 buf.elems[op+1] = (byte)((x ) & 0xFF);
250 }
251
252 /** Write an integer into given byte buffer;
253 * byte buffer will not be grown.
254 */
255 void putInt(ByteBuffer buf, int adr, int x) {
256 buf.elems[adr ] = (byte)((x >> 24) & 0xFF);
257 buf.elems[adr+1] = (byte)((x >> 16) & 0xFF);
825 databuf.appendChar(poolWriter.putClass(srvc));
826 databuf.appendChar(impls.size());
827 impls.forEach(impl -> databuf.appendChar(poolWriter.putClass(impl)));
828 });
829
830 endAttr(alenIdx);
831 return 1;
832 }
833
834 /* ********************************************************************
835 * Writing Objects
836 **********************************************************************/
837
838 /** Write "inner classes" attribute.
839 */
840 void writeInnerClasses() {
841 int alenIdx = writeAttr(names.InnerClasses);
842 databuf.appendChar(poolWriter.innerClasses.size());
843 for (ClassSymbol inner : poolWriter.innerClasses) {
844 inner.markAbstractIfNeeded(types);
845 int flags = adjustFlags(inner.flags_field);
846 if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT
847 flags &= ~STRICTFP; //inner classes should not have the strictfp flag set.
848 if (dumpInnerClassModifiers) {
849 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
850 pw.println("INNERCLASS " + inner.name);
851 pw.println("---" + flagNames(flags));
852 }
853 databuf.appendChar(poolWriter.putClass(inner));
854 databuf.appendChar(
855 inner.owner.kind == TYP && !inner.name.isEmpty() ? poolWriter.putClass((ClassSymbol)inner.owner) : 0);
856 databuf.appendChar(
857 !inner.name.isEmpty() ? poolWriter.putName(inner.name) : 0);
858 databuf.appendChar(flags);
859 }
860 endAttr(alenIdx);
861 }
862
863 int writeRecordAttribute(ClassSymbol csym) {
864 int alenIdx = writeAttr(names.Record);
865 Scope s = csym.members();
866 databuf.appendChar(csym.getRecordComponents().size());
867 for (VarSymbol v: csym.getRecordComponents()) {
868 //databuf.appendChar(poolWriter.putMember(v.accessor.head.snd));
869 databuf.appendChar(poolWriter.putName(v.name));
870 databuf.appendChar(poolWriter.putDescriptor(v));
871 int acountIdx = beginAttrs();
872 int acount = 0;
873 acount += writeMemberAttrs(v, true);
874 endAttrs(acountIdx, acount);
875 }
876 endAttr(alenIdx);
877 return 1;
878 }
879
880 /**
881 * Write NestMembers attribute (if needed)
882 */
956 }
957 } while (lastBootstrapMethods < poolWriter.bootstrapMethods.size());
958 databuf.appendChar(poolWriter.bootstrapMethods.size());
959 for (BsmKey bsmKey : poolWriter.bootstrapMethods.keySet()) {
960 //write BSM handle
961 databuf.appendChar(poolWriter.putConstant(bsmKey.bsm));
962 LoadableConstant[] uniqueArgs = bsmKey.staticArgs;
963 //write static args length
964 databuf.appendChar(uniqueArgs.length);
965 //write static args array
966 for (LoadableConstant arg : uniqueArgs) {
967 databuf.appendChar(poolWriter.putConstant(arg));
968 }
969 }
970 endAttr(alenIdx);
971 }
972
973 /** Write field symbol, entering all references into constant pool.
974 */
975 void writeField(VarSymbol v) {
976 int flags = adjustFlags(v.flags());
977 databuf.appendChar(flags);
978 if (dumpFieldModifiers) {
979 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
980 pw.println("FIELD " + v.name);
981 pw.println("---" + flagNames(v.flags()));
982 }
983 databuf.appendChar(poolWriter.putName(v.name));
984 databuf.appendChar(poolWriter.putDescriptor(v));
985 int acountIdx = beginAttrs();
986 int acount = 0;
987 if (v.getConstValue() != null) {
988 int alenIdx = writeAttr(names.ConstantValue);
989 databuf.appendChar(poolWriter.putConstant(v.getConstValue()));
990 endAttr(alenIdx);
991 acount++;
992 }
993 acount += writeMemberAttrs(v, false);
994 acount += writeExtraAttributes(v);
995 endAttrs(acountIdx, acount);
996 }
997
998 /** Write method symbol, entering all references into constant pool.
999 */
1000 void writeMethod(MethodSymbol m) {
1001 int flags = adjustFlags(m.flags());
1002 databuf.appendChar(flags);
1003 if (dumpMethodModifiers) {
1004 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1005 pw.println("METHOD " + m.name);
1006 pw.println("---" + flagNames(m.flags()));
1007 }
1008 databuf.appendChar(poolWriter.putName(m.name));
1009 databuf.appendChar(poolWriter.putDescriptor(m));
1010 int acountIdx = beginAttrs();
1011 int acount = 0;
1012 if (m.code != null) {
1013 int alenIdx = writeAttr(names.Code);
1014 writeCode(m.code);
1015 m.code = null; // to conserve space
1016 endAttr(alenIdx);
1017 acount++;
1018 }
1019 List<Type> thrown = m.erasure(types).getThrownTypes();
1020 if (thrown.nonEmpty()) {
1021 int alenIdx = writeAttr(names.Exceptions);
1022 databuf.appendChar(thrown.length());
1023 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1024 databuf.appendChar(poolWriter.putClass(l.head));
1025 endAttr(alenIdx);
1026 acount++;
1027 }
1028 if (m.defaultValue != null) {
1029 int alenIdx = writeAttr(names.AnnotationDefault);
1280 break;
1281 case UNINITIALIZED_THIS:
1282 if (debugstackmap) System.out.print("uninit_this");
1283 databuf.appendByte(6);
1284 break;
1285 case UNINITIALIZED_OBJECT:
1286 { UninitializedType uninitType = (UninitializedType)t;
1287 databuf.appendByte(8);
1288 if (debugstackmap) System.out.print("uninit_object@" + uninitType.offset);
1289 databuf.appendChar(uninitType.offset);
1290 }
1291 break;
1292 default:
1293 throw new AssertionError();
1294 }
1295 }
1296
1297 /** An entry in the JSR202 StackMapTable */
1298 abstract static class StackMapTableFrame {
1299 abstract int getFrameType();
1300
1301 void write(ClassWriter writer) {
1302 int frameType = getFrameType();
1303 writer.databuf.appendByte(frameType);
1304 if (writer.debugstackmap) System.out.print(" frame_type=" + frameType);
1305 }
1306
1307 static class SameFrame extends StackMapTableFrame {
1308 final int offsetDelta;
1309 SameFrame(int offsetDelta) {
1310 this.offsetDelta = offsetDelta;
1311 }
1312 int getFrameType() {
1313 return (offsetDelta < SAME_FRAME_SIZE) ? offsetDelta : SAME_FRAME_EXTENDED;
1314 }
1315 @Override
1316 void write(ClassWriter writer) {
1317 super.write(writer);
1318 if (getFrameType() == SAME_FRAME_EXTENDED) {
1319 writer.databuf.appendChar(offsetDelta);
1320 if (writer.debugstackmap){
1321 System.out.print(" offset_delta=" + offsetDelta);
1322 }
1323 }
1324 }
1325 }
1326
1327 static class SameLocals1StackItemFrame extends StackMapTableFrame {
1328 final int offsetDelta;
1329 final Type stack;
1330 SameLocals1StackItemFrame(int offsetDelta, Type stack) {
1331 this.offsetDelta = offsetDelta;
1332 this.stack = stack;
1333 }
1334 int getFrameType() {
1335 return (offsetDelta < SAME_FRAME_SIZE) ?
1336 (SAME_FRAME_SIZE + offsetDelta) :
1337 SAME_LOCALS_1_STACK_ITEM_EXTENDED;
1338 }
1339 @Override
1340 void write(ClassWriter writer) {
1341 super.write(writer);
1342 if (getFrameType() == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
1343 writer.databuf.appendChar(offsetDelta);
1344 if (writer.debugstackmap) {
1345 System.out.print(" offset_delta=" + offsetDelta);
1346 }
1347 }
1348 if (writer.debugstackmap) {
1349 System.out.print(" stack[" + 0 + "]=");
1350 }
1351 writer.writeStackMapType(stack);
1352 }
1353 }
1354
1355 static class ChopFrame extends StackMapTableFrame {
1356 final int frameType;
1357 final int offsetDelta;
1358 ChopFrame(int frameType, int offsetDelta) {
1359 this.frameType = frameType;
1360 this.offsetDelta = offsetDelta;
1361 }
1362 int getFrameType() { return frameType; }
1363 @Override
1364 void write(ClassWriter writer) {
1365 super.write(writer);
1366 writer.databuf.appendChar(offsetDelta);
1367 if (writer.debugstackmap) {
1368 System.out.print(" offset_delta=" + offsetDelta);
1369 }
1370 }
1371 }
1372
1373 static class AppendFrame extends StackMapTableFrame {
1374 final int frameType;
1375 final int offsetDelta;
1376 final Type[] locals;
1377 AppendFrame(int frameType, int offsetDelta, Type[] locals) {
1378 this.frameType = frameType;
1379 this.offsetDelta = offsetDelta;
1380 this.locals = locals;
1381 }
1382 int getFrameType() { return frameType; }
1383 @Override
1384 void write(ClassWriter writer) {
1385 super.write(writer);
1386 writer.databuf.appendChar(offsetDelta);
1387 if (writer.debugstackmap) {
1388 System.out.print(" offset_delta=" + offsetDelta);
1389 }
1390 for (int i=0; i<locals.length; i++) {
1391 if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1392 writer.writeStackMapType(locals[i]);
1393 }
1394 }
1395 }
1396
1397 static class FullFrame extends StackMapTableFrame {
1398 final int offsetDelta;
1399 final Type[] locals;
1400 final Type[] stack;
1401 FullFrame(int offsetDelta, Type[] locals, Type[] stack) {
1402 this.offsetDelta = offsetDelta;
1403 this.locals = locals;
1404 this.stack = stack;
1405 }
1406 int getFrameType() { return FULL_FRAME; }
1407 @Override
1408 void write(ClassWriter writer) {
1409 super.write(writer);
1410 writer.databuf.appendChar(offsetDelta);
1411 writer.databuf.appendChar(locals.length);
1412 if (writer.debugstackmap) {
1413 System.out.print(" offset_delta=" + offsetDelta);
1414 System.out.print(" nlocals=" + locals.length);
1415 }
1416 for (int i=0; i<locals.length; i++) {
1417 if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1418 writer.writeStackMapType(locals[i]);
1419 }
1420
1421 writer.databuf.appendChar(stack.length);
1422 if (writer.debugstackmap) { System.out.print(" nstack=" + stack.length); }
1423 for (int i=0; i<stack.length; i++) {
1424 if (writer.debugstackmap) System.out.print(" stack[" + i + "]=");
1425 writer.writeStackMapType(stack[i]);
1426 }
1427 }
1428 }
1429
1430 /** Compare this frame with the previous frame and produce
1431 * an entry of compressed stack map frame. */
1432 static StackMapTableFrame getInstance(Code.StackMapFrame this_frame,
1433 int prev_pc,
1434 Type[] prev_locals,
1435 Types types) {
1436 Type[] locals = this_frame.locals;
1437 Type[] stack = this_frame.stack;
1438 int offset_delta = this_frame.pc - prev_pc - 1;
1439 if (stack.length == 1) {
1440 if (locals.length == prev_locals.length
1441 && compare(prev_locals, locals, types) == 0) {
1442 return new SameLocals1StackItemFrame(offset_delta, stack[0]);
1443 }
1444 } else if (stack.length == 0) {
1445 int diff_length = compare(prev_locals, locals, types);
1446 if (diff_length == 0) {
1447 return new SameFrame(offset_delta);
1448 } else if (-MAX_LOCAL_LENGTH_DIFF < diff_length && diff_length < 0) {
1449 // APPEND
1450 Type[] local_diff = new Type[-diff_length];
1451 for (int i=prev_locals.length, j=0; i<locals.length; i++,j++) {
1452 local_diff[j] = locals[i];
1453 }
1454 return new AppendFrame(SAME_FRAME_EXTENDED - diff_length,
1455 offset_delta,
1456 local_diff);
1457 } else if (0 < diff_length && diff_length < MAX_LOCAL_LENGTH_DIFF) {
1458 // CHOP
1459 return new ChopFrame(SAME_FRAME_EXTENDED - diff_length,
1460 offset_delta);
1461 }
1462 }
1463 // FULL_FRAME
1464 return new FullFrame(offset_delta, locals, stack);
1465 }
1466
1467 static boolean isInt(Type t) {
1468 return (t.getTag().isStrictSubRangeOf(INT) || t.hasTag(BOOLEAN));
1469 }
1470
1471 static boolean isSameType(Type t1, Type t2, Types types) {
1472 if (t1 == null) { return t2 == null; }
1473 if (t2 == null) { return false; }
1474
1475 if (isInt(t1) && isInt(t2)) { return true; }
1476
1477 if (t1.hasTag(UNINITIALIZED_THIS)) {
1478 return t2.hasTag(UNINITIALIZED_THIS);
1479 } else if (t1.hasTag(UNINITIALIZED_OBJECT)) {
1480 if (t2.hasTag(UNINITIALIZED_OBJECT)) {
1481 return ((UninitializedType)t1).offset == ((UninitializedType)t2).offset;
1482 } else {
1483 return false;
1484 }
1567 }
1568 return outFile; // may be null if write failed
1569 }
1570
1571 /** Write class `c' to outstream `out'.
1572 */
1573 public void writeClassFile(OutputStream out, ClassSymbol c)
1574 throws IOException, PoolOverflow, StringOverflow {
1575 Assert.check((c.flags() & COMPOUND) == 0);
1576 databuf.reset();
1577 poolbuf.reset();
1578
1579 Type supertype = types.supertype(c.type);
1580 List<Type> interfaces = types.interfaces(c.type);
1581 List<Type> typarams = c.type.getTypeArguments();
1582
1583 int flags;
1584 if (c.owner.kind == MDL) {
1585 flags = ACC_MODULE;
1586 } else {
1587 flags = adjustFlags(c.flags() & ~DEFAULT);
1588 if ((flags & PROTECTED) != 0) flags |= PUBLIC;
1589 flags = flags & ClassFlags & ~STRICTFP;
1590 if ((flags & INTERFACE) == 0) flags |= ACC_SUPER;
1591 }
1592
1593 if (dumpClassModifiers) {
1594 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1595 pw.println();
1596 pw.println("CLASSFILE " + c.getQualifiedName());
1597 pw.println("---" + flagNames(flags));
1598 }
1599 databuf.appendChar(flags);
1600
1601 if (c.owner.kind == MDL) {
1602 PackageSymbol unnamed = ((ModuleSymbol) c.owner).unnamedPackage;
1603 databuf.appendChar(poolWriter.putClass(new ClassSymbol(0, names.module_info, unnamed)));
1604 } else {
1605 databuf.appendChar(poolWriter.putClass(c));
1606 }
1607 databuf.appendChar(supertype.hasTag(CLASS) ? poolWriter.putClass((ClassSymbol)supertype.tsym) : 0);
1608 databuf.appendChar(interfaces.length());
1609 for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
1610 databuf.appendChar(poolWriter.putClass((ClassSymbol)l.head.tsym));
1696 }
1697
1698 if (c.isRecord()) {
1699 acount += writeRecordAttribute(c);
1700 }
1701
1702 if (target.hasSealedClasses()) {
1703 acount += writePermittedSubclassesIfNeeded(c);
1704 }
1705
1706 if (!poolWriter.bootstrapMethods.isEmpty()) {
1707 writeBootstrapMethods();
1708 acount++;
1709 }
1710
1711 if (!poolWriter.innerClasses.isEmpty()) {
1712 writeInnerClasses();
1713 acount++;
1714 }
1715
1716 endAttrs(acountIdx, acount);
1717
1718 out.write(poolbuf.elems, 0, poolbuf.length);
1719
1720 poolWriter.writePool(out);
1721 poolWriter.reset(); // to save space
1722
1723 out.write(databuf.elems, 0, databuf.length);
1724 }
1725
1726 /**Allows subclasses to write additional class attributes
1727 *
1728 * @return the number of attributes written
1729 */
1730 protected int writeExtraClassAttributes(ClassSymbol c) {
1731 return 0;
1732 }
1733
1734 /**Allows friends to write additional attributes
1735 *
1736 * @return the number of attributes written
1737 */
1738 protected int writeExtraAttributes(Symbol sym) {
1739 int i = 0;
1740 for (ToIntFunction<Symbol> hook : extraAttributeHooks) {
1741 i += hook.applyAsInt(sym);
1742 }
1743 return i;
1744 }
1745
1746 int adjustFlags(final long flags) {
1747 int result = (int)flags;
1748
1749 // Elide strictfp bit in class files
1750 if (target.obsoleteAccStrict())
1751 result &= ~STRICTFP;
1752
1753 if ((flags & BRIDGE) != 0)
1754 result |= ACC_BRIDGE;
1755 if ((flags & VARARGS) != 0)
1756 result |= ACC_VARARGS;
1757 if ((flags & DEFAULT) != 0)
1758 result &= ~ABSTRACT;
1759 return result;
1760 }
1761
1762 long getLastModified(FileObject filename) {
1763 return filename.getLastModified();
1764 }
1765 }
|
1 /*
2 * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
129 /** An output buffer for the constant pool.
130 */
131 ByteBuffer poolbuf = new ByteBuffer(CLASS_BUF_SIZE);
132
133 /** The constant pool writer.
134 */
135 final PoolWriter poolWriter;
136
137 /** The log to use for verbose output.
138 */
139 private final Log log;
140
141 /** The name table. */
142 private final Names names;
143
144 /** Access to files. */
145 private final JavaFileManager fileManager;
146
147 /** The tags and constants used in compressed stackmap. */
148 static final int SAME_FRAME_SIZE = 64;
149 static final int EARLY_LARVAL = 246;
150 static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
151 static final int SAME_FRAME_EXTENDED = 251;
152 static final int FULL_FRAME = 255;
153 static final int MAX_LOCAL_LENGTH_DIFF = 4;
154
155 /** Get the ClassWriter instance for this context. */
156 public static ClassWriter instance(Context context) {
157 ClassWriter instance = context.get(classWriterKey);
158 if (instance == null)
159 instance = new ClassWriter(context);
160 return instance;
161 }
162
163 /** Construct a class writer, given an options table.
164 */
165 @SuppressWarnings("this-escape")
166 protected ClassWriter(Context context) {
167 context.put(classWriterKey, this);
168
169 log = Log.instance(context);
206 * 'c' -- classes
207 * 'f' -- fields
208 * 'i' -- innerclass attributes
209 * 'm' -- methods
210 * For example, to dump everything:
211 * javac -XDdumpmodifiers=cifm MyProg.java
212 */
213 private boolean dumpClassModifiers; // -XDdumpmodifiers=c
214 private boolean dumpFieldModifiers; // -XDdumpmodifiers=f
215 private boolean dumpInnerClassModifiers; // -XDdumpmodifiers=i
216 private boolean dumpMethodModifiers; // -XDdumpmodifiers=m
217
218
219 /** Return flags as a string, separated by " ".
220 */
221 public static String flagNames(long flags) {
222 StringBuilder sbuf = new StringBuilder();
223 int i = 0;
224 long f = flags & StandardFlags;
225 while (f != 0) {
226 if ((f & 1) != 0 && flagName[i] != "") {
227 sbuf.append(" ");
228 sbuf.append(flagName[i]);
229 }
230 f = f >> 1;
231 i++;
232 }
233 return sbuf.toString();
234 }
235 //where
236 private static final String[] flagName = {
237 "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL",
238 // the empty position should be for synchronized but right now we don't have any test checking it
239 "", "VOLATILE", "TRANSIENT", "NATIVE", "INTERFACE",
240 "ABSTRACT", "STRICTFP"};
241
242 /* ****************************************************************
243 * Output routines
244 ******************************************************************/
245
246 /** Write a character into given byte buffer;
247 * byte buffer will not be grown.
248 */
249 void putChar(ByteBuffer buf, int op, int x) {
250 buf.elems[op ] = (byte)((x >> 8) & 0xFF);
251 buf.elems[op+1] = (byte)((x ) & 0xFF);
252 }
253
254 /** Write an integer into given byte buffer;
255 * byte buffer will not be grown.
256 */
257 void putInt(ByteBuffer buf, int adr, int x) {
258 buf.elems[adr ] = (byte)((x >> 24) & 0xFF);
259 buf.elems[adr+1] = (byte)((x >> 16) & 0xFF);
827 databuf.appendChar(poolWriter.putClass(srvc));
828 databuf.appendChar(impls.size());
829 impls.forEach(impl -> databuf.appendChar(poolWriter.putClass(impl)));
830 });
831
832 endAttr(alenIdx);
833 return 1;
834 }
835
836 /* ********************************************************************
837 * Writing Objects
838 **********************************************************************/
839
840 /** Write "inner classes" attribute.
841 */
842 void writeInnerClasses() {
843 int alenIdx = writeAttr(names.InnerClasses);
844 databuf.appendChar(poolWriter.innerClasses.size());
845 for (ClassSymbol inner : poolWriter.innerClasses) {
846 inner.markAbstractIfNeeded(types);
847 int flags = adjustFlags(inner, inner.flags_field);
848 if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT
849 if (dumpInnerClassModifiers) {
850 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
851 pw.println("INNERCLASS " + inner.name);
852 pw.println("---" + flagNames(flags));
853 }
854 databuf.appendChar(poolWriter.putClass(inner));
855 databuf.appendChar(
856 inner.owner.kind == TYP && !inner.name.isEmpty() ? poolWriter.putClass((ClassSymbol)inner.owner) : 0);
857 databuf.appendChar(
858 !inner.name.isEmpty() ? poolWriter.putName(inner.name) : 0);
859 databuf.appendChar(flags);
860 }
861 endAttr(alenIdx);
862 }
863
864 /** Write out "LoadableDescriptors" attribute by enumerating the value classes encountered in field/method descriptors during this compilation.
865 */
866 void writeLoadableDescriptorsAttribute() {
867 int alenIdx = writeAttr(names.LoadableDescriptors);
868 databuf.appendChar(poolWriter.loadableDescriptors.size());
869 for (Symbol c : poolWriter.loadableDescriptors) {
870 databuf.appendChar(poolWriter.putDescriptor(c));
871 }
872 endAttr(alenIdx);
873 }
874
875 int writeRecordAttribute(ClassSymbol csym) {
876 int alenIdx = writeAttr(names.Record);
877 Scope s = csym.members();
878 databuf.appendChar(csym.getRecordComponents().size());
879 for (VarSymbol v: csym.getRecordComponents()) {
880 //databuf.appendChar(poolWriter.putMember(v.accessor.head.snd));
881 databuf.appendChar(poolWriter.putName(v.name));
882 databuf.appendChar(poolWriter.putDescriptor(v));
883 int acountIdx = beginAttrs();
884 int acount = 0;
885 acount += writeMemberAttrs(v, true);
886 endAttrs(acountIdx, acount);
887 }
888 endAttr(alenIdx);
889 return 1;
890 }
891
892 /**
893 * Write NestMembers attribute (if needed)
894 */
968 }
969 } while (lastBootstrapMethods < poolWriter.bootstrapMethods.size());
970 databuf.appendChar(poolWriter.bootstrapMethods.size());
971 for (BsmKey bsmKey : poolWriter.bootstrapMethods.keySet()) {
972 //write BSM handle
973 databuf.appendChar(poolWriter.putConstant(bsmKey.bsm));
974 LoadableConstant[] uniqueArgs = bsmKey.staticArgs;
975 //write static args length
976 databuf.appendChar(uniqueArgs.length);
977 //write static args array
978 for (LoadableConstant arg : uniqueArgs) {
979 databuf.appendChar(poolWriter.putConstant(arg));
980 }
981 }
982 endAttr(alenIdx);
983 }
984
985 /** Write field symbol, entering all references into constant pool.
986 */
987 void writeField(VarSymbol v) {
988 int flags = adjustFlags(v, v.flags());
989 databuf.appendChar(flags);
990 if (dumpFieldModifiers) {
991 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
992 pw.println("FIELD " + v.name);
993 pw.println("---" + flagNames(v.flags()));
994 }
995 databuf.appendChar(poolWriter.putName(v.name));
996 databuf.appendChar(poolWriter.putDescriptor(v));
997 Type fldType = v.erasure(types);
998 if (fldType.requiresLoadableDescriptors(v.owner)) {
999 poolWriter.enterLoadableDescriptorsClass(fldType.tsym);
1000 if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
1001 preview.markUsesPreview(null);
1002 }
1003 }
1004 int acountIdx = beginAttrs();
1005 int acount = 0;
1006 if (v.getConstValue() != null) {
1007 int alenIdx = writeAttr(names.ConstantValue);
1008 databuf.appendChar(poolWriter.putConstant(v.getConstValue()));
1009 endAttr(alenIdx);
1010 acount++;
1011 }
1012 acount += writeMemberAttrs(v, false);
1013 acount += writeExtraAttributes(v);
1014 endAttrs(acountIdx, acount);
1015 }
1016
1017 /** Write method symbol, entering all references into constant pool.
1018 */
1019 void writeMethod(MethodSymbol m) {
1020 int flags = adjustFlags(m, m.flags());
1021 databuf.appendChar(flags);
1022 if (dumpMethodModifiers) {
1023 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1024 pw.println("METHOD " + m.name);
1025 pw.println("---" + flagNames(m.flags()));
1026 }
1027 databuf.appendChar(poolWriter.putName(m.name));
1028 databuf.appendChar(poolWriter.putDescriptor(m));
1029 MethodType mtype = (MethodType) m.externalType(types);
1030 for (Type t : mtype.getParameterTypes()) {
1031 if (t.requiresLoadableDescriptors(m.owner)) {
1032 poolWriter.enterLoadableDescriptorsClass(t.tsym);
1033 if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
1034 preview.markUsesPreview(null);
1035 }
1036 }
1037 }
1038 Type returnType = mtype.getReturnType();
1039 if (returnType.requiresLoadableDescriptors(m.owner)) {
1040 poolWriter.enterLoadableDescriptorsClass(returnType.tsym);
1041 if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
1042 preview.markUsesPreview(null);
1043 }
1044 }
1045 int acountIdx = beginAttrs();
1046 int acount = 0;
1047 if (m.code != null) {
1048 int alenIdx = writeAttr(names.Code);
1049 writeCode(m.code);
1050 m.code = null; // to conserve space
1051 endAttr(alenIdx);
1052 acount++;
1053 }
1054 List<Type> thrown = m.erasure(types).getThrownTypes();
1055 if (thrown.nonEmpty()) {
1056 int alenIdx = writeAttr(names.Exceptions);
1057 databuf.appendChar(thrown.length());
1058 for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1059 databuf.appendChar(poolWriter.putClass(l.head));
1060 endAttr(alenIdx);
1061 acount++;
1062 }
1063 if (m.defaultValue != null) {
1064 int alenIdx = writeAttr(names.AnnotationDefault);
1315 break;
1316 case UNINITIALIZED_THIS:
1317 if (debugstackmap) System.out.print("uninit_this");
1318 databuf.appendByte(6);
1319 break;
1320 case UNINITIALIZED_OBJECT:
1321 { UninitializedType uninitType = (UninitializedType)t;
1322 databuf.appendByte(8);
1323 if (debugstackmap) System.out.print("uninit_object@" + uninitType.offset);
1324 databuf.appendChar(uninitType.offset);
1325 }
1326 break;
1327 default:
1328 throw new AssertionError();
1329 }
1330 }
1331
1332 /** An entry in the JSR202 StackMapTable */
1333 abstract static class StackMapTableFrame {
1334 abstract int getFrameType();
1335 int pc;
1336
1337 StackMapTableFrame(int pc) {
1338 this.pc = pc;
1339 }
1340
1341 void write(ClassWriter writer) {
1342 int frameType = getFrameType();
1343 writer.databuf.appendByte(frameType);
1344 if (writer.debugstackmap) System.out.println(" frame_type=" + frameType + " bytecode offset " + pc);
1345 }
1346
1347 static class SameFrame extends StackMapTableFrame {
1348 final int offsetDelta;
1349 SameFrame(int pc, int offsetDelta) {
1350 super(pc);
1351 this.offsetDelta = offsetDelta;
1352 }
1353 int getFrameType() {
1354 return (offsetDelta < SAME_FRAME_SIZE) ? offsetDelta : SAME_FRAME_EXTENDED;
1355 }
1356 @Override
1357 void write(ClassWriter writer) {
1358 super.write(writer);
1359 if (getFrameType() == SAME_FRAME_EXTENDED) {
1360 writer.databuf.appendChar(offsetDelta);
1361 if (writer.debugstackmap){
1362 System.out.print(" offset_delta=" + offsetDelta);
1363 }
1364 }
1365 }
1366 }
1367
1368 static class SameLocals1StackItemFrame extends StackMapTableFrame {
1369 final int offsetDelta;
1370 final Type stack;
1371 SameLocals1StackItemFrame(int pc, int offsetDelta, Type stack) {
1372 super(pc);
1373 this.offsetDelta = offsetDelta;
1374 this.stack = stack;
1375 }
1376 int getFrameType() {
1377 return (offsetDelta < SAME_FRAME_SIZE) ?
1378 (SAME_FRAME_SIZE + offsetDelta) :
1379 SAME_LOCALS_1_STACK_ITEM_EXTENDED;
1380 }
1381 @Override
1382 void write(ClassWriter writer) {
1383 super.write(writer);
1384 if (getFrameType() == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
1385 writer.databuf.appendChar(offsetDelta);
1386 if (writer.debugstackmap) {
1387 System.out.print(" offset_delta=" + offsetDelta);
1388 }
1389 }
1390 if (writer.debugstackmap) {
1391 System.out.print(" stack[" + 0 + "]=");
1392 }
1393 writer.writeStackMapType(stack);
1394 }
1395 }
1396
1397 static class ChopFrame extends StackMapTableFrame {
1398 final int frameType;
1399 final int offsetDelta;
1400 ChopFrame(int pc, int frameType, int offsetDelta) {
1401 super(pc);
1402 this.frameType = frameType;
1403 this.offsetDelta = offsetDelta;
1404 }
1405 int getFrameType() { return frameType; }
1406 @Override
1407 void write(ClassWriter writer) {
1408 super.write(writer);
1409 writer.databuf.appendChar(offsetDelta);
1410 if (writer.debugstackmap) {
1411 System.out.print(" offset_delta=" + offsetDelta);
1412 }
1413 }
1414 }
1415
1416 static class AppendFrame extends StackMapTableFrame {
1417 final int frameType;
1418 final int offsetDelta;
1419 final Type[] locals;
1420 AppendFrame(int pc, int frameType, int offsetDelta, Type[] locals) {
1421 super(pc);
1422 this.frameType = frameType;
1423 this.offsetDelta = offsetDelta;
1424 this.locals = locals;
1425 }
1426 int getFrameType() { return frameType; }
1427 @Override
1428 void write(ClassWriter writer) {
1429 super.write(writer);
1430 writer.databuf.appendChar(offsetDelta);
1431 if (writer.debugstackmap) {
1432 System.out.print(" offset_delta=" + offsetDelta);
1433 }
1434 for (int i=0; i<locals.length; i++) {
1435 if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1436 writer.writeStackMapType(locals[i]);
1437 }
1438 }
1439 }
1440
1441 static class FullFrame extends StackMapTableFrame {
1442 final int offsetDelta;
1443 final Type[] locals;
1444 final Type[] stack;
1445 FullFrame(int pc, int offsetDelta, Type[] locals, Type[] stack) {
1446 super(pc);
1447 this.offsetDelta = offsetDelta;
1448 this.locals = locals;
1449 this.stack = stack;
1450 }
1451 int getFrameType() { return FULL_FRAME; }
1452 @Override
1453 void write(ClassWriter writer) {
1454 super.write(writer);
1455 writer.databuf.appendChar(offsetDelta);
1456 writer.databuf.appendChar(locals.length);
1457 if (writer.debugstackmap) {
1458 System.out.print(" offset_delta=" + offsetDelta);
1459 System.out.print(" nlocals=" + locals.length);
1460 }
1461 for (int i=0; i<locals.length; i++) {
1462 if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1463 writer.writeStackMapType(locals[i]);
1464 }
1465
1466 writer.databuf.appendChar(stack.length);
1467 if (writer.debugstackmap) { System.out.print(" nstack=" + stack.length); }
1468 for (int i=0; i<stack.length; i++) {
1469 if (writer.debugstackmap) System.out.print(" stack[" + i + "]=");
1470 writer.writeStackMapType(stack[i]);
1471 }
1472 }
1473 }
1474
1475 static class EarlyLarvalFrame extends StackMapTableFrame {
1476 final StackMapTableFrame base;
1477 Set<VarSymbol> unsetFields;
1478
1479 EarlyLarvalFrame(StackMapTableFrame base, Set<VarSymbol> unsetFields) {
1480 super(base.pc);
1481 Assert.check(!(base instanceof EarlyLarvalFrame));
1482 this.base = base;
1483 this.unsetFields = unsetFields == null ? Set.of() : unsetFields;
1484 }
1485
1486 int getFrameType() { return EARLY_LARVAL; }
1487
1488 @Override
1489 void write(ClassWriter writer) {
1490 super.write(writer);
1491 writer.databuf.appendChar(unsetFields.size());
1492 if (writer.debugstackmap) {
1493 System.out.println(" # writing: EarlyLarval stackmap frame with " + unsetFields.size() + " fields");
1494 }
1495 for (VarSymbol vsym : unsetFields) {
1496 int index = writer.poolWriter.putNameAndType(vsym);
1497 writer.databuf.appendChar(index);
1498 if (writer.debugstackmap) {
1499 System.out.println(" #writing unset field: " + index + ", with name: " + vsym.name.toString());
1500 }
1501 }
1502 base.write(writer);
1503 }
1504 }
1505
1506 /** Compare this frame with the previous frame and produce
1507 * an entry of compressed stack map frame. */
1508 static StackMapTableFrame getInstance(Code.StackMapFrame this_frame,
1509 Code.StackMapFrame prevFrame,
1510 Types types,
1511 int pc) {
1512 Type[] locals = this_frame.locals;
1513 Type[] stack = this_frame.stack;
1514 int offset_delta = this_frame.pc - prevFrame.pc - 1;
1515 if (stack.length == 1) {
1516 if (locals.length == prevFrame.locals.length
1517 && compare(prevFrame.locals, locals, types) == 0) {
1518 return new SameLocals1StackItemFrame(pc, offset_delta, stack[0]);
1519 }
1520 } else if (stack.length == 0) {
1521 int diff_length = compare(prevFrame.locals, locals, types);
1522 if (diff_length == 0) {
1523 return new SameFrame(pc, offset_delta);
1524 } else if (-MAX_LOCAL_LENGTH_DIFF < diff_length && diff_length < 0) {
1525 // APPEND
1526 Type[] local_diff = new Type[-diff_length];
1527 for (int i=prevFrame.locals.length, j=0; i<locals.length; i++,j++) {
1528 local_diff[j] = locals[i];
1529 }
1530 return new AppendFrame(pc, SAME_FRAME_EXTENDED - diff_length,
1531 offset_delta,
1532 local_diff);
1533 } else if (0 < diff_length && diff_length < MAX_LOCAL_LENGTH_DIFF) {
1534 // CHOP
1535 return new ChopFrame(pc, SAME_FRAME_EXTENDED - diff_length,
1536 offset_delta);
1537 }
1538 }
1539 // FULL_FRAME
1540 return new FullFrame(pc, offset_delta, locals, stack);
1541 }
1542
1543 static boolean isInt(Type t) {
1544 return (t.getTag().isStrictSubRangeOf(INT) || t.hasTag(BOOLEAN));
1545 }
1546
1547 static boolean isSameType(Type t1, Type t2, Types types) {
1548 if (t1 == null) { return t2 == null; }
1549 if (t2 == null) { return false; }
1550
1551 if (isInt(t1) && isInt(t2)) { return true; }
1552
1553 if (t1.hasTag(UNINITIALIZED_THIS)) {
1554 return t2.hasTag(UNINITIALIZED_THIS);
1555 } else if (t1.hasTag(UNINITIALIZED_OBJECT)) {
1556 if (t2.hasTag(UNINITIALIZED_OBJECT)) {
1557 return ((UninitializedType)t1).offset == ((UninitializedType)t2).offset;
1558 } else {
1559 return false;
1560 }
1643 }
1644 return outFile; // may be null if write failed
1645 }
1646
1647 /** Write class `c' to outstream `out'.
1648 */
1649 public void writeClassFile(OutputStream out, ClassSymbol c)
1650 throws IOException, PoolOverflow, StringOverflow {
1651 Assert.check((c.flags() & COMPOUND) == 0);
1652 databuf.reset();
1653 poolbuf.reset();
1654
1655 Type supertype = types.supertype(c.type);
1656 List<Type> interfaces = types.interfaces(c.type);
1657 List<Type> typarams = c.type.getTypeArguments();
1658
1659 int flags;
1660 if (c.owner.kind == MDL) {
1661 flags = ACC_MODULE;
1662 } else {
1663 long originalFlags = c.flags();
1664 flags = adjustFlags(c, c.flags() & ~(DEFAULT | STRICTFP));
1665 if ((flags & PROTECTED) != 0) flags |= PUBLIC;
1666 flags = flags & ClassFlags;
1667 flags |= (originalFlags & IDENTITY_TYPE) != 0 ? ACC_IDENTITY : flags;
1668 }
1669
1670 if (dumpClassModifiers) {
1671 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1672 pw.println();
1673 pw.println("CLASSFILE " + c.getQualifiedName());
1674 pw.println("---" + flagNames(flags));
1675 }
1676 databuf.appendChar(flags);
1677
1678 if (c.owner.kind == MDL) {
1679 PackageSymbol unnamed = ((ModuleSymbol) c.owner).unnamedPackage;
1680 databuf.appendChar(poolWriter.putClass(new ClassSymbol(0, names.module_info, unnamed)));
1681 } else {
1682 databuf.appendChar(poolWriter.putClass(c));
1683 }
1684 databuf.appendChar(supertype.hasTag(CLASS) ? poolWriter.putClass((ClassSymbol)supertype.tsym) : 0);
1685 databuf.appendChar(interfaces.length());
1686 for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
1687 databuf.appendChar(poolWriter.putClass((ClassSymbol)l.head.tsym));
1773 }
1774
1775 if (c.isRecord()) {
1776 acount += writeRecordAttribute(c);
1777 }
1778
1779 if (target.hasSealedClasses()) {
1780 acount += writePermittedSubclassesIfNeeded(c);
1781 }
1782
1783 if (!poolWriter.bootstrapMethods.isEmpty()) {
1784 writeBootstrapMethods();
1785 acount++;
1786 }
1787
1788 if (!poolWriter.innerClasses.isEmpty()) {
1789 writeInnerClasses();
1790 acount++;
1791 }
1792
1793 if (!poolWriter.loadableDescriptors.isEmpty()) {
1794 writeLoadableDescriptorsAttribute();
1795 acount++;
1796 }
1797
1798 endAttrs(acountIdx, acount);
1799
1800 out.write(poolbuf.elems, 0, poolbuf.length);
1801
1802 poolWriter.writePool(out);
1803 poolWriter.reset(); // to save space
1804
1805 out.write(databuf.elems, 0, databuf.length);
1806 }
1807
1808 /**Allows subclasses to write additional class attributes
1809 *
1810 * @return the number of attributes written
1811 */
1812 protected int writeExtraClassAttributes(ClassSymbol c) {
1813 return 0;
1814 }
1815
1816 /**Allows friends to write additional attributes
1817 *
1818 * @return the number of attributes written
1819 */
1820 protected int writeExtraAttributes(Symbol sym) {
1821 int i = 0;
1822 for (ToIntFunction<Symbol> hook : extraAttributeHooks) {
1823 i += hook.applyAsInt(sym);
1824 }
1825 return i;
1826 }
1827
1828 int adjustFlags(Symbol sym, final long flags) {
1829 int result = (int)flags;
1830
1831 // Elide strictfp bit in class files
1832 if (target.obsoleteAccStrict())
1833 result &= ~STRICTFP;
1834
1835 if ((flags & BRIDGE) != 0)
1836 result |= ACC_BRIDGE;
1837 if ((flags & VARARGS) != 0)
1838 result |= ACC_VARARGS;
1839 if ((flags & DEFAULT) != 0)
1840 result &= ~ABSTRACT;
1841 if ((flags & IDENTITY_TYPE) != 0) {
1842 result |= ACC_IDENTITY;
1843 }
1844 if (sym.kind == VAR) {
1845 if ((flags & STRICT) != 0) {
1846 result |= ACC_STRICT;
1847 }
1848 }
1849 return result;
1850 }
1851
1852 long getLastModified(FileObject filename) {
1853 return filename.getLastModified();
1854 }
1855 }
|