Skip to content

Commit

Permalink
Added initial version of Compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
alcides committed Mar 17, 2011
0 parents commit 3e2709c
Show file tree
Hide file tree
Showing 23 changed files with 257 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="compiler-src"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/AeminiumGPU.jar"/>
<classpathentry kind="lib" path="lib/javacl-1.0-beta-6-shaded.jar"/>
<classpathentry kind="lib" path="lib/javacl-demos-1.0-beta-6-shaded.jar"/>
<classpathentry kind="lib" path="compiler-lib/spoon-core-1.4-jar-with-dependencies.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin
spooned
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SpoonCompilerTest</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#Fri Mar 11 14:37:20 EST 2011
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
59 changes: 59 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="AeminiumGPU" basedir="." default="main">

<property name="src.dir" value="src"/>
<property name="build.dir" value="bin"/>
<property name="lib.dir" value="lib"/>
<property name ="main-class" value="code.SimpleMap" />


<property name="spooned.dir" value="spooned"/>
<property name="compiler.src.dir" value="compiler-src"/>
<property name="compiler.build.dir" value="compiler"/>
<property name="compiler.lib.dir" value="compiler-lib"/>

<path id="classpath">
<path refid="compiler.classpath" />
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

<path id="compiler.classpath">
<pathelement location="${compiler.build.dir}" />
<fileset dir="${compiler.lib.dir}" includes="**/*.jar"/>
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

<target name="compile-compiler">
<mkdir dir="${compiler.build.dir}"/>
<javac srcdir="${compiler.src.dir}" destdir="${compiler.build.dir}" classpathref="compiler.classpath" />
</target>

<target name="precompile" depends="compile-compiler">
<!-- define spoon task -->
<taskdef name="spoon" classname="spoon.SpoonTask" classpathref="compiler.classpath"/>

<spoon classpathref="compiler.classpath" verbose= "true">
<sourceSet dir="${src.dir}" />
<templateset dir="${compiler.src.dir}" includes="template/" />
<processor type="processing.MapLambdaProcessor" />
</spoon>
</target>

<target name="compile" depends="precompile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${spooned.dir}" destdir="${build.dir}" classpathref="classpath" />
</target>

<target name="main" depends="compile"></target>

<target name="run" depends="compile">
<java fork="true" classname="${main-class}">
<classpath>
<path location="${build.dir}" />
<path refid="classpath"/>
</classpath>
</java>
</target>


</project>
Binary file not shown.
5 changes: 5 additions & 0 deletions compiler-src/processing/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package processing;

public class Environment {

}
103 changes: 103 additions & 0 deletions compiler-src/processing/MapLambdaProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package processing;

import spoon.processing.AbstractProcessor;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtCodeElement;
import spoon.reflect.code.CtReturn;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.template.Substitution;
import spoon.template.Template;
import template.MapLambdaTemplate;

public class MapLambdaProcessor<T> extends AbstractProcessor<CtMethod<T>>{

@SuppressWarnings("rawtypes")
@Override
public void process(CtMethod<T> target) {
if (target.getSimpleName().equals("call")) {
CtClass parent = target.getParent(CtClass.class);
int c = parent.getMethods().size();
/*
Set<CtMethod> methods = parent.getMethods();
parent.setMethods(methods);
*/

createGetSourceMethod(target);
int c2 = parent.getMethods().size();
System.out.println("Alcides says:" + target.getSimpleName() + " c:" + c + "->" + c2);

}
}

@SuppressWarnings("unchecked")
private StringBuilder visitExpr(CtCodeElement expr, StringBuilder b) {
StringBuilder n = new StringBuilder();
if (expr instanceof CtBlock) {
// Iterate over statements
for (CtStatement st : ((CtBlock<T>) expr).getStatements()) {
n.append(visitExpr(st, b));
n.append("\n");
}

} else if (expr instanceof CtReturn) {
CtReturn<T> ret = (CtReturn<T>) expr;
n.append("return ");
n.append(visitExpr(ret.getReturnedExpression(), b));
n.append(";");

} else if (expr instanceof CtVariableAccess) {
CtVariableAccess<?> va = (CtVariableAccess<?>) expr;
if (va.getVariable().getSimpleName().equals("input")) {
n.append("input");
} else {
// TODO: Check if valid
System.out.println("Type of var " + va.getVariable().getSimpleName() + " is " + va.getVariable().getType());

b.append(va.getVariable().getSimpleName());
}

} else {
System.out.println("Unknown type:" + expr.getClass());
}

return n;
}

private CtMethod<T> createGetSourceMethod(CtMethod<T> target) {
// Template

StringBuilder clCode = new StringBuilder();
CtBlock<T> body = target.getBody();
clCode = visitExpr(body, clCode);

System.out.println(":::: Final Code ::::");
System.out.println(clCode);
System.out.println(":: End Final Code ::");
Template t = new MapLambdaTemplate(clCode.toString());
Substitution.insertAllMethods(target.getParent(CtClass.class), t);
return null;
/*
CtMethod<T> n = new CtMethodImpl<T>();
// Signature
n.setSimpleName("getSource2");
n.setParameters(new ArrayList());
// Return Type
CtTypeReference void_ = new CtTypeReferenceImpl<T>();
void_.setSimpleName("java.lang.String");
n.setType(void_);
// Body
return n;*/
}

}
21 changes: 21 additions & 0 deletions compiler-src/template/MapLambdaTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package template;

import spoon.template.Local;
import spoon.template.Parameter;
import spoon.template.Template;

public class MapLambdaTemplate implements Template {


@Parameter
String _code_;

public String getSource() {
return _code_;
}

@Local
public MapLambdaTemplate(String code) {
_code_ = code;
}
}
Binary file added compiler/annotation/Access.class
Binary file not shown.
Binary file added compiler/annotation/Getter.class
Binary file not shown.
Binary file added compiler/annotation/Setter.class
Binary file not shown.
Binary file added compiler/processing/Environment.class
Binary file not shown.
Binary file added compiler/processing/MapLambdaProcessor.class
Binary file not shown.
Binary file added compiler/processing/ReadAccessProcessor.class
Binary file not shown.
Binary file added compiler/processing/ServerAccessProcessor.class
Binary file not shown.
Binary file added compiler/processing/WriteAccessProcessor.class
Binary file not shown.
Binary file added compiler/template/FieldAccessTemplate.class
Binary file not shown.
Binary file added compiler/template/MapLambdaTemplate.class
Binary file not shown.
Binary file added lib/AeminiumGPU.jar
Binary file not shown.
Binary file added lib/javacl-1.0-beta-6-shaded.jar
Binary file not shown.
Binary file added lib/javacl-demos-1.0-beta-6-shaded.jar
Binary file not shown.
27 changes: 27 additions & 0 deletions src/code/SimpleMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package code;

import aeminium.gpu.lists.IntList;
import aeminium.gpu.lists.PList;
import aeminium.gpu.operations.Lambda;

public class SimpleMap {
public static void main(String[] args) {
IntList in = new IntList();
in.add(1);
in.add(2);
in.add(5);
PList<Integer> out = in.map(new Lambda<Integer, Integer>() {

@Override
public Integer call(Integer input) {
return input;
}

});

for (int i = 0; i < out.size(); i++) {
System.out.println(i + ": " + out.get(i));
}

}
}

0 comments on commit 3e2709c

Please sign in to comment.