Simn / genjvm

13 stars 1 forks source link

OpAdd #4

Closed Simn closed 5 years ago

Simn commented 5 years ago

All the variants of + have to be supported (with conversion to string).

nadako commented 5 years ago

Why do we generate Jvm.stringConcat("hi ", "world") instead of native concatenation? It seems that the right thing is already done by JVM: https://repl.it/@nadako/RewardingSnowTutorial Or is it Java compiling it to something similar to stringConcat in the byte-code?

Simn commented 5 years ago

I don't know what javac does with this, but we should indeed check that.

nadako commented 5 years ago

Hm let me check

nadako commented 5 years ago
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

class Main {
    Main() {
    }

    public static void main(String[] var0) {
        byte var1 = 3;
        Object var2 = null;
        System.out.println("" + var1 + var2);
        Object var3 = null;
        System.out.println("" + var3);
        System.out.println("hi " + new Main());
    }

    public String toString() {
        return "HIHI";
    }
}
nadako commented 5 years ago

Interestingly, the local var names seem to be stripped away, while we keep them in Haxe.

Simn commented 5 years ago

Be careful with the decompiler, it tries to make things look Java again. This is the bytecode:

  public static void main(java.lang.String[]);
    Code:
       0: iconst_3
       1: istore_1
       2: aconst_null
       3: astore_2
       4: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: new           #3                  // class java/lang/StringBuilder
      10: dup
      11: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
      14: iload_1
      15: invokevirtual #5                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      18: aload_2
      19: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      22: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      25: invokevirtual #8                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      28: aconst_null
      29: astore_3
      30: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      33: new           #3                  // class java/lang/StringBuilder
      36: dup
      37: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
      40: ldc           #9                  // String
      42: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      45: aload_3
      46: invokevirtual #10                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;      49: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      52: invokevirtual #8                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      55: return

So it creates StringBuilder instances and appends to them.

nadako commented 5 years ago

Yeah, I just wanted to also paste this :)

Simn commented 5 years ago

StringBuilder is nice because it returns itself, which makes the stack handling trivial. I guess we should use it too, especially if it handles the null case already.

nadako commented 5 years ago

I assume it's also better wrt boxing because of overloaded append functions

Simn commented 5 years ago

There might be this stupid problem where Haxe specifies that a 1.0 value is printed as 1 for some retarded reason...