Commit 770a4179 authored by Sascha Niklas Schneiders's avatar Sascha Niklas Schneiders
Browse files

merging local changes into master

parent ba09c2c2
......@@ -8,7 +8,7 @@
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>embedded-montiarc-math-generator</artifactId>
<version>0.0.2</version>
<version>0.0.3-SNAPSHOT</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
......@@ -20,10 +20,11 @@
<mc.grammars.assembly.version>0.0.6-SNAPSHOT</mc.grammars.assembly.version>
<SIUnit.version>0.0.7</SIUnit.version>
<Common-MontiCar.version>0.0.4</Common-MontiCar.version>
<Embedded-MontiArc.version>0.0.4</Embedded-MontiArc.version>
<Embedded-MontiArc-Behaviour.version>0.0.4</Embedded-MontiArc-Behaviour.version>
<Embedded-MontiArc.version>0.0.5</Embedded-MontiArc.version>
<Embedded-MontiArc-Behaviour.version>0.0.5</Embedded-MontiArc-Behaviour.version>
<Math.version>0.0.4</Math.version>
<Embedded-MontiArc-Math.version>0.0.4</Embedded-MontiArc-Math.version>
<Embedded-MontiArc-Math.version>0.0.5</Embedded-MontiArc-Math.version>
<tagging.version>0.0.1</tagging.version>
<!-- .. Libraries .................................................. -->
<guava.version>18.0</guava.version>
<junit.version>4.12</junit.version>
......@@ -50,6 +51,11 @@
</properties>
<dependencies>
<dependency>
<groupId>de.monticore.lang</groupId>
<artifactId>Tagging</artifactId>
<version>${tagging.version}</version>
</dependency>
<dependency>
<groupId>de.se_rwth.commons</groupId>
<artifactId>se-commons-logging</artifactId>
......
......@@ -19,11 +19,9 @@
package de.monticore.lang.monticar.generator;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ComponentSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.Scope;
import java.io.File;
......@@ -41,18 +39,18 @@ public interface Generator {
void setGenerationTargetPath(String newPath);
String generateString(ExpandedComponentInstanceSymbol componentSymbol, MathStatementsSymbol mathStatementsSymbol);
String generateString(TaggingResolver taggingResolver, ExpandedComponentInstanceSymbol componentSymbol, MathStatementsSymbol mathStatementsSymbol);
/**
* This method should generate the source for the ExpandedComponentInstanceSymbol and
* add MathStatementsSymbols, accordingly. Does also do this for all of its subcomponents.
*/
List<FileContent> generateStrings(ExpandedComponentInstanceSymbol componentInstanceSymbol, Scope symtab);
List<FileContent> generateStrings(TaggingResolver taggingResolver, ExpandedComponentInstanceSymbol componentInstanceSymbol, Scope symtab);
/**
* This methods writes the resulting code for the ExpandedComponentInstance and its subcomponents to the corresponding files
*/
List<File> generateFiles(ExpandedComponentInstanceSymbol componentSymbol, Scope symtab) throws IOException;
List<File> generateFiles(TaggingResolver taggingResolver, ExpandedComponentInstanceSymbol componentSymbol, Scope symtab) throws IOException;
boolean useAlgebraicOptimizations();
......
package de.monticore.lang.monticar.generator;
import de.monticore.lang.math.math._symboltable.expression.MathArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol;
/**
* @author Sascha Schneiders
*/
public interface MathBackend {
String getMatrixTypeName();
String getMatrixInitString(int sizeN, int sizeM);
String getRowVectorTypeName();
String getColumnVectorTypeName();
String getColumnAccessCommandName();
String getRowAccessCommandName();
String getBackendName();
String getTransposeCommand();
String getIncludeHeaderName();
String getPowerOfString(MathArithmeticExpressionSymbol mathExpressionSymbol, String valueListString);
String getPowerOfString(MathArithmeticExpressionSymbol mathExpressionSymbol, String valueListString, String seperator);
String getPowerOfString(MathMatrixArithmeticExpressionSymbol mathExpressionSymbol, String valueListString);
String getPowerOfEEString(MathMatrixArithmeticExpressionSymbol mathExpressionSymbol, String valueListString);
String getDivisionEEString(MathMatrixArithmeticExpressionSymbol mathExpressionSymbol, String valueListString);
}
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.math.math._symboltable.expression.MathArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol;
import de.monticore.lang.monticar.generator.MathBackend;
import de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
/**
* @author Sascha Schneiders
*/
public class ArmadilloBackend implements MathBackend {
@Override
public String getMatrixTypeName() {
return "mat";
}
@Override
public String getMatrixInitString(int sizeN, int sizeM) {
return "(" + sizeN + "," + sizeM + ");\n";
}
@Override
public String getRowVectorTypeName() {
return "rowvec";
}
@Override
public String getColumnVectorTypeName() {
return "colvec";
}
@Override
public String getColumnAccessCommandName() {
return "col";
}
@Override
public String getRowAccessCommandName() {
return "row";
}
@Override
public String getBackendName() {
return "ArmadilloBackend";
}
@Override
public String getTransposeCommand() {
return "t";
}
@Override
public String getIncludeHeaderName() {
return "armadillo";
}
@Override
public String getPowerOfString(MathArithmeticExpressionSymbol mathExpressionSymbol, String valueListString) {
return getPowerOfString(mathExpressionSymbol, valueListString, ",");
}
@Override
public String getPowerOfString(MathArithmeticExpressionSymbol mathExpressionSymbol, String valueListString, String separator) {
/*String matrixName = StringValueListExtractorUtil.getElement(valueListString, 0, separator);
String result = matrixName;
String powerOfNumber = StringValueListExtractorUtil.getElement(valueListString, 1, separator);
for (int c = 1; !(c + "").equals(powerOfNumber); ++c) {
result += "*" + matrixName;
}*/
Log.error("Break down power of into smaller multiplications, this is not fully supported by this backend");
return null;
}
@Override
public String getPowerOfString(MathMatrixArithmeticExpressionSymbol mathExpressionSymbol, String valueListString) {
/*String matrixName = StringValueListExtractorUtil.getElement(valueListString, 0);
String result = matrixName;
for (int c = 1; !(c + "").equals(StringValueListExtractorUtil.getElement(valueListString, 1)); ++c) {
result += "*" + matrixName;
}*/
Log.error("Break down power of into smaller multiplications, this is not fully supported by this backend");
return null;
}
@Override
public String getPowerOfEEString(MathMatrixArithmeticExpressionSymbol mathExpressionSymbol, String valueListString) {
return "pow" + valueListString;
}
@Override
public String getDivisionEEString(MathMatrixArithmeticExpressionSymbol mathExpressionSymbol, String valueListString) {
return ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol.getLeftExpression(), new ArrayList<>()) + "/" +
ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol.getRightExpression(), new ArrayList<>());
}
}
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.monticar.generator.FileContent;
/**
* @author Sascha Schneiders
*/
public class ArmadilloHelper {
public static String fileName = "HelperA";
public static FileContent getArmadilloHelperFileContent() {
FileContent fileContent = new FileContent();
fileContent.setFileName(fileName + ".h");
String fileContentString = ArmadilloHelperSource.armadilloHelperSourceCode;
fileContent.setFileContent(fileContentString);
return fileContent;
}
}
package de.monticore.lang.monticar.generator.cpp;
/**
* @author Sascha Schneiders
*/
public class ArmadilloHelperSource {
public static String armadilloHelperSourceCode = "#ifndef HELPERA_H\n" +
"#define HELPERA_H\n" +
"#define _GLIBCXX_USE_CXX11_ABI 0\n" +
"#include <iostream>\n" +
"#include \"armadillo.h\"\n" +
"#include <stdarg.h>\n" +
"#include <initializer_list>\n" +
"using namespace arma;\n" +
"class HelperA{\n" +
"public:\n" +
"static mat getEigenVectors(mat A){\n" +
"vec eigenValues;\n" +
"mat eigenVectors;\n" +
"eig_sym(eigenValues,eigenVectors,A);\n" +
"return eigenVectors;\n" +
"}\n" +
"static vec getEigenValues(mat A){\n" +
"vec eigenValues;\n" +
"mat eigenVectors;\n" +
"eig_sym(eigenValues,eigenVectors,A);\n" +
"return eigenValues;\n" +
"}\n" +
"static mat getKMeansClusters(mat A, int k){\n" +
"mat clusters;\n" +
"kmeans(clusters,A,k,random_subset,10,true);\n" +
"return clusters;\n" +
"}\n" +
"\n" +
"static mat getSqrtMat(mat A){\n" +
"cx_mat result=sqrtmat(A);\n" +
"return real(result);\n" +
"}\n" +
"};\n" +
"#endif\n";
}
......@@ -57,8 +57,12 @@ public class BluePrintCPP extends BluePrint {
Log.info(resolutionDeclarationSymbol.getNameToResolve(), "ResDecl:");
ASTSubComponent subComponent = componentSymbol.getInstanceInformation().get().getASTSubComponent();
int number = InstanceInformation.getInstanceNumberFromASTSubComponent(subComponent, index);
//if(resolutionDeclarationSymbol.getNameToResolve().equals("targetEigenvectors")){
Log.info(subComponent.toString(), "InfoKK:");
//}
if (number == -1) {
Log.info(subComponent.toString(), "No number added");
Log.info(subComponent.toString(), "No number added for" + resolutionDeclarationSymbol.getNameToResolve());
++index;
break;
}
fixSubComponentInstanceNumbers(componentSymbol, resolutionDeclarationSymbol.getNameToResolve(), number, index);
......
......@@ -4,6 +4,7 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.Expanded
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cpp.converter.ComponentConverter;
import de.monticore.lang.monticar.generator.order.ImplementExecutionOrder;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
......@@ -15,14 +16,14 @@ import java.util.Map;
* @author Sascha Schneiders
*/
public class ExecutionOrderFixer {
public static void fixExecutionOrder(BluePrintCPP bluePrintCPP, GeneratorCPP generatorCPP) {
public static void fixExecutionOrder(TaggingResolver taggingResolver, BluePrintCPP bluePrintCPP, GeneratorCPP generatorCPP) {
Method method = bluePrintCPP.getMethod("execute").get();
Map<String, List<Instruction>> map = new HashMap<>();
List<ExpandedComponentInstanceSymbol> threadableSubComponents = bluePrintCPP.getOriginalSymbol().getIndependentSubComponents();
List<Instruction> otherInstructions = computeOtherInstructions(map, method);
List<ExpandedComponentInstanceSymbol> exOrder = ImplementExecutionOrder.exOrder(bluePrintCPP.getOriginalSymbol());
List<Instruction> newList=getExecutionOrderInstructionsList(exOrder, map, bluePrintCPP, threadableSubComponents);
List<ExpandedComponentInstanceSymbol> exOrder = ImplementExecutionOrder.exOrder(taggingResolver, bluePrintCPP.getOriginalSymbol());
List<Instruction> newList = getExecutionOrderInstructionsList(exOrder, map, bluePrintCPP, threadableSubComponents);
fixSlistExecutionOrder(bluePrintCPP.getOriginalSymbol(), newList, bluePrintCPP, threadableSubComponents, generatorCPP);
List<TargetCodeInstruction> joinInstructions = new ArrayList<>();
......
......@@ -2,13 +2,11 @@ package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.monticar.generator.BluePrint;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.Generator;
import de.monticore.lang.monticar.generator.Helper;
import de.monticore.lang.monticar.generator.MathCommandRegister;
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.resolver.Resolver;
import de.monticore.lang.monticar.generator.cpp.resolver.SymTabCreator;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.Scope;
import de.se_rwth.commons.logging.Log;
......@@ -37,6 +35,15 @@ public class GeneratorCPP implements Generator {
public GeneratorCPP() {
this.mathCommandRegister = new MathCommandRegisterCPP();
useOctaveBackend();
}
public void useArmadilloBackend() {
MathConverter.curBackend = new ArmadilloBackend();
}
public void useOctaveBackend() {
MathConverter.curBackend = new OctaveBackend();
}
public static void main(String[] args) throws IOException, URISyntaxException {
......@@ -45,7 +52,7 @@ public class GeneratorCPP implements Generator {
String outputPath = args[2];
SymTabCreator symTabCreator = new SymTabCreator(resolvingPath);
Scope symtab = symTabCreator.createSymTab();
TaggingResolver symtab = symTabCreator.createSymTabAndTaggingResolver();
Resolver resolver = new Resolver(symtab);
ExpandedComponentInstanceSymbol componentSymbol = resolver.getExpandedComponentInstanceSymbol(fullName)
......@@ -53,13 +60,13 @@ public class GeneratorCPP implements Generator {
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setGenerationTargetPath(outputPath);
generatorCPP.generateFiles(componentSymbol, symtab);
generatorCPP.generateFiles(symtab, componentSymbol, symtab);
}
public String generateString(ExpandedComponentInstanceSymbol componentInstanceSymbol, Scope symtab) {
public String generateString(TaggingResolver taggingResolver, ExpandedComponentInstanceSymbol componentInstanceSymbol, Scope symtab) {
MathStatementsSymbol mathSymbol = Helper.getMathStatementsSymbolFor(componentInstanceSymbol, symtab);
return generateString(componentInstanceSymbol, mathSymbol);
return generateString(taggingResolver, componentInstanceSymbol, mathSymbol);
}
@Override
......@@ -73,7 +80,7 @@ public class GeneratorCPP implements Generator {
}
@Override
public String generateString(ExpandedComponentInstanceSymbol componentSymbol, MathStatementsSymbol mathStatementsSymbol) {
public String generateString(TaggingResolver taggingResolver, ExpandedComponentInstanceSymbol componentSymbol, MathStatementsSymbol mathStatementsSymbol) {
LanguageUnitCPP languageUnitCPP = new LanguageUnitCPP();
languageUnitCPP.setGeneratorCPP(this);
languageUnitCPP.addSymbolToConvert(componentSymbol);
......@@ -88,14 +95,14 @@ public class GeneratorCPP implements Generator {
}
}
String result = languageUnitCPP.getGeneratedHeader(bluePrintCPP);
String result = languageUnitCPP.getGeneratedHeader(taggingResolver, bluePrintCPP);
return result;
}
public static List<FileContent> currentFileContentList = null;
@Override
public List<FileContent> generateStrings(ExpandedComponentInstanceSymbol componentInstanceSymbol, Scope symtab) {
public List<FileContent> generateStrings(TaggingResolver taggingResolver, ExpandedComponentInstanceSymbol componentInstanceSymbol, Scope symtab) {
List<FileContent> fileContents = new ArrayList<>();
if (componentInstanceSymbol.getFullName().equals("simulator.mainController")) {
setGenerateSimulatorInterface(true);
......@@ -103,7 +110,7 @@ public class GeneratorCPP implements Generator {
//setGenerateMainClass(true);
}
currentFileContentList = fileContents;
fileContents.add(new FileContent(generateString(componentInstanceSymbol, symtab), componentInstanceSymbol));
fileContents.add(new FileContent(generateString(taggingResolver, componentInstanceSymbol, symtab), componentInstanceSymbol));
String lastNameWithoutArrayPart = "";
for (ExpandedComponentInstanceSymbol instanceSymbol : componentInstanceSymbol.getSubComponents()) {
//fileContents.add(new FileContent(generateString(instanceSymbol, symtab), instanceSymbol));
......@@ -117,11 +124,13 @@ public class GeneratorCPP implements Generator {
}
if (generateComponentInstance) {
fileContents.addAll(generateStrings(instanceSymbol, symtab));
fileContents.addAll(generateStrings(taggingResolver, instanceSymbol, symtab));
}
}
fileContents.add(OctaveHelper.getOctaveHelperFileContent());
if (MathConverter.curBackend.getBackendName().equals("OctaveBackend"))
fileContents.add(OctaveHelper.getOctaveHelperFileContent());
if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend"))
fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent());
if (shouldGenerateMainClass()) {
//fileContents.add(getMainClassFileContent(componentInstanceSymbol, fileContents.get(0)));
......@@ -132,8 +141,9 @@ public class GeneratorCPP implements Generator {
return fileContents;
}
public List<File> generateFiles(ExpandedComponentInstanceSymbol componentSymbol, Scope symtab) throws IOException {
List<FileContent> fileContents = generateStrings(componentSymbol, symtab);
public List<File> generateFiles(TaggingResolver taggingResolver, ExpandedComponentInstanceSymbol componentSymbol,
Scope symtab) throws IOException {
List<FileContent> fileContents = generateStrings(taggingResolver, componentSymbol, symtab);
//System.out.println(fileContents);
if (getGenerationTargetPath().charAt(getGenerationTargetPath().length() - 1) != '/') {
setGenerationTargetPath(getGenerationTargetPath() + "/");
......@@ -146,7 +156,12 @@ public class GeneratorCPP implements Generator {
return files;
}
public File generateFile(FileContent fileContent) throws IOException{
public List<File> generateFiles(ExpandedComponentInstanceSymbol componentSymbol,
TaggingResolver taggingResolver) throws IOException {
return generateFiles(taggingResolver, componentSymbol, taggingResolver);
}
public File generateFile(FileContent fileContent) throws IOException {
File f = new File(getGenerationTargetPath() + fileContent.getFileName());
Log.info(f.getName(), "FileCreation:");
if (!f.exists()) {
......
......@@ -23,8 +23,10 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.Expanded
import de.monticore.lang.math.math._symboltable.MathStatementsSymbol;
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cpp.converter.ComponentConverter;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.instruction.ConnectInstructionCPP;
import de.monticore.lang.monticar.generator.order.ImplementExecutionOrder;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.Symbol;
import de.se_rwth.commons.logging.Log;
......@@ -79,8 +81,8 @@ public class LanguageUnitCPP extends LanguageUnit {
}
}
public String getGeneratedHeader(BluePrintCPP bluePrint) {
ExecutionOrderFixer.fixExecutionOrder(bluePrint, (GeneratorCPP) bluePrint.getGenerator());
public String getGeneratedHeader(TaggingResolver taggingResolver, BluePrintCPP bluePrint) {
ExecutionOrderFixer.fixExecutionOrder(taggingResolver, bluePrint, (GeneratorCPP) bluePrint.getGenerator());
String resultString = "";
//guard defines
resultString += "#ifndef " + bluePrint.getName().toUpperCase() + "\n";
......@@ -93,8 +95,13 @@ public class LanguageUnitCPP extends LanguageUnit {
List<String> alreadyGeneratedIncludes = new ArrayList<>();
//includes
//add default include
resultString += "#include \"octave/oct.h\"\n";
alreadyGeneratedIncludes.add("octave/oct");
if (MathConverter.curBackend.getBackendName().equals("OctaveBackend")) {
resultString += "#include \"octave/oct.h\"\n";
alreadyGeneratedIncludes.add("octave/oct");
} else if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend")) {
resultString += "#include \"" + MathConverter.curBackend.getIncludeHeaderName() + ".h\"\n";
alreadyGeneratedIncludes.add(MathConverter.curBackend.getIncludeHeaderName());
}
for (Variable v : bluePrint.getVariables()) {
//TODO remove multiple same includes
if (v.hasInclude()) {
......@@ -111,7 +118,15 @@ public class LanguageUnitCPP extends LanguageUnit {
for (String include : includeStrings) {
resultString += include;
}
if (generatorCPP.useThreadingOptimizations()) {
//if(MathConverter.curBackend.getBackendName().equals("OctaveBackend"))
//resultString+="#include \"mingw.thread.h\"\n";
//else if(MathConverter.curBackend.getBackendName().equals("ArmadilloBackend"))
resultString += "#include <thread>\n";
}
if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend")) {
resultString += "using namespace arma;\n";
}
//class definition start
resultString += "class " + bluePrint.getName() + "{\n";
......
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.math.math._symboltable.expression.MathArithmeticExpressionSymbol;
import de.monticore.lang.math.math._symboltable.matrix.MathMatrixArithmeticExpressionSymbol;
import de.monticore.lang.monticar.generator.MathBackend;
import de.se_rwth.commons.logging.Log;
/**
* @author Sascha Schneiders
*/
public class LinalgBackend implements MathBackend {
@Override
public String getMatrixTypeName() {
return null;
}
@Override
public String getMatrixInitString(int sizeN, int sizeM) {
return "(" + sizeN + "," + sizeM + ");\n";
}
@Override
public String getRowVectorTypeName() {
return null;
}
@Override
public String getColumnVectorTypeName() {
return null;
}
@Override
public String getColumnAccessCommandName() {
return null;
}
@Override
public String getRowAccessCommandName() {
return null;
}
@Override
public String getBackendName() {
return null;
}
@Override
public String getTransposeCommand() {
return null;
}
@Override
public String getIncludeHeaderName() {
return null;
}
@Override
public String getPowerOfString(MathArithmeticExpressionSymbol mathExpressionSymbol, String valueListString) {
Log.error("Not supported yet");
return null;
}
@Override
public String getPowerOfString(MathArithmeticExpressionSymbol mathExpressionSymbol, String valueListString, String seperator) {
Log.error("Not supported yet");
return null;
}
@Override
public String getPowerOfString(MathMatrixArithmeticExpressionSymbol mathExpressionSymbol, String valueListString) {
Log.error("Not supported yet");
return null;
}