]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/CodeBuilderUtils.java
Added info on backup location to documentation backup.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / utils / CodeBuilderUtils.java
1 package org.simantics.scl.compiler.internal.codegen.utils;
2
3 import java.util.Arrays;
4
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;
11
12 public class CodeBuilderUtils {
13     
14     /**
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. 
17      * @param classFile
18      * @param fieldModifiers
19      * @param fieldNamePrefix
20      * @param types
21      */
22     public static void makeRecord(ClassBuilder classBuilder, String recordName, int fieldModifiers, String fieldNamePrefix, TypeDesc[] types,
23             boolean generateEqualsAndHashCode) {
24         // Create fields
25         for(int i=0;i<types.length;++i)
26             if(!types[i].equals(TypeDesc.VOID))
27                 classBuilder.addField(fieldModifiers, fieldNamePrefix+i, types[i]);
28         
29         // Create constructor        
30         MethodBuilderBase mb = classBuilder.addConstructor(
31                 types.length == 0 ? Opcodes.ACC_PRIVATE : Opcodes.ACC_PUBLIC, 
32                 JavaTypeTranslator.filterVoid(types));
33         mb.loadThis();
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);
38             
39             MethodBuilderBase inimb = classBuilder.addInitializerBase();
40             inimb.newObject(thisClass);
41             inimb.dup();
42             inimb.invokeConstructor(classBuilder.getClassName(), Constants.EMPTY_TYPEDESC_ARRAY);
43             inimb.storeStaticField(classBuilder.getClassName(), "INSTANCE", thisClass);
44             inimb.returnVoid();
45             inimb.finish();
46         }
47         else {
48             for(int i=0,j=0;i<types.length;++i) {
49                 if(!types[i].equals(TypeDesc.VOID)) {
50                     mb.loadThis();
51                     mb.loadLocal(mb.getParameter(j++));
52                     mb.storeField(classBuilder.getClassName(), fieldNamePrefix+i, types[i]);            
53                 }
54             }
55         }
56         mb.returnVoid();
57         mb.finish();
58         
59         // Create toString
60         {
61             MethodBuilderBase tsmb = classBuilder.addMethodBase(Opcodes.ACC_PUBLIC, "toString", TypeDesc.STRING, Constants.EMPTY_TYPEDESC_ARRAY);
62             
63             if(types.length > 0) {
64                 tsmb.newObject(TypeDesc.forClass(StringBuilder.class));
65                 tsmb.dup();
66                 tsmb.invokeConstructor("java/lang/StringBuilder", Constants.EMPTY_TYPEDESC_ARRAY);
67                 
68                 // build string
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);
75                     }
76                     else {
77                         tsmb.loadConstant(" ");
78                         StringBuilder_appendString(tsmb);
79                         tsmb.loadThis();
80                         tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, types[i]);
81                         StringBuilder_appendObject(tsmb, types[i]);
82                     }
83                 }
84                 tsmb.loadConstant(")");
85                 StringBuilder_appendString(tsmb);
86                 
87                 // return
88                 tsmb.invokeVirtual("java/lang/StringBuilder", "toString", TypeDesc.STRING, Constants.EMPTY_TYPEDESC_ARRAY);
89             }
90             else
91                 tsmb.loadConstant(recordName);
92             tsmb.returnValue(TypeDesc.STRING);
93             tsmb.finish();
94         }
95         
96         if(generateEqualsAndHashCode) {
97             // Create equals
98             {
99                 TypeDesc CLASS = TypeDesc.forClass(Class.class);
100
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();
105
106                 // Check type
107                 tsmb.loadThis();
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);
114                 tsmb.loadThis();
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);
121
122                 // Compare fields
123                 for(int i=0;i<types.length;++i) {
124                     TypeDesc type = types[i];
125                     if(type.equals(TypeDesc.VOID))
126                         continue;
127                     tsmb.loadThis();
128                     tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, type);
129                     tsmb.loadLocal(other);
130                     tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, type);
131                     if(type.isPrimitive())
132                         tsmb.ifComparisonBranch(failure, "!=", type);
133                     else {
134                         Label isNull = tsmb.createLabel();
135                         Label finished = tsmb.createLabel();
136                         tsmb.swap();
137                         tsmb.dup();
138                         tsmb.ifNullBranch(isNull, true);
139                         tsmb.swap();
140                         tsmb.invokeVirtual("java/lang/Object", "equals", TypeDesc.BOOLEAN, Constants.OBJECTS[1]);
141                         tsmb.ifZeroComparisonBranch(failure, "==");
142                         tsmb.branch(finished);
143                         tsmb.setLocation(isNull);
144                         tsmb.pop();
145                         tsmb.ifNullBranch(failure, false);
146                         tsmb.setLocation(finished);
147                     }
148                 }
149
150                 // Return
151                 tsmb.setLocation(success);
152                 tsmb.loadConstant(true);
153                 tsmb.returnValue(TypeDesc.BOOLEAN);
154                 tsmb.setLocation(failure);
155                 tsmb.loadConstant(false);
156                 tsmb.returnValue(TypeDesc.BOOLEAN);
157                 tsmb.finish();
158             }
159
160             // Create hashCode
161             {
162                 MethodBuilderBase tsmb = classBuilder.addMethodBase(Opcodes.ACC_PUBLIC, "hashCode", TypeDesc.INT, Constants.EMPTY_TYPEDESC_ARRAY);
163                 tsmb.loadConstant(recordName.hashCode());
164                 for(int i=0;i<types.length;++i) {
165                     TypeDesc type = types[i];
166                     if(type.equals(TypeDesc.VOID))
167                         continue;
168                     tsmb.loadConstant(31);
169                     tsmb.math(Opcodes.IMUL);
170                     tsmb.loadThis();
171                     tsmb.loadField(classBuilder.getClassName(), fieldNamePrefix+i, type);
172                     switch(type.getTypeCode()) {
173                     case TypeDesc.INT_CODE:
174                         break;
175                     case TypeDesc.OBJECT_CODE: {
176                         Label isNull = tsmb.createLabel();
177                         Label finished = tsmb.createLabel();
178                         tsmb.dup();
179                         tsmb.ifNullBranch(isNull, true);
180                         tsmb.invokeVirtual("java/lang/Object", "hashCode", TypeDesc.INT, Constants.EMPTY_TYPEDESC_ARRAY);
181                         tsmb.branch(finished);
182                         tsmb.setLocation(isNull);
183                         tsmb.pop();
184                         tsmb.loadConstant(0);
185                         tsmb.setLocation(finished);
186                     } break;
187                     case TypeDesc.DOUBLE_CODE:
188                         tsmb.invokeStatic("java/lang/Double", "doubleToLongBits", TypeDesc.LONG, new TypeDesc[] { TypeDesc.DOUBLE });
189                     case TypeDesc.LONG_CODE:
190                         tsmb.dup2();
191                         tsmb.loadConstant(32);
192                         tsmb.math(Opcodes.LSHR);
193                         tsmb.math(Opcodes.LXOR);
194                         tsmb.convert(TypeDesc.LONG, TypeDesc.INT);
195                         break;
196                     case TypeDesc.FLOAT_CODE:
197                         tsmb.invokeStatic("java/lang/Float", "floatToIntBits", TypeDesc.INT, new TypeDesc[] { TypeDesc.FLOAT });
198                         break;
199                     default:
200                         tsmb.convert(type, TypeDesc.INT);
201                     }
202                     tsmb.math(Opcodes.IADD);
203                 }
204                 tsmb.returnValue(TypeDesc.INT);
205                 tsmb.finish();
206             }
207         }
208     }
209     
210     public static void constructRecord(TypeDesc clazz, MethodBuilder mb,
211             Type[] parameterTypes, Val... parameters) {
212         if(parameters.length == 0) {
213             mb.loadStaticField(clazz, "INSTANCE", clazz);
214         }
215         else {
216             mb.newObject(clazz);
217             mb.dup();
218             for(int i=0;i<parameters.length;++i)
219                 mb.push(parameters[i], parameterTypes[i]);
220             JavaTypeTranslator tt = mb.moduleBuilder.getJavaTypeTranslator();
221             mb.invokeConstructor(clazz, JavaTypeTranslator.filterVoid( 
222                     tt.toTypeDescs(Arrays.copyOf(parameterTypes, parameters.length))));
223         }
224     }
225     
226     public static void StringBuilder_appendString(MethodBuilderBase mb) {
227         mb.invokeVirtual("java/lang/StringBuilder", "append", TypeDesc.forClass("java.lang.StringBuilder"), 
228                 new TypeDesc[] {TypeDesc.STRING});
229     }
230     
231     public static void StringBuilder_appendObject(MethodBuilderBase mb, TypeDesc type) {
232         if(!type.isPrimitive() && type != TypeDesc.STRING)
233             type = TypeDesc.OBJECT;
234         mb.invokeVirtual("java/lang/StringBuilder", "append", TypeDesc.forClass("java.lang.StringBuilder"), 
235                 new TypeDesc[] {type});
236     }
237     
238 }