blob: af9481f62ca3eaec2eaa1ce7a53be04a23ea72f0 [file] [log] [blame]
/**
* Copyright (c) 2020 Robert Bosch GmbH.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
*/
package org.eclipse.app4mc.slg.commons.m2t.generators;
import org.eclipse.app4mc.amalthea.model.DataSize;
import org.eclipse.app4mc.amalthea.model.Label;
import org.eclipse.app4mc.slg.commons.m2t.AmaltheaModelUtils;
import org.eclipse.app4mc.transformation.util.OutputBuffer;
import org.eclipse.xtend2.lib.StringConcatenation;
public class LabelTranslationUnit extends TranslationUnit {
public static String getModulePathStatic() {
return "synthetic_gen" + "/" + getModuleNameStatic();
}
public static String getMakeFilePathStatic() {
return getModulePathStatic() + "/" + "CMakeLists.txt";
}
public static String getModuleNameStatic() {
return "labels";
}
public static String getLibName() {
return "LABELS_LIB";
}
public String getModuleName() {
return LabelTranslationUnit.getModuleNameStatic();
}
protected String getName() {
return this.label.getName();
}
public String getInitCall() {
StringConcatenation builder = new StringConcatenation();
builder.append("initialize_");
builder.append(getName());
builder.append("()");
return builder.toString();
}
public String readCall(final String param) {
StringConcatenation builder = new StringConcatenation();
builder.append("read_");
builder.append(getName());
builder.append("(");
builder.append(param);
builder.append(")");
return builder.toString();
}
public String writeCall(final String param) {
StringConcatenation builder = new StringConcatenation();
builder.append("write_");
builder.append(getName());
builder.append("(");
builder.append(param);
builder.append(")");
return builder.toString();
}
public String getIncFile() {
return getModuleName() + ".h";
}
private final Label label;
private OutputBuffer outputBuffer;
public LabelTranslationUnit(final OutputBuffer outputBuffer, final Label label) {
super();
this.label = label;
this.outputBuffer = outputBuffer;
this.genFiles();
}
public void genFiles() {
if (isSrcFileEmpty()) {
srcAppend("#include \"" + getIncFile() + "\"\n\n");
}
incAppend(this.toH());
srcAppend(this.toCpp());
}
private String toH() {
StringConcatenation _builder = new StringConcatenation();
{
boolean _isIncFileEmpty = this.isIncFileEmpty();
if (_isIncFileEmpty) {
String _xblockexpression = null;
{
this.incAppend("#include <stdbool.h>\n\n");
_xblockexpression = "";
}
_builder.append(_xblockexpression);
_builder.newLineIfNotEmpty();
}
}
_builder.append("void ");
String _initCall = this.getInitCall();
_builder.append(_initCall);
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("void ");
String _readCall = this.readCall("int labelAccessStatistics");
_builder.append(_readCall);
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("void ");
String _writeCall = this.writeCall("int labelAccessStatistics");
_builder.append(_writeCall);
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
return _builder.toString();
}
private String toCpp() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("int ");
String _name = this.getName();
_builder.append(_name);
_builder.append("[");
long _labelArraySize = AmaltheaModelUtils.getLabelArraySize(this.label);
_builder.append(_labelArraySize);
_builder.append("];\t");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("static bool isIinitialized_");
String _name_1 = this.getName();
_builder.append(_name_1);
_builder.append(" = false;");
_builder.newLineIfNotEmpty();
_builder.append("void ");
String _initCall = this.getInitCall();
_builder.append(_initCall);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("if (!isIinitialized_");
String _name_2 = this.getName();
_builder.append(_name_2, "\t");
_builder.append("){");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("for (int i=0; i < ");
long _labelArraySize_1 = AmaltheaModelUtils.getLabelArraySize(this.label);
_builder.append(_labelArraySize_1, "\t\t");
_builder.append("; i++){");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_3 = this.getName();
_builder.append(_name_3, "\t\t\t");
_builder.append("[i] = i+1;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("isIinitialized_");
String _name_4 = this.getName();
_builder.append(_name_4, "\t\t");
_builder.append(" = true;");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.newLine();
_builder.append("void ");
String _readCall = this.readCall("int labelAccessStatistics");
_builder.append(_readCall);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("int numberOfBytes = ");
Label _label = null;
if (this != null) {
_label = this.label;
}
DataSize _size = null;
if (_label != null) {
_size = _label.getSize();
}
long _numberBytes = 0l;
if (_size != null) {
_numberBytes = _size.getNumberBytes();
}
_builder.append(_numberBytes, "\t");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("for (int repeat = 0 ; repeat < labelAccessStatistics; repeat++){");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if(numberOfBytes < 4){");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("numberOfBytes = 4;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int arraysize = sizeof(");
String _name_5 = this.getName();
_builder.append(_name_5, "\t\t");
_builder.append(") / 4;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("//printf(\"number of bytes:%d\\n\",arraysize);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int leftOverElements=arraysize%10;");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int arraySizeWith10Multiples=arraysize-leftOverElements;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int i = 0;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int a = 0;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (i = 0; i < arraySizeWith10Multiples; i = i + 10) { //iteration with 10 reads");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_6 = this.getName();
_builder.append(_name_6, "\t\t\t");
_builder.append("[i];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_7 = this.getName();
_builder.append(_name_7, "\t\t\t");
_builder.append("[i+1];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_8 = this.getName();
_builder.append(_name_8, "\t\t\t");
_builder.append("[i+2];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_9 = this.getName();
_builder.append(_name_9, "\t\t\t");
_builder.append("[i+3];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_10 = this.getName();
_builder.append(_name_10, "\t\t\t");
_builder.append("[i+4];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_11 = this.getName();
_builder.append(_name_11, "\t\t\t");
_builder.append("[i+5];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_12 = this.getName();
_builder.append(_name_12, "\t\t\t");
_builder.append("[i+6];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_13 = this.getName();
_builder.append(_name_13, "\t\t\t");
_builder.append("[i+7];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_14 = this.getName();
_builder.append(_name_14, "\t\t\t");
_builder.append("[i+8];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_15 = this.getName();
_builder.append(_name_15, "\t\t\t");
_builder.append("[i+9];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for(;i<arraysize;i++){");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("a = ");
String _name_16 = this.getName();
_builder.append(_name_16, "\t\t\t");
_builder.append("[i];");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("void ");
String _writeCall = this.writeCall("int labelAccessStatistics");
_builder.append(_writeCall);
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("int numberOfBytes = ");
Label _label_1 = null;
if (this != null) {
_label_1 = this.label;
}
DataSize _size_1 = null;
if (_label_1 != null) {
_size_1 = _label_1.getSize();
}
long _numberBytes_1 = 0l;
if (_size_1 != null) {
_numberBytes_1 = _size_1.getNumberBytes();
}
_builder.append(_numberBytes_1, "\t");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("for (int repeat = 0 ; repeat < labelAccessStatistics; repeat++){");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if(numberOfBytes < 4){");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("numberOfBytes = 4;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int arraysize = sizeof(");
String _name_17 = this.getName();
_builder.append(_name_17, "\t\t");
_builder.append(") / 4;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("int leftOverElements=arraysize%10;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int arraySizeWith10Multiples=arraysize-leftOverElements;");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("int i = 0;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (i = 0; i < arraySizeWith10Multiples; i = i + 10) {");
_builder.newLine();
_builder.append("\t\t\t");
String _name_18 = this.getName();
_builder.append(_name_18, "\t\t\t");
_builder.append("[i] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_19 = this.getName();
_builder.append(_name_19, "\t\t\t");
_builder.append("[i+1] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_20 = this.getName();
_builder.append(_name_20, "\t\t\t");
_builder.append("[i+2] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_21 = this.getName();
_builder.append(_name_21, "\t\t\t");
_builder.append("[i+3] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_22 = this.getName();
_builder.append(_name_22, "\t\t\t");
_builder.append("[i+4] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_23 = this.getName();
_builder.append(_name_23, "\t\t\t");
_builder.append("[i+5] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_24 = this.getName();
_builder.append(_name_24, "\t\t\t");
_builder.append("[i+6] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_25 = this.getName();
_builder.append(_name_25, "\t\t\t");
_builder.append("[i+7] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_26 = this.getName();
_builder.append(_name_26, "\t\t\t");
_builder.append("[i+8] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
String _name_27 = this.getName();
_builder.append(_name_27, "\t\t\t");
_builder.append("[i+9] = 0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for(;i<arraysize;i++){");
_builder.newLine();
_builder.append("\t\t\t\t");
String _name_28 = this.getName();
_builder.append(_name_28, "\t\t\t\t");
_builder.append("[i]=0xAFFE;");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
return _builder.toString();
}
protected long getSize() {
return this.label.getSize().getNumberBytes();
}
public String getBasePath() {
return "synthetic_gen";
}
public String getModulePath() {
String _basePath = this.getBasePath();
String _plus = (_basePath + "/");
String _moduleName = this.getModuleName();
return (_plus + _moduleName);
}
@Override
public String getSrcFile() {
String _moduleName = this.getModuleName();
return (_moduleName + ".cpp");
}
public String getIncPath() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_inc/");
String _incFile = this.getIncFile();
return (_plus + _incFile);
}
public String getSrcPath() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_src/");
String _srcFile = this.getSrcFile();
return (_plus + _srcFile);
}
public boolean isIncFileEmpty() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_inc/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
boolean _bufferExists = this.outputBuffer.bufferExists("INC", _plus_1);
return (!_bufferExists);
}
public boolean isSrcFileEmpty() {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_src/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
boolean _bufferExists = this.outputBuffer.bufferExists("SRC", _plus_1);
return (!_bufferExists);
}
public boolean incAppend(final String str) {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_inc/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
return this.outputBuffer.appendTo("INC", _plus_1, str);
}
public boolean srcAppend(final String str) {
String _modulePath = this.getModulePath();
String _plus = (_modulePath + "/_src/");
String _moduleName = this.getModuleName();
String _plus_1 = (_plus + _moduleName);
return this.outputBuffer.appendTo("SRC", _plus_1, str);
}
}