Skip to content

Commit

Permalink
variable codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
PtrMan committed Dec 11, 2015
1 parent be1392b commit 7fdc547
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Type extends ptrman.causalReasoningSystem.functional.tree.Type {
public enum EnumType {
FUNCDAMENTAL_SEQUENCE,

COND,
//COND, is functioncall with method = cond
FUNCTION_DEFINITION,
FUNCTION_CALL,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package ptrman.causalReasoningSystem.functional.language.codegen;

import javassist.CannotCompileException;
import javassist.CtMethod;
import javassist.*;
import ptrman.causalReasoningSystem.functional.language.ast.Element;
import ptrman.causalReasoningSystem.functional.language.ast.Type;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javassist.CtClass;
import ptrman.causalReasoningSystem.functional.language.runtime.ResultAndControlflowPropagationInfo;

/**
Expand All @@ -33,12 +31,26 @@ private enum EnumMathematicalAccumulatorInitialValue {

private final CtClass ctClass;

private String variableContainerClassName;
private CtClass classForVariableContainer;


public CodegenJava(CtClass classTemplate) {
public CodegenJava(ClassPool classPool, CtClass classTemplate) {
this.ctClass = classTemplate;

variableContainerClassName = "VariableContainerForFunctionalFunction0";
classForVariableContainer = classPool.makeClass(variableContainerClassName);
}



public void addVariableFieldsToVariableContainerForGeneratedFunctionalFunction(Map<String, Typeinfo> typeinfoOfVariables) throws CannotCompileException, NotFoundException {
for( String iterationVariablename : typeinfoOfVariables.keySet() ) {
classForVariableContainer.addField(new CtField(CtClass.booleanType, iterationVariablename, classForVariableContainer));
}
}



public String generateFunctionByStaticTypes(Element astEntry, Map<String, Typeinfo> typeinfoOfVariables) {
// TODO< uncomment, currently we are testing stuff >

Expand All @@ -56,10 +68,11 @@ private InlinedResultCodeWithType generateFunctionByStaticTypesInternalFunctionW

// public for testing
public InlinedResultCodeWithType generateBodyForwardPass(Element astElement, Map<String, Typeinfo> typeinfoOfVariables) throws CannotCompileException {
/*
if( astElement.type.isEqualWithType(new Type(Type.EnumType.COND)) ) {
return generateBodyForwardPassForCondition(astElement, typeinfoOfVariables);
}
else if( astElement.type.isEqualWithType(new Type(Type.EnumType.FUNCTION_CALL))) {
}*/
if( astElement.type.isEqualWithType(new Type(Type.EnumType.FUNCTION_CALL))) {
if( astElement.identifier == null ) {
if( astElement.callType == Element.EnumCallType.ADD ) {
return generateBodyForwardPassForMathematicalAccumulatorOperation("+", EnumMathematicalAccumulatorInitialValue.ZERO, astElement, typeinfoOfVariables);
Expand All @@ -78,17 +91,45 @@ else if( astElement.callType == Element.EnumCallType.DIV ) {
}
}
else {
if( astElement.identifier.equals("cond") ) {
return generateBodyForwardPassForCondition(astElement, typeinfoOfVariables);
}


throw new RuntimeException("TODO< generate code for call of function >");
}
}
else if( astElement.type.isEqualWithType(new Type(Type.EnumType.CONSTANT_INTEGER)) ) {
return generateBodyForwardPassForIntegerConstant(astElement, typeinfoOfVariables);
}
else if( astElement.type.isEqualWithType(new Type(Type.EnumType.VARIABLE)) ) {
return generateBodyForwardPassForVariable(astElement, typeinfoOfVariables);
}

else {
throw new RuntimeException("Internal Error!");
}
}

private InlinedResultCodeWithType generateBodyForwardPassForVariable(Element astElement, Map<String, Typeinfo> typeinfoOfVariables) throws CannotCompileException {
final String variableName = astElement.identifier;

if( !typeinfoOfVariables.containsKey(variableName) ) {
throw new RuntimeException("Static compilation Error: Variable " + variableName + " was not found!");
}

final Typeinfo typeinfoOfVariable = typeinfoOfVariables.get(variableName);
final Typeinfo resultType = typeinfoOfVariable;

StringBuilder resultCodeBuilder = new StringBuilder();
resultCodeBuilder.append(String.format("result = %s.%s(variables.%s);\n", RESULT_AND_PROPAGATION_TYPENAME, ResultAndControlflowPropagationInfo.codegenGetJavaFunctionnameForCreationOfType(typeinfoOfVariable), variableName));

final String resultCode = resultCodeBuilder.toString();
final GeneratedFunctionInfo generatedFunctionInfo = emitInternalFunction(resultType, resultCode);

return new InlinedResultCodeWithType(resultType, generatedFunctionInfo);
}

private InlinedResultCodeWithType generateBodyForwardPassForMathematicalAccumulatorOperation(String operationString, EnumMathematicalAccumulatorInitialValue accumulatorInitialValue, Element astElement, Map<String, Typeinfo> typeinfoOfVariables) throws CannotCompileException {
/*assert
astElement.type.isEqualWithType(new Type(Type.EnumType.OPERATION_ADDITION)) ||
Expand Down Expand Up @@ -167,7 +208,7 @@ private List<InlinedResultCodeWithType> inlineResultCodeForAllArguments(Element
}

private InlinedResultCodeWithType generateBodyForwardPassForCondition(Element condition, Map<String, Typeinfo> typeinfoOfVariables) throws CannotCompileException {
assert condition.type.isEqualWithType(new Type(Type.EnumType.COND));
//assert condition.type.isEqualWithType(new Type(Type.EnumType.COND));

Element conditionCheck = (Element)condition.children.get(0);
Element conditionTrue = (Element) condition.children.get(1);
Expand Down Expand Up @@ -218,8 +259,11 @@ private InlinedResultCodeWithType generateBodyForwardPassForIntegerConstant(Elem


private String getJavaFunctioncallForFunction(GeneratedFunctionInfo functionInfo) {
// TODO< parameters >
return functionInfo.getInternalFunctionname() + "()";
return functionInfo.getInternalFunctionname() + "(variables)";
}

private String getJavaClassnameOfVariableContainer() {
return variableContainerClassName;
}


Expand All @@ -234,7 +278,7 @@ private GeneratedFunctionInfo emitInternalFunction(Typeinfo typeinfo, String cod
StringBuilder functionCodeBuilder = new StringBuilder();

// for now public for debugging, later private
functionCodeBuilder.append(String.format("public static %s %s() {\n", RESULT_AND_PROPAGATION_TYPENAME, generatedFunctionInfo.getInternalFunctionname()));
functionCodeBuilder.append(String.format("public static %s %s(%s variables) {\n", RESULT_AND_PROPAGATION_TYPENAME, generatedFunctionInfo.getInternalFunctionname(), getJavaClassnameOfVariableContainer()));

functionCodeBuilder.append("// codegen: result allocation\n");
functionCodeBuilder.append(RESULT_AND_PROPAGATION_TYPENAME + " result;\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ else if( root instanceof BasicIntegerParseTreeElement ) {
result.constantInteger = Integer.parseInt(convertedRoot.constant);
return result;
}

else if( root instanceof HasChildrenParseTreeElement ) {
HasChildrenParseTreeElement convertedRoot = (HasChildrenParseTreeElement)root;

Expand Down Expand Up @@ -82,6 +81,13 @@ else if( convertedRoot.type == HasChildrenParseTreeElement.EnumType.VARIABLE_OR_
// simplfy it
return convert(convertedRoot.children[0]);
}
else if( convertedRoot.type == HasChildrenParseTreeElement.EnumType.VARIABLE ) {
BasicIdentifierParseTreeElement identiferParseTreeElement = (BasicIdentifierParseTreeElement) convertedRoot.children[0];

Element result = new Element(new Type(Type.EnumType.VARIABLE));
result.identifier = identiferParseTreeElement.identifier;
return result;
}
else {
throw new RuntimeException("Internal Error!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@
*/
public class ResultAndControlflowPropagationInfo {
int resultInteger;
boolean resultBoolean;

// called from generated code
public static ResultAndControlflowPropagationInfo createResultForConstantInteger(final int value) {
public static ResultAndControlflowPropagationInfo createResultForInteger(final int value) {
ResultAndControlflowPropagationInfo result = new ResultAndControlflowPropagationInfo();
result.resultInteger = value;
return result;
}

// called from generated code
public static ResultAndControlflowPropagationInfo createResultForBoolean(final boolean value) {
ResultAndControlflowPropagationInfo result = new ResultAndControlflowPropagationInfo();
result.resultBoolean = value;
return result;
}


// used by codegeneration
public static String codegenGetJavaFunctionnameForCreationOfType(Typeinfo typeinfo) {
if( typeinfo.type == Typeinfo.EnumType.INTEGER ) {
return "createResultForConstantInteger";
return "createResultForInteger";
}
else if( typeinfo.type == Typeinfo.EnumType.BOOLEAN ) {
return "createResultForBoolean";
}

throw new RuntimeException("Internal Error: No function for Static Runtime found to create value of type!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
import ptrman.causalReasoningSystem.functional.language.ast.Element;
import ptrman.causalReasoningSystem.functional.language.codegen.CodegenJava;
import ptrman.causalReasoningSystem.functional.language.codegen.Typeinfo;
import ptrman.causalReasoningSystem.functional.language.convertParseTreeToAst.ConvertParseTreeToAst;
import ptrman.causalReasoningSystem.functional.language.parseTree.ParseTreeElement;
import ptrman.causalReasoningSystem.functional.language.parser.Parse;

import java.util.HashMap;
import java.util.Map;

/**
* Created by r0b3 on 11.12.2015.
Expand All @@ -23,13 +26,25 @@ public static void main(String[] args) {
CtClass class0 = ctPool.makeClass("GeneratedCode");


ParseTreeElement parseTree = Parse.parse("(+ 5 4)");
ParseTreeElement parseTree = Parse.parse("(cond booleanX 5 6)");
Element ast = ConvertParseTreeToAst.convert(parseTree);


CodegenJava codegen = new CodegenJava(class0);
CodegenJava codegen = new CodegenJava(ctPool, class0);

Map<String, Typeinfo> variableTypes = new HashMap<>();
variableTypes.put("booleanX", new Typeinfo(Typeinfo.EnumType.BOOLEAN));

try {
codegen.addVariableFieldsToVariableContainerForGeneratedFunctionalFunction(variableTypes);
} catch (CannotCompileException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}

try {
codegen.generateBodyForwardPass(ast, new HashMap<>());
codegen.generateBodyForwardPass(ast, variableTypes);
} catch (CannotCompileException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 7fdc547

Please sign in to comment.