1 package org.simantics.scl.compiler.internal.codegen.utils;
3 import java.util.Arrays;
5 import org.cojen.classfile.TypeDesc;
6 import org.objectweb.asm.Label;
7 import org.objectweb.asm.Opcodes;
8 import org.simantics.scl.compiler.internal.codegen.references.Val;
9 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
10 import org.simantics.scl.compiler.types.Type;
12 public class CodeBuilderUtils {
15 * Creates fields c0,...,c{N-1} to the given class, where c is fieldNamePrefix and N is the length of types.
16 * Creates also a constructor for the fields.
18 * @param fieldModifiers
19 * @param fieldNamePrefix
22 public static void makeRecord(ClassBuilder classBuilder, String recordName, int fieldModifiers, String fieldNamePrefix, TypeDesc[] types,
23 boolean generateEqualsAndHashCode) {
25 for(int i=0;i<types.length;++i)
26 if(!types[i].equals(TypeDesc.VOID))
27 classBuilder.addField(fieldModifiers, fieldNamePrefix+i, types[i]);
30 MethodBuilderBase mb = classBuilder.addConstructor(
31 types.length == 0 ? Opcodes.ACC_PRIVATE : Opcodes.ACC_PUBLIC,
32 JavaTypeTranslator.filterVoid(types));
34 mb.invokeConstructor(classBuilder.getSuperClassName(), Constants.EMPTY_TYPEDESC_ARRAY);
35 if(types.length == 0) {
36 TypeDesc thisClass = classBuilder.getType();
37 classBuilder.addField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "INSTANCE", thisClass);
39 MethodBuilderBase inimb = classBuilder.addInitializerBase();
40 inimb.newObject(thisClass);
42 inimb.invokeConstructor(classBuilder.getClassName(), Constants.EMPTY_TYPEDESC_ARRAY);
43 inimb.storeStaticField(classBuilder.getClassName(), "INSTANCE", thisClass);
48 for(int i=0,j=0;i<types.length;++i) {
49 if(!types[i].equals(TypeDesc.VOID)) {
51 mb.loadLocal(mb.getParameter(j++));
52 mb.storeField(classBuilder.getClassName(), fieldNamePrefix+i, types[i]);
61 MethodBuilderBase tsmb = classBuilder.addMethodBase(Opcodes.ACC_PUBLIC, "toString", TypeDesc.STRING, Constants.EMPTY_TYPEDESC_ARRAY);
63 if(types.length > 0) {
64 tsmb.newObject(TypeDesc.forClass(StringBuilder.class));
66 tsmb.invokeConstructor("java/lang/StringBuilder", Constants.EMPTY_TYPEDESC_ARRAY);
69 tsmb.loadConstant("(" + recordName);
70 StringBuilder_appendString(tsmb);
71 for(int i=0;i<types.length;++i) {
72 if(types[i].equals(TypeDesc.VOID)) {
73 tsmb.loadConstant(" ()");
74 StringBuilder_appendString(tsmb);
77 tsmb.loadConstant(" ");
78 StringBuilder_appendString(tsmb);
80 tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, types[i]);
81 StringBuilder_appendObject(tsmb, types[i]);
84 tsmb.loadConstant(")");
85 StringBuilder_appendString(tsmb);
88 tsmb.invokeVirtual("java/lang/StringBuilder", "toString", TypeDesc.STRING, Constants.EMPTY_TYPEDESC_ARRAY);
91 tsmb.loadConstant(recordName);
92 tsmb.returnValue(TypeDesc.STRING);
96 if(generateEqualsAndHashCode) {
99 TypeDesc CLASS = TypeDesc.forClass(Class.class);
101 MethodBuilderBase tsmb = classBuilder.addMethodBase(Opcodes.ACC_PUBLIC, "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[1]);
102 LocalVariable parameter = tsmb.getParameter(0);
103 Label success = tsmb.createLabel();
104 Label failure = tsmb.createLabel();
108 tsmb.loadLocal(parameter);
109 tsmb.ifComparisonBranch(success, "==", TypeDesc.OBJECT);
110 tsmb.loadLocal(parameter);
111 tsmb.ifNullBranch(failure, true);
112 tsmb.loadLocal(parameter);
113 tsmb.invokeVirtual("java/lang/Object", "getClass", CLASS, Constants.EMPTY_TYPEDESC_ARRAY);
115 tsmb.invokeVirtual("java/lang/Object", "getClass", CLASS, Constants.EMPTY_TYPEDESC_ARRAY);
116 tsmb.ifComparisonBranch(failure, "!=", CLASS);
117 tsmb.loadLocal(parameter);
118 tsmb.checkCast(classBuilder.getType());
119 LocalVariable other = tsmb.createLocalVariable("other", classBuilder.getType());
120 tsmb.storeLocal(other);
123 for(int i=0;i<types.length;++i) {
124 TypeDesc type = types[i];
125 if(type.equals(TypeDesc.VOID))
128 tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, type);
129 tsmb.loadLocal(other);
130 tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, type);
131 equals(tsmb, type, failure);
135 tsmb.setLocation(success);
136 tsmb.loadConstant(true);
137 tsmb.returnValue(TypeDesc.BOOLEAN);
138 tsmb.setLocation(failure);
139 tsmb.loadConstant(false);
140 tsmb.returnValue(TypeDesc.BOOLEAN);
146 MethodBuilderBase tsmb = classBuilder.addMethodBase(Opcodes.ACC_PUBLIC, "hashCode", TypeDesc.INT, Constants.EMPTY_TYPEDESC_ARRAY);
147 tsmb.loadConstant(recordName.hashCode());
148 for(int i=0;i<types.length;++i) {
149 TypeDesc type = types[i];
150 if(type.equals(TypeDesc.VOID))
152 tsmb.loadConstant(31);
153 tsmb.math(Opcodes.IMUL);
155 tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, type);
156 hashCode(tsmb, type);
157 tsmb.math(Opcodes.IADD);
159 tsmb.returnValue(TypeDesc.INT);
165 public static void equals(MethodBuilderBase mb, TypeDesc typeDesc, Label failure) {
166 if(typeDesc.isPrimitive())
167 mb.ifComparisonBranch(failure, "!=", typeDesc);
169 Label isNull = mb.createLabel();
170 Label finished = mb.createLabel();
173 mb.ifNullBranch(isNull, true);
175 mb.invokeVirtual("java/lang/Object", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[1]);
176 mb.ifZeroComparisonBranch(failure, "==");
178 mb.setLocation(isNull);
180 mb.ifNullBranch(failure, false);
181 mb.setLocation(finished);
186 * Calculates the hash code of a value in stack.
188 public static void hashCode(MethodBuilderBase mb, TypeDesc typeDesc) {
189 switch(typeDesc.getTypeCode()) {
190 case TypeDesc.INT_CODE:
192 case TypeDesc.OBJECT_CODE: {
193 Label isNull = mb.createLabel();
194 Label finished = mb.createLabel();
196 mb.ifNullBranch(isNull, true);
197 mb.invokeVirtual("java/lang/Object", "hashCode", TypeDesc.INT, Constants.EMPTY_TYPEDESC_ARRAY);
199 mb.setLocation(isNull);
202 mb.setLocation(finished);
204 case TypeDesc.DOUBLE_CODE:
205 mb.invokeStatic("java/lang/Double", "doubleToLongBits", TypeDesc.LONG, new TypeDesc[] { TypeDesc.DOUBLE });
206 case TypeDesc.LONG_CODE:
209 mb.math(Opcodes.LSHR);
210 mb.math(Opcodes.LXOR);
211 mb.convert(TypeDesc.LONG, TypeDesc.INT);
213 case TypeDesc.FLOAT_CODE:
214 mb.invokeStatic("java/lang/Float", "floatToIntBits", TypeDesc.INT, new TypeDesc[] { TypeDesc.FLOAT });
217 mb.convert(typeDesc, TypeDesc.INT);
221 public static void constructRecord(TypeDesc clazz, MethodBuilder mb,
222 Type[] parameterTypes, Val... parameters) {
223 if(parameters.length == 0) {
224 mb.loadStaticField(clazz, "INSTANCE", clazz);
229 for(int i=0;i<parameters.length;++i)
230 mb.push(parameters[i], parameterTypes[i]);
231 JavaTypeTranslator tt = mb.moduleBuilder.getJavaTypeTranslator();
232 mb.invokeConstructor(clazz, JavaTypeTranslator.filterVoid(
233 tt.toTypeDescs(Arrays.copyOf(parameterTypes, parameters.length))));
237 public static void StringBuilder_appendString(MethodBuilderBase mb) {
238 mb.invokeVirtual("java/lang/StringBuilder", "append", TypeDesc.forClass("java.lang.StringBuilder"),
239 new TypeDesc[] {TypeDesc.STRING});
242 public static void StringBuilder_appendObject(MethodBuilderBase mb, TypeDesc type) {
243 if(!type.isPrimitive() && type != TypeDesc.STRING)
244 type = TypeDesc.OBJECT;
245 mb.invokeVirtual("java/lang/StringBuilder", "append", TypeDesc.forClass("java.lang.StringBuilder"),
246 new TypeDesc[] {type});