Post Reply 
Python to FOCAL Compiler
10-07-2015, 04:38 PM
Post: #13
Java to FOCAL Compiler
(10-06-2015 03:58 PM)Gerson W. Barbosa Wrote:  what about converting the fast algorithm presented by Hugh Steers here to RPN?

Since the Java-VM is a stack-machine too we can do a similar thing with the generated byte-code. The original C-code can be used without major changes:

Code:
class Compiler {

     double ellipse(double a, double b) {
        double u = 1;
        double v = b/a;
        double s = (1 + v*v)/2;
        double t = 1;

        for (;;) {
            double a1 = (u + v)/2;
            if (u == a1)
                break;
            double c = (u - v)/2;
            v = Math.sqrt(u*v);
            u = a1;
            s = s - t*c*c;
            t = t * 2;
        }
        return 2*Math.PI*a*s/u;
    }
}

We compile this to a class-file and use javap to disassemble the code:

javac Compiler.java
javap -c Compiler


This is what we get:
Code:
Compiled from "Compiler.java"                                        
class Compiler {
  Compiler();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  double ellipse(double, double);
    Code:
       0: dconst_1
       1: dstore        5
       3: dload_3
       4: dload_1
       5: ddiv
       6: dstore        7
       8: dconst_1
       9: dload         7
      11: dload         7
      13: dmul
      14: dadd
      15: ldc2_w        #2                  // double 2.0d
      18: ddiv
      19: dstore        9
      21: dconst_1
      22: dstore        11
      24: dload         5
      26: dload         7
      28: dadd
      29: ldc2_w        #2                  // double 2.0d
      32: ddiv
      33: dstore        13
      35: dload         5
      37: dload         13
      39: dcmpl
      40: ifne          46
      43: goto          95
      46: dload         5
      48: dload         7
      50: dsub
      51: ldc2_w        #2                  // double 2.0d
      54: ddiv
      55: dstore        15
      57: dload         5
      59: dload         7
      61: dmul
      62: invokestatic  #4                  // Method java/lang/Math.sqrt:(D)D
      65: dstore        7
      67: dload         13
      69: dstore        5
      71: dload         9
      73: dload         11
      75: dload         15
      77: dmul
      78: dload         15
      80: dmul
      81: dsub
      82: dstore        9
      84: dload         11
      86: ldc2_w        #2                  // double 2.0d
      89: dmul
      90: dstore        11
      92: goto          24
      95: ldc2_w        #5                  // double 6.283185307179586d
      98: dload_1
      99: dmul
     100: dload         9
     102: dmul
     103: dload         5
     105: ddiv
     106: dreturn
}

Since a double occupies two registers only odd indexes are used. But we can still see the similarities to the generated FOCAL program using Python. Thus we could probably generate FOCAL programs from class-files as well.

Kind regards
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Python to FOCAL Compiler - Thomas Klemm - 10-05-2015, 10:58 PM
RE: Python to FOCAL Compiler - Sukiari - 10-06-2015, 03:36 AM
Java to FOCAL Compiler - Thomas Klemm - 10-07-2015 04:38 PM



User(s) browsing this thread: 1 Guest(s)