Merge branch 'bytecode2' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into bytecode2

This commit is contained in:
JanUlrich 2018-02-21 11:05:57 +01:00
commit 6cf98f200b
10 changed files with 176 additions and 7 deletions

View File

@ -55,6 +55,7 @@ public class BytecodeGen implements ASTVisitor {
@Override
public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()) {
System.out.println("in Class: " + cl.getClassName().toString());
BytecodeGen classGen = new BytecodeGen(classFiles, resultSet);
cl.accept(classGen);
classGen.writeClass(cl.getClassName().toString());
@ -183,7 +184,8 @@ public class BytecodeGen implements ASTVisitor {
System.out.println(sig);
NormalMethod meth = new NormalMethod(method,genericsAndBounds,genericsAndBoundsMethod,hasGen);
methDesc = meth.accept(new DescriptorToString(resultSet));
MethodVisitor mv = cw.visitMethod(acc, method.getName(), methDesc, sig, null);
System.out.println(methDesc);
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC+acc, method.getName(), methDesc, sig, null);
mv.visitCode();

View File

@ -236,7 +236,7 @@ public class BytecodeGenMethod implements StatementVisitor{
cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup",
Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL);
generateBCForFunN(lambdaExpression,typeErasure);
// generateBCForFunN(lambdaExpression,typeErasure);
}
private void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc) {
@ -435,7 +435,126 @@ public class BytecodeGenMethod implements StatementVisitor{
@Override
public void visit(Literal literal) {
mv.visitLdcInsn(getResolvedType(literal.getType()));
System.out.println(resultSet.resolveType(literal.getType()).resolvedType.acceptTV(new TypeToDescriptor()));
Object value = literal.value;
switch (resultSet.resolveType(literal.getType()).resolvedType.acceptTV(new TypeToDescriptor())) {
case "java/lang/String":
mv.visitLdcInsn((String) value);
break;
case "java/lang/Boolean":
visitBooleanLiteral((Boolean) value);
break;
case "java/lang/Byte":
visitByteLiteral(((Double) value).byteValue(),false);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf",
"(B)Ljava/lang/Byte;", false);
break;
case "java/lang/Short":
visitShortLiteral(((Double) value).shortValue(),false);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf",
"(S)Ljava/lang/Short;", false);
break;
case "java/lang/Integer":
//zweite Argument isLong
visitIntegerLiteral(((Double) value).intValue(), false);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
"(I)Ljava/lang/Integer;", false);
break;
case "java/lang/Long":
visitLongLiteral(((Double) value).longValue(), true);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf",
"(J)Ljava/lang/Long;", false);
break;
case "java/lang/Float":
visitFloatLiteral(((Double) value).floatValue());
break;
case "java/lang/Double":
visitDoubleLiteral((Double) value);
break;
case "java/lang/Character":
visitCharLiteral((Character) value);
break;
default:
break;
}
}
private void visitCharLiteral(Character value) {
mv.visitIntInsn(Opcodes.BIPUSH, Character.getNumericValue(value.charValue()));
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf",
"(C)Ljava/lang/Character;", false);
}
private void visitDoubleLiteral(Double value) {
if(value == 0) {
mv.visitInsn(Opcodes.DCONST_0);
}else if(value == 1) {
mv.visitInsn(Opcodes.DCONST_1);
}else {
mv.visitLdcInsn(value);
}
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
"(D)Ljava/lang/Double;", false);
}
private void visitFloatLiteral(Float value) {
if(value.intValue()>-1 && value.intValue() < 3) {
//Opcodes.FCONST_0 = 11, Opcodes.FCONST_1 = 12, usw
mv.visitInsn(value.intValue()+11);
}else {
mv.visitLdcInsn(value);
}
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf",
"(F)Ljava/lang/Float;", false);
}
private void visitLongLiteral(Long value, boolean isLong) {
if(value<Math.pow(2, 15) || (value>=-Math.pow(2, 15))&&value<-128) {
visitShortLiteral(value.shortValue(),isLong);
}else {
mv.visitLdcInsn(value);
}
}
private void visitShortLiteral(Short value,boolean isLong) {
if(value< 128 || (value>-129 && value<-1)) {
visitByteLiteral(value.byteValue(), isLong);
}else if(value<Math.pow(2, 15) || (value>=-Math.pow(2, 15))&&value<-128) {
mv.visitIntInsn(Opcodes.SIPUSH, value);
}
}
private void visitByteLiteral(Byte value, boolean isLong) {
if(!isLong && value<6 && value>-1) {
//Opcodes.ICONST_0 = 3, Opcodes.ICONST_1 = 4, usw
mv.visitInsn(value+3);
}else if(isLong && value>-1 && value<2){
//Opcodes.LCONST_0 = 9, Opcodes.LCONST_1 = 10
mv.visitInsn(value+9);
}else {
mv.visitIntInsn(Opcodes.BIPUSH, value);
}
}
private void visitIntegerLiteral(Integer value, boolean isLong) {
if(value<Math.pow(2, 15) || (value>=-Math.pow(2, 15))&&value<-128) {
visitShortLiteral(value.shortValue(),isLong);
}else {
mv.visitLdcInsn(value);
}
}
private void visitBooleanLiteral(Boolean b) {
if(b) {
mv.visitInsn(Opcodes.ICONST_1);
}else {
mv.visitInsn(Opcodes.ICONST_0);
}
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf",
"(Z)Ljava/lang/Boolean;", false);
}
@Override

View File

@ -134,7 +134,7 @@ public class Signature {
RefTypeOrTPHOrWildcardOrGeneric r = resultSet.resolveType(t).resolvedType;
sv.visitInterface().visitClassType(r.acceptTV(new TypeToSignature()));
// sv.visitClassType(r.acceptTV(new TypeToSignature()));
// System.out.println(r.getClass()+" Signature TPH: "+r.acceptTV(new TypeToSignature()));
System.out.println(r.getClass()+" Signature TPH: "+r.acceptTV(new TypeToSignature()));
break;
default:
if(!isParameterType)

View File

@ -0,0 +1,30 @@
import java.lang.Integer;
import java.lang.Boolean;
import java.lang.String;
import java.lang.Byte;
import java.lang.Short;
import java.lang.Long;
import java.lang.Float;
import java.lang.Double;
import java.lang.Character;
class AssignToLit {
void m(){
String s = "String";
Boolean b = false;
Byte byte1 = 5;
Byte byte2 = 55;
Short short1 = 5;
Short short2 = 55;
Integer int1 = 5;
Integer int2 = 8888888;
Long long1 = 1;
Long long2 = 5;
Long long3 = 89989898;
Float float1 = 1;
Float float2 = 55;
Double d1 = 1;
Double d2 = 55;
Character c = 'A';
}
}

View File

@ -0,0 +1,7 @@
package bytecode;
public class AssignToLitTest extends JavaTXCompilerTest {
public AssignToLitTest() {
this.fileName = "AssignToLit";
}
}

View File

@ -1,7 +1,9 @@
import java.lang.String;
public class Example {
public m(Integer x) {
// String x = "X";
public m() {
String x = "X";
return x;
}
}

View File

@ -1,3 +1,6 @@
import java.lang.Integer;
import java.lang.Boolean;
class For{
m(Integer x){
Boolean b = true;

View File

@ -1,5 +1,7 @@
package bytecode;
import org.objectweb.asm.Opcodes;
public class ForTest extends JavaTXCompilerTest {
public ForTest() {

View File

@ -67,7 +67,7 @@ public class JavaTXCompilerTest {
byte[] bytecode = classFiles.get(name);
try {
System.out.println("generating "+name+ ".class file ...");
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" +name+".class"));
output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class"));
output.write(bytecode);
output.close();
System.out.println(name+".class file generated");

View File

@ -9,3 +9,7 @@ class LamAssign {
return lam1;
}
}
interface Fun1<A,B>{
public A apply(B b);
}