X-Git-Url: http://sraa.de/git/?a=blobdiff_plain;f=dol%2Fsrc%2Fdol%2Fvisitor%2Fprotothread%2FProtothreadModuleVisitor.java;fp=dol%2Fsrc%2Fdol%2Fvisitor%2Fprotothread%2FProtothreadModuleVisitor.java;h=897d49681e806f1d5b2ee580ac0ada8358cc040d;hb=8c411cf24ed0eb889191aaeafd8fa1e69081df42;hp=0000000000000000000000000000000000000000;hpb=dea7a4fb1ed110d3ce6e6d9255103d724bd66c0e;p=jump.git diff --git a/dol/src/dol/visitor/protothread/ProtothreadModuleVisitor.java b/dol/src/dol/visitor/protothread/ProtothreadModuleVisitor.java new file mode 100644 index 0000000..897d496 --- /dev/null +++ b/dol/src/dol/visitor/protothread/ProtothreadModuleVisitor.java @@ -0,0 +1,170 @@ +/* $Id: ProtothreadModuleVisitor.java 1 2010-02-24 13:03:05Z haidw $ */ +package dol.visitor.protothread; + +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.StringTokenizer; +import java.util.Vector; + +import dol.datamodel.pn.Channel; +import dol.datamodel.pn.Port; +import dol.datamodel.pn.Process; +import dol.datamodel.pn.ProcessNetwork; +import dol.util.CodePrintStream; +import dol.visitor.PNVisitor; + +/** + * This class is a class for a visitor that is used to generate + * the main program. + */ +public class ProtothreadModuleVisitor extends PNVisitor { + + /** + * Constructor. + */ + public ProtothreadModuleVisitor(String dir) { + _dir = dir; + } + + public void visitComponent(ProcessNetwork x) { + try { + String filename = _dir + _delimiter + "sc_application.cpp"; + OutputStream file = new FileOutputStream(filename); + _code = new CodePrintStream(file); + + _code.printPrefixln("#include \"pt.h\""); + _code.printPrefixln("#include \"Fifo.h\""); + _code.printPrefixln("#include \"WindowedFifo.h\""); + for (String basename : x.getProcessBasenames()) { + _code.printPrefixln("#include \"" + basename + + "Wrapper.h\""); + } + + _code.println(); + _code.printPrefixln("int main(void)"); + _code.printLeftBracket(); + + //instantiate channels + for (Channel c : x.getChannelList()) { + if (c.getType().equals("fifo")) { + _code.printPrefixln("Fifo " + c.getName() + "(" + + c.getSize() * c.getTokenSize() + ");"); + } else if (c.getType().equals("wfifo")) { + _code.printPrefixln("WindowedFifo " + c.getName() + "(" + + c.getSize() * c.getTokenSize() + ");"); + } + } + _code.println(); + + //instantiate processes + for (Process p : x.getProcessList()) { + _code.printPrefix("int " + p.getName() + + "Indices[] = { "); + Vector iteratorIndex = + p.getIteratorIndices(); + if (iteratorIndex.size() < 4) { + while (iteratorIndex.size() < 4) { + iteratorIndex.add(-1); + } + } else if (iteratorIndex.size() > 4) { + new RuntimeException("Error: Currently not more than " + + "4 iterator dimensions are supported." + + "Consider revising " + p.getBasename() + + "."); + } + for (int i = 0; i < 4; i++) { + if (i < 3) { + _code.print(iteratorIndex.elementAt(i) + ", "); + } else { + _code.println(iteratorIndex.elementAt(i) + " };"); + } + } + _code.printPrefixln(p.getBasename() + "Wrapper *" + + p.getName() + " = new " + + p.getBasename() + "Wrapper(\"" + + p.getName() + "\", " + + p.getName() + "Indices);"); + } + _code.println(); + + //connect the network + for (Process p : x.getProcessList()) { + for (Port port : p.getPortList()) { + if (port.getName().equals(port.getBasename())) { + _code.printPrefixln(p.getName() + + "->_port" + port.getName() + "Fifo = &" + + port.getPeerResource().getName() + ";"); + } else { + _code.printPrefix(p.getName() + + "->_port" + port.getBasename() + + "Fifo"); + StringTokenizer tokenizer = + new StringTokenizer(port.getName(). + replaceFirst(port.getBasename(), ""), "_"); + while (tokenizer.hasMoreTokens()) { + _code.print("[" + tokenizer.nextToken() + + "]"); + } + _code.println(" = &" + + port.getPeerResource().getName() + ";"); + } + } + } + _code.println(); + + //initialize processes + for (Process p : x.getProcessList()) { + _code.printPrefixln(p.getName() + "->init();"); + } + _code.println(); + + /* + _code.printPrefix("while("); + int counter = 0; + for (Process p : x.getProcessList()) { + if (counter > 0) { + _code.printPrefix(" "); + } + _code.print("!" + p.getName() + "->isDetached()"); + if (counter++ < x.getProcessList().size() - 1) { + _code.println(" ||"); + } else { + _code.println(")"); + } + } + */ + _code.printPrefixln("bool allBlocked = false;"); + _code.printPrefixln("while(!allBlocked)"); + _code.printLeftBracket(); + _code.printPrefixln("allBlocked = true;"); + for (Process p : x.getProcessList()) { + _code.printPrefixln("if (!" + p.getName() + + "->isDetached()) {"); + //_code.printPrefixln(" " + p.getName() + "->fire();"); + _code.printPrefixln(" if (" + p.getName() + + "->fire() == PT_ENDED) {"); + _code.printPrefixln(" allBlocked = false;"); + _code.printPrefixln(" }"); + _code.printPrefixln("}"); + } + _code.printRightBracket(); + _code.println(); + + for (Process p : x.getProcessList()) { + _code.printPrefixln("delete " + p.getName() + ";"); + } + _code.println(); + _code.printPrefixln("return 0;"); + _code.printRightBracket(); + } + catch (Exception e) { + System.out.println("ProtothreadModuleVisitor: " + + "exception occured: " + e.getMessage()); + e.printStackTrace(); + } + } + + protected CodePrintStream _code = null; + protected String _dir = null; +} +