1 package org.simantics.scl.compiler.internal.codegen.utils;
3 import java.io.PrintWriter;
5 import org.cojen.classfile.MethodDesc;
6 import org.cojen.classfile.TypeDesc;
7 import org.objectweb.asm.ClassVisitor;
8 import org.objectweb.asm.ClassWriter;
9 import org.objectweb.asm.FieldVisitor;
10 import org.objectweb.asm.MethodVisitor;
11 import org.objectweb.asm.Opcodes;
12 import org.objectweb.asm.commons.CodeSizeEvaluator;
13 import org.objectweb.asm.util.TraceClassVisitor;
14 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
16 public class ClassBuilder {
17 ModuleBuilder moduleBuilder;
19 String superClassName;
20 ClassVisitor classVisitor;
21 ClassWriter classWriter;
23 public ClassBuilder(ModuleBuilder moduleBuilder, int access, String className, String superClassName,
24 String ... interfaces) {
25 checkClassName(className);
26 checkClassName(superClassName);
27 this.moduleBuilder = moduleBuilder;
28 this.className = className;
29 this.superClassName = superClassName;
30 this.classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
31 this.classVisitor = SCLCompilerConfiguration.SHOW_COMPILED_BYTECODE
32 ? new TraceClassVisitor(classWriter, new PrintWriter(System.out))
34 classVisitor.visit(Opcodes.V1_5, access, className, null, superClassName, interfaces);
37 public void setSourceFile(String fileName) {
38 classVisitor.visitSource(fileName, null);
41 public MethodBuilder addMethod(int access,
45 MethodVisitor methodVisitor = classVisitor.visitMethod(access, methodName,
46 MethodDesc.forArguments(ret, params).getDescriptor(),
48 methodVisitor = augmentMethodVisitor(methodName, methodVisitor);
49 return new MethodBuilder(this, moduleBuilder, (access&Opcodes.ACC_STATIC) != 0, methodVisitor, params);
52 private MethodVisitor augmentMethodVisitor(final String methodName, MethodVisitor methodVisitor) {
53 if(SCLCompilerConfiguration.TRACE_MAX_METHOD_SIZE && moduleBuilder != null) {
54 methodVisitor = new CodeSizeEvaluator(methodVisitor) {
56 public void visitEnd() {
58 if(moduleBuilder.methodSizeCounter == null)
59 moduleBuilder.methodSizeCounter = new MethodSizeCounter();
60 moduleBuilder.methodSizeCounter.update(methodName, getMinSize(), getMaxSize());
67 public MethodBuilderBase addMethodBase(int access,
71 MethodVisitor methodVisitor = classVisitor.visitMethod(access, methodName,
72 MethodDesc.forArguments(ret, params).getDescriptor(),
74 methodVisitor = augmentMethodVisitor(methodName, methodVisitor);
75 return new MethodBuilderBase(this, (access&Opcodes.ACC_STATIC) != 0, methodVisitor, params);
78 public void addAbstractMethod(int access,
82 MethodVisitor methodVisitor = classVisitor.visitMethod(access, methodName,
83 MethodDesc.forArguments(ret, params).getDescriptor(),
85 for(int i=0;i<params.length;++i)
86 methodVisitor.visitParameter("p" + i, Opcodes.ACC_PUBLIC);
87 methodVisitor.visitEnd();
90 public MethodBuilderBase addConstructor(int access, TypeDesc[] params) {
91 return addMethodBase(access, "<init>", TypeDesc.VOID, params);
94 public MethodBuilder addInitializer() {
95 return addMethod(Opcodes.ACC_PUBLIC, "<clinit>", TypeDesc.VOID, Constants.EMPTY_TYPEDESC_ARRAY);
98 public MethodBuilderBase addInitializerBase() {
99 return addMethodBase(Opcodes.ACC_PUBLIC, "<clinit>", TypeDesc.VOID, Constants.EMPTY_TYPEDESC_ARRAY);
102 public void addField(int access, String fieldName,
104 FieldVisitor fieldVisitor = classVisitor.visitField(access, fieldName, type.getDescriptor(), null, null);
105 fieldVisitor.visitEnd();
108 public byte[] finishClass() {
109 classVisitor.visitEnd();
110 return classWriter.toByteArray();
113 public String getClassName() {
117 public void addDefaultConstructor() {
118 MethodVisitor methodVisitor = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
119 methodVisitor.visitCode();
120 methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
121 methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClassName, "<init>", "()V", false);
122 methodVisitor.visitInsn(Opcodes.RETURN);
123 methodVisitor.visitMaxs(0, 0);
124 methodVisitor.visitEnd();
127 public TypeDesc getType() {
128 return TypeDesc.forClass(className);
131 public String getSuperClassName() {
132 return superClassName;
135 public static void checkClassName(String className) {
136 /*if(className != null && className.contains(".")) {
137 System.out.print(className);
138 System.out.println();
142 public ModuleBuilder getModuleBuilder() {
143 return moduleBuilder;