Skip to content

Commit

Permalink
fixed .class references, todo: fill out class data in ReflectionGener…
Browse files Browse the repository at this point in the history
…ator :D
  • Loading branch information
badlogic committed Sep 6, 2012
1 parent 7b6d6e5 commit c6b903a
Show file tree
Hide file tree
Showing 30 changed files with 308 additions and 132 deletions.
2 changes: 1 addition & 1 deletion classpath/src/jack/Arithmetic.java → classpath/src/jack/tests/Arithmetic.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class Arithmetic {
public int arithmetic() {
Expand Down
2 changes: 1 addition & 1 deletion classpath/src/jack/ArrayTest.java → classpath/src/jack/tests/ArrayTest.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class ArrayTest {
public static final Object[] staticArray = new Object[1000000];
Expand Down
2 changes: 1 addition & 1 deletion classpath/src/jack/Arrays.java → classpath/src/jack/tests/Arrays.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class Arrays {
int[] ia = new int[10];
Expand Down
2 changes: 1 addition & 1 deletion classpath/src/jack/Exceptions.java → classpath/src/jack/tests/Exceptions.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class Exceptions {
public void runtime() {
Expand Down
2 changes: 1 addition & 1 deletion classpath/src/jack/InstanceOf.java → classpath/src/jack/tests/InstanceOf.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class InstanceOf {
class A {
Expand Down
2 changes: 1 addition & 1 deletion classpath/src/jack/Main.java → classpath/src/jack/tests/Main.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class Main {
Statics statics;
Expand Down
2 changes: 1 addition & 1 deletion classpath/src/jack/Primes.java → classpath/src/jack/tests/Primes.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class Primes {

Expand Down
16 changes: 16 additions & 0 deletions classpath/src/jack/tests/Reflection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jack.tests;

public class Reflection {
public void test() throws ClassNotFoundException {
Class c = byte[].class;
c = byte[][][].class;
c = Object[][][].class;
c = int.class;
c = Object.class;
c = Class.forName("[C");
}

public static void main(String[] args) throws ClassNotFoundException {
new Reflection().test();
}
}
2 changes: 1 addition & 1 deletion classpath/src/jack/Statics.java → classpath/src/jack/tests/Statics.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class Statics {
static final boolean z = true;
Expand Down
2 changes: 1 addition & 1 deletion classpath/src/jack/Strings.java → classpath/src/jack/tests/Strings.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;

public class Strings {
static String staticString = "static string";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jack;
package jack.tests;


public class Synchronized {
Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Boolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package java.lang;

public final class Boolean implements Comparable<Boolean> {
public static final Class TYPE = Class.forCanonicalName("Z");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;

public static final Boolean FALSE = new Boolean(false);
public static final Boolean TRUE = new Boolean(true);
Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Byte.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package java.lang;

public final class Byte extends Number implements Comparable<Byte> {
public static final Class TYPE = Class.forCanonicalName("B");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;

private final byte value;

Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public final class Character implements Comparable<Character> {
public static final int MIN_RADIX = 2;
public static final int MAX_RADIX = 36;

public static final Class TYPE = Class.forCanonicalName("C");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;

private final char value;

Expand Down
16 changes: 8 additions & 8 deletions classpath/src/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

// FIXME reflection
public final class Class<T> {
private static Map<String, Class> classes = new HashMap<String, Class>();

public static Class forName(String name) throws ClassNotFoundException {
throw new UnsupportedOperationException();
}

String name;
Class superClass;
Class[] interfaces;
Expand Down Expand Up @@ -34,14 +42,6 @@ public T newInstance() throws IllegalAccessException, InstantiationException {
throw new UnsupportedOperationException();
}

public static Class forName(String name) throws ClassNotFoundException {
throw new UnsupportedOperationException();
}

public static Class forCanonicalName(String name) {
throw new UnsupportedOperationException();
}

public Class getComponentType() {
return componentType;
}
Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Double.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package java.lang;

public final class Double extends Number {
public static final Class TYPE = Class.forCanonicalName("D");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;

public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Float.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package java.lang;

public final class Float extends Number {
public static final Class TYPE = Class.forCanonicalName("F");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;
private static final int EXP_BIT_MASK = 0x7F800000;
private static final int SIGNIF_BIT_MASK = 0x007FFFFF;

Expand Down
4 changes: 2 additions & 2 deletions classpath/src/java/lang/Integer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
package java.lang;

public final class Integer extends Number implements Comparable<Integer> {
public static final Class TYPE = Class.forCanonicalName("I");

// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;
public static final int MIN_VALUE = 0x80000000;
public static final int MAX_VALUE = 0x7FFFFFFF;

Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Long.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public final class Long extends Number implements Comparable<Long> {
public static final long MIN_VALUE = -9223372036854775808l;
public static final long MAX_VALUE = 9223372036854775807l;

public static final Class TYPE = Class.forCanonicalName("J");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;

private final long value;

Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Short.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package java.lang;

public final class Short extends Number implements Comparable<Short> {
public static final Class TYPE = Class.forCanonicalName("S");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;
public static final short MAX_VALUE = 32767;

private final short value;
Expand Down
3 changes: 2 additions & 1 deletion classpath/src/java/lang/Void.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package java.lang;

public final class Void {
public static final Class TYPE = Class.forCanonicalName("V");
// this field is filled by ReflectionGenerator#initializePrimitiveTypes()
public static final Class TYPE = null;

private Void() { }
}
10 changes: 8 additions & 2 deletions native/jack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "vm/garbagecollection.h"

void testPrimes() {
jack_Primes* primes = new jack_Primes();
jack_tests_Primes* primes = new jack_tests_Primes();
primes->m_init();
j_long sum = 0;
j_long start = getCurrentTimeMillis();
Expand All @@ -23,7 +23,7 @@ void testPrimes() {

void testAllocation() {
for(int i = 0; i < 10000000; i++) {
jack_ArrayTest* at = new jack_ArrayTest();
jack_tests_ArrayTest* at = new jack_tests_ArrayTest();
at->m_init();

if(i % 10000 == 0) {
Expand All @@ -32,6 +32,12 @@ void testAllocation() {
}
}

void testInstanceOf() {
jack_tests_InstanceOf* obj = new jack_tests_InstanceOf();
obj->m_init();
obj->m_test();
}

int main() {
jack_gc_init();
jack_init();
Expand Down
2 changes: 1 addition & 1 deletion src/com/badlogic/jack/Jack.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static void main(String[] args) {
String sources = args[1].endsWith("/")? args[1]: args[1] + "/";
String outputDir = args[2].endsWith("/")? args[2]: args[2] + "/";

Jack compiler = new Jack(classpath, sources, outputDir, true);
Jack compiler = new Jack(classpath, sources, outputDir, false);
compiler.compile();
}
}
76 changes: 56 additions & 20 deletions src/com/badlogic/jack/generators/ReflectionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,76 @@
public class ReflectionGenerator {
private final SourceWriter writer;
private final Map<SootClass, ClassInfo> infos;
private int nextLiteralId = 0;

public ReflectionGenerator(SourceWriter writer, Map<SootClass, ClassInfo> infos) {
this.writer = writer;
this.infos = infos;
}

public void generate() {
initializeClasses();
generatePrimitiveClasses();

// fill out their reflection data for each class
for(SootClass c: infos.keySet()) {
generateClassReflectionData(c);
}
writer.wl("");

addClassesToClassMap();
}

/**
* Adds the references of each class' Class instance
* to the Class#classes map.
*/
private void addClassesToClassMap() {

}

private String generateLiteral(String value) {
return null;
}

/**
* Creates the xxx::clazz instances. Those have to be created first
* as they are referenced later when generating their actual content.
*/
private void initializeClasses() {
// initialize the java_lang_Class* for each class/interface
for(SootClass c: infos.keySet()) {
String var = Mangling.mangle(c) + "::clazz";
writer.wl(var + "= new java_lang_Class();");
}
writer.wl("");
}

/**
* Initializes the primitive type classes.
* @return
*/
private void generatePrimitiveClasses() {
writer.wl("error, need to create primitive classes");
}

private void generateClassReflectionData(SootClass c) {
String var = Mangling.mangle(c) + "::clazz";
writer.wl(var + "->m_init();");
// FIXME reflection
// writer.wl(var + "->f_isPrimitive = " + )
// writer.wl(var + "->f_isArray = " + );
if(c.hasSuperclass()) {
writer.wl(var + "->f_superClass = " + Mangling.mangle(c.getSuperclass()) + "::clazz;");
}

// fill out their reflection data
for(SootClass c: infos.keySet()) {
String var = Mangling.mangle(c) + "::clazz";
writer.wl(var + "->m_init();");
// FIXME reflection
// writer.wl(var + "->f_isPrimitive = " + )
// writer.wl(var + "->f_isArray = " + );
if(c.hasSuperclass()) {
writer.wl(var + "->f_superClass = " + Mangling.mangle(c.getSuperclass()) + "::clazz;");
}

// create array for interfaces
writer.wl(var + "->f_interfaces = new Array<java_lang_Class*>(" + c.getInterfaceCount() + ", false);");
int i = 0;
for(SootClass itf: c.getInterfaces()) {
writer.wl("(*" + var + "->f_interfaces)[" + i + "] = " + Mangling.mangle(itf) + "::clazz;");
i++;
}
writer.wl("");
// create array for interfaces
writer.wl(var + "->f_interfaces = new Array<java_lang_Class*>(" + c.getInterfaceCount() + ", false);");
int i = 0;
for(SootClass itf: c.getInterfaces()) {
writer.wl("(*" + var + "->f_interfaces)[" + i + "] = " + Mangling.mangle(itf) + "::clazz;");
i++;
}
writer.wl("");
writer.wl("");
}
}
2 changes: 1 addition & 1 deletion src/com/badlogic/jack/generators/RuntimeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void generate() {
writer.push();

// generate all the reflection info
new ReflectionGenerator(writer, infos);
new ReflectionGenerator(writer, infos).generate();

// call all m_clinit methods, this should cascade
// FIXME clinit (propagation correct?)
Expand Down
Loading

0 comments on commit c6b903a

Please sign in to comment.