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 for(TypeDesc param : params)
46 if(param.equals(TypeDesc.VOID))
47 throw new IllegalArgumentException();
48 MethodVisitor methodVisitor = classVisitor.visitMethod(access, methodName,
49 MethodDesc.forArguments(ret, params).getDescriptor(),
51 methodVisitor = augmentMethodVisitor(methodName, methodVisitor);
52 return new MethodBuilder(this, moduleBuilder, (access&Opcodes.ACC_STATIC) != 0, methodVisitor, params);
55 private MethodVisitor augmentMethodVisitor(final String methodName, MethodVisitor methodVisitor) {
56 if(SCLCompilerConfiguration.TRACE_MAX_METHOD_SIZE && moduleBuilder != null) {
57 methodVisitor = new CodeSizeEvaluator(methodVisitor) {
59 public void visitEnd() {
61 if(moduleBuilder.methodSizeCounter == null)
62 moduleBuilder.methodSizeCounter = new MethodSizeCounter();
63 moduleBuilder.methodSizeCounter.update(methodName, getMinSize(), getMaxSize());
70 public MethodBuilderBase addMethodBase(int access,
74 for(TypeDesc param : params)
75 if(param.equals(TypeDesc.VOID))
76 throw new IllegalArgumentException();
77 MethodVisitor methodVisitor = classVisitor.visitMethod(access, methodName,
78 MethodDesc.forArguments(ret, params).getDescriptor(),
80 methodVisitor = augmentMethodVisitor(methodName, methodVisitor);
81 return new MethodBuilderBase(this, (access&Opcodes.ACC_STATIC) != 0, methodVisitor, params);
84 public void addAbstractMethod(int access,
88 for(TypeDesc param : params)
89 if(param.equals(TypeDesc.VOID))
90 throw new IllegalArgumentException();
91 MethodVisitor methodVisitor = classVisitor.visitMethod(access, methodName,
92 MethodDesc.forArguments(ret, params).getDescriptor(),
94 for(int i=0;i<params.length;++i)
95 methodVisitor.visitParameter("p" + i, Opcodes.ACC_PUBLIC);
96 methodVisitor.visitEnd();
99 public MethodBuilderBase addConstructor(int access, TypeDesc[] params) {
100 return addMethodBase(access, "<init>", TypeDesc.VOID, params);
103 public MethodBuilder addInitializer() {
104 return addMethod(Opcodes.ACC_PUBLIC, "<clinit>", TypeDesc.VOID, Constants.EMPTY_TYPEDESC_ARRAY);
107 public MethodBuilderBase addInitializerBase() {
108 return addMethodBase(Opcodes.ACC_PUBLIC, "<clinit>", TypeDesc.VOID, Constants.EMPTY_TYPEDESC_ARRAY);
111 public void addField(int access, String fieldName,
113 if(type.equals(TypeDesc.VOID))
114 throw new IllegalArgumentException();
115 FieldVisitor fieldVisitor = classVisitor.visitField(access, fieldName, type.getDescriptor(), null, null);
116 fieldVisitor.visitEnd();
119 public byte[] finishClass() {
120 classVisitor.visitEnd();
121 return classWriter.toByteArray();
124 public String getClassName() {
128 public void addDefaultConstructor() {
129 MethodVisitor methodVisitor = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
130 methodVisitor.visitCode();
131 methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
132 methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClassName, "<init>", "()V", false);
133 methodVisitor.visitInsn(Opcodes.RETURN);
134 methodVisitor.visitMaxs(0, 0);
135 methodVisitor.visitEnd();
138 public TypeDesc getType() {
139 return TypeDesc.forClass(className);
142 public String getSuperClassName() {
143 return superClassName;
146 public static void checkClassName(String className) {
147 /*if(className != null && className.contains(".")) {
148 System.out.print(className);
149 System.out.println();
153 public ModuleBuilder getModuleBuilder() {
154 return moduleBuilder;