-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaDBCodeGen.jrag
191 lines (147 loc) · 6.89 KB
/
JavaDBCodeGen.jrag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
aspect L1CodeGen {
//TODO replace the runtime with static function call
void OwnerOfExpr.emitOperation(CodeGeneration gen) {
getOperand().createBCode(gen);
String classname = "javadb.JavaDBRuntime";
String desc = "(Ljava/lang/Object;)Ljava/lang/Object;";
String name = "getParent";
int index = gen.constantPool().addMethodref(classname, name, desc);
gen.emit(Bytecode.INVOKESTATIC, 1).add2(index);
gen.emitCheckCast(type());
}
refine CreateBCode public void ClassInstanceExpr.createBCode(CodeGeneration gen) {
refined(gen);
//L1: if this is the type is ownedby, then we do following
if (!type().getOwnerType().isUnknown()) {
gen.emitDup();
gen.emitLoadReference(0); //push this to stack
String classname = "javadb.JavaDBRuntime";
String desc = "(Ljava/lang/Object;Ljava/lang/Object;)V";
String name = "putParent";
int index = gen.constantPool().addMethodref(classname, name, desc);
gen.emit(Bytecode.INVOKESTATIC, -2).add2(index);
}
}
refine GenericsCodegen public void VarAccess.emitStore(CodeGeneration gen) {
if (isParField()) {
FieldVariable fv = getParFieldVar();
//we already have the object and the target value in the stack
//now box the value if it is primitive, only isPrimitive seems have bugs
if (!type().isReferenceType() && type().isPrimitive())
type().boxed().emitBoxingOperation(gen);
gen.emitLoadReference(0); //push this to stack
IntegerLiteral.push(gen, fv.getPosition()); //load field name string
String classname = "javadb.JavaDBRuntime";
String desc = "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;I)V";
String name = "setFieldValue";
int index = gen.constantPool().addMethodref(classname, name, desc);
gen.emit(Bytecode.INVOKESTATIC, 1).add2(index);
} else
refined(gen);
}
refine GenericsCodegen public void VarAccess.createBCode(CodeGeneration gen) {
if (isParField()) {
FieldVariable fv = getParFieldVar();
gen.emitLoadReference(0); //push this to stack
createLoadQualifier(gen); //push the object who have the field to stack
//load field name string
IntegerLiteral.push(gen, fv.getPosition());
String classname = "javadb.JavaDBRuntime";
String desc = "(Ljava/lang/Object;Ljava/lang/Object;I)Ljava/lang/Object;";
String name = "getFieldValue";
int index = gen.constantPool().addMethodref(classname, name, desc);
gen.emit(Bytecode.INVOKESTATIC, 1).add2(index);
//now box the value if it is primitive, only isPrimitive seems have bugs
if (!type().isReferenceType() && type().isPrimitive()) {
TypeDecl boxTy = type().boxed();
gen.emitCheckCast(boxTy);
boxTy.emitUnboxingOperation(gen);
} else
gen.emitCheckCast(type());
} else
refined(gen);
}
refine GenericsCodegen public void MethodAccess.createBCode(CodeGeneration gen) {
if (isParMethod()) {
MethodVariable mv = getParMethodVar();
String classname = "javadb.JavaDBRuntime";
String desc = "(Ljava/lang/Object;Ljava/lang/Object;I[Ljava/lang/Object;)Ljava/lang/Object;";
String name = "callMethod";
int call_index = gen.constantPool().addMethodref(classname, name, desc);
gen.emitLoadReference(0); //arg0, push this to stack
createLoadQualifier(gen); //arg1, receiver
IntegerLiteral.push(gen, mv.getPosition()); //arg2, the method index
int index_object = gen.constantPool().addClass("java/lang/Object");
IntegerLiteral.push(gen, getNumArg());
gen.emit(Bytecode.ANEWARRAY).add2(index_object); //arg3, the argument list
for (int i = 0; i < getNumArg(); ++i) {
gen.emitDup();
IntegerLiteral.push(gen, i);
Expr arg = getArg(i);
arg.createBCode(gen);
if (!arg.type().isReferenceType() && arg.type().isPrimitive()) {
arg.type().boxed().emitBoxingOperation(gen);
} else {
arg.type().emitCastTo(gen, mv.getArgumentTypes(i).type());
}
gen.emit(Bytecode.AASTORE); //store into arg3
}
gen.emit(Bytecode.INVOKESTATIC, 1).add2(call_index);
} else {
refined(gen);
}
}
public void ParMemberClassInstanceExpr.createBCode(CodeGeneration gen) {
super.createBCode(gen);
//iterate and register field
List<MemberVariable> fieldPars = type().getMemberPars();
List<ParMemberAccess> fieldArgs = this.getMemberArguments();
String classname = "javadb.JavaDBRuntime";
String desc1 = "(Ljava/lang/Object;Ljava/lang/String;ILjava/lang/String;)V";
String name1 = "registerField";
int index1 = gen.constantPool().addMethodref(classname, name1, desc1);
String desc12 = "(Ljava/lang/Object;Ljava/lang/String;ILjava/lang/Object;I)V";
String name12 = "registerFieldVar";
int index12 = gen.constantPool().addMethodref(classname, name12, desc12);
String desc2 = "(Ljava/lang/Object;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Class;)V"; //TODO
String name2 = "registerMethod";
int index2 = gen.constantPool().addMethodref(classname, name2, desc2);
String desc22 = "(Ljava/lang/Object;Ljava/lang/String;ILjava/lang/Object;I)V"; //TODO
String name22 = "registerMethodVar";
int index22 = gen.constantPool().addMethodref(classname, name22, desc22);
int index_class = gen.constantPool().addClass("java/lang/Class");
int index_forname = gen.constantPool().addMethodref(classname, "forNameNoException", "(Ljava/lang/String;)Ljava/lang/Class;");
for (int i = 0; i < fieldArgs.getNumChild(); i++) {
MemberVariable fv = fieldPars.getChild(i);
ParMemberAccess fa = fieldArgs.getChild(i);
gen.emitDup(); //arg0: the new object
StringLiteral.push(gen, fa.getReceiverTypeAccess().type().fullName()); //arg1: recieve name
IntegerLiteral.push(gen, fv.getPosition()); //arg2: declaration id
MemberVariable mv = fa.getParVarAccess();
if (mv != null) {
gen.emitLoadReference(0); //arg3, push this to stack
IntegerLiteral.push(gen, mv.getPosition()); //arg4: this's id
gen.emit(Bytecode.INVOKESTATIC, 1).add2((fv instanceof FieldVariable) ? index12 : index22);
} else {
StringLiteral.push(gen, fa.getMemberName()); //arg3: actual field name
if (fv instanceof FieldVariable) {
gen.emit(Bytecode.INVOKESTATIC, 1).add2(index1);
} else {
MethodVariable methodVar = (MethodVariable) fv;
int m = methodVar.getNumArgumentTypes();
IntegerLiteral.push(gen, m);
gen.emit(Bytecode.ANEWARRAY).add2(index_class);
//TODO BE TESTED
for (int j = 0; j < m; j++) {
gen.emitDup();
IntegerLiteral.push(gen, j);
StringLiteral.push(gen, methodVar.getArgumentTypes(j).type().fullName());
gen.emit(Bytecode.INVOKESTATIC, 1).add2(index_forname);
gen.emit(Bytecode.AASTORE);
}
gen.emit(Bytecode.INVOKESTATIC, 1).add2(index2);
}
}
}
}
}