Exemplo n.º 1
0
  /**
   * Create code for the work function of a built-in filter.
   *
   * @param filter The filter that the work function is being built for
   * @param selfID A unique id assigned in FlatIRToC
   * @param p A printer to output the generated code
   */
  static void predefinedFilterWork(SIRPredefinedFilter filter, int selfID, CodegenPrintWriter p) {

    Tape inputTape = RegisterStreams.getFilterInStream(filter);
    Tape outputTape = RegisterStreams.getFilterOutStream(filter);

    // Caller has printed function name and an iteration parameter ____n
    // Generate loop to execute body of this function ____n times,
    // In loop generate specialized code for function.
    p.print("void " + ClusterUtils.getWorkName(filter, selfID) + "(int ____n) {");
    p.indent();
    p.newLine();
    p.indent();
    //        p.println("// predefinedFilterWork " + filter.getName());

    // SIRFileReader
    if (filter instanceof SIRFileReader) {
      if (!(KjcOptions.mencoder || KjcOptions.blender)) {
        genFileReaderWork((SIRFileReader) filter, outputTape, selfID, p);
      }
      // SIRFileWriter
    } else if (filter instanceof SIRFileWriter) {
      if (!(KjcOptions.mencoder || KjcOptions.blender)) {
        genFileWriterWork((SIRFileWriter) filter, inputTape, selfID, p);
      }
      // SIRIdentity
    } else if (filter instanceof SIRIdentity) {
      assert false : "should not process SIRIdentity here";
      p.println("for (; 0 < ____n; ____n--) {");
      p.indent();
      p.println("  " + outputTape.getPushName() + "(" + inputTape.getPopName() + "());");
      p.outdent();
      p.print("}"); // end of for loop.
    } else if (filter instanceof SIRDummySink || filter instanceof SIRDummySource) {
      // DummySource and SummySink do not appear in any of our
      // application code.  Are they part of the language?
      // TODO: get right exception for unimplemented.
      throw new Error("Unsupported predefined filter " + filter.getName());
    } else {
      // TODO: get right unchecked exception for unextended code...
      throw new Error("Unknown predefined filter " + filter.getName());
    }
    p.newLine();
    p.outdent(); // end of method body
    p.outdent(); // end of method definition
    p.print("}");
    p.newLine();
    p.newLine();
  }
Exemplo n.º 2
0
  /*
   * File writer code currently works by using fwrite.
   *
   * The File* is declared outside any function / method.
   *
   * There is special case code for FileReader<bit> since individual
   * bits can not be read in by any system routine that I know.
   * The current bits and the count of unprocessed bits are created
   * outside of
   */
  private static void genFileWriterWork(
      SIRFileWriter fw, Tape inputTape, int selfID, CodegenPrintWriter p) {

    if (KjcOptions.asciifileio) {
      System.err.println("Error: -asciifileio not supported in cluster backend.");
      System.err.println("Exiting...");
      System.exit(1);
    }

    String theType = "" + fw.getInputType();
    if (theType.equals("bit")) {
      // the bit type is special since you can not just read or
      // write a bit.  It requires buffering in some larger
      // integer type.
      String bits_to_go = bitsToGoName(fw);
      String the_bits = theBitsName(fw);

      p.println("unsigned char __buffer[____n/8+1];");
      p.println("int __index = 0;");
      p.println("for (; 0 < ____n; ____n--) {");
      p.indent();

      p.println(
          the_bits
              + " = ("
              + bits_type
              + ") (("
              + the_bits
              + " << 1) | ("
              + inputTape.getPopName()
              + "() & 1));");
      p.println(bits_to_go + "--;");
      p.println("if (" + bits_to_go + " == 0) {");
      p.indent();
      p.println("__buffer[__index++] = " + the_bits + ";");
      p.println(the_bits + " = 0;");
      p.println(bits_to_go + " = 8 * sizeof(" + the_bits + ");");
      p.outdent();
      p.println("}");
      p.outdent();
      p.println("}");
      p.println(
          "fwrite(__buffer, " + "sizeof(" + the_bits + "), " + "__index, " + fpName(fw) + ");");
    } else {
      // not a bit type.  write directly to file without needing to buffer bits.

      p.println("\n  int __index;");
      p.println("  for (__index=0; __index < ____n; __index++) {");
      if (KjcOptions.compressed) {
        Tape out = RegisterStreams.getFilterInStream(fw);
        int s = out.getSource();
        int d = out.getDest();

        String BUFFER = "BUFFER_" + s + "_" + d;
        String TAIL = "TAIL_" + s + "_" + d;

        String pop = inputTape.getPopName() + "()";
        p.println("    unsigned char __temp = " + pop + ";");
        p.println("    FileWriter_write<unsigned char>(__file_descr__" + selfID + ", __temp);");
        p.println("    unsigned int __frame_size = __temp;");

        p.println("    __temp = " + pop + ";");
        p.println("    FileWriter_write<unsigned char>(__file_descr__" + selfID + ", __temp);");
        p.println("    __frame_size <<= 8;");
        p.println("    __frame_size += __temp;");

        p.println("    __temp = " + pop + ";");
        p.println("    FileWriter_write<unsigned char>(__file_descr__" + selfID + ", __temp);");
        p.println("    __frame_size <<= 8;");
        p.println("    __frame_size += __temp;");

        p.println("    __temp = " + pop + ";");
        p.println("    FileWriter_write<unsigned char>(__file_descr__" + selfID + ", __temp);");
        p.println("    __frame_size <<= 8;");
        p.println("    __frame_size += __temp;");

        // the frame size includes the four bytes used to state the frame size
        p.println(
            "    FileWriter_write(__file_descr__"
                + selfID
                + ", (void *)("
                + BUFFER
                + " + "
                + TAIL
                + "), __frame_size - 4);");
        p.println("    " + TAIL + " += __frame_size - 4;");
      } else {
        p.println(
            "    FileWriter_write<"
                + theType
                + ">(__file_descr__"
                + selfID
                + ", "
                + inputTape.getPopName()
                + "());");
      }
      p.println("  }\n");

      /*
             NetStream in = RegisterStreams.getFilterInStream(fw);
             // source and destination of incoming stream
             int s = in.getSource();
             int d = in.getDest();

             p.println("#ifdef __FUSED_" + s + "_" + d);
             p.indent();
             {
                 p.println("fwrite(&(BUFFER_" + s + "_" + d + "[TAIL_" + s + "_" + d + "]), " +
                           "sizeof(BUFFER_" + s + "_" + d + "[TAIL_" + s + "_" + d + "]), " +
                           "____n, " + fpName(fw) + ");");
                 p.println("TAIL_" + s + "_" + d + "+=____n;");
             }
             p.outdent();
             p.println("#else");
             p.indent();
             {
                 p.println("fwrite(&(__pop_buf__" + selfID + "[__tail__" + selfID + "]), " +
                           "sizeof(__pop_buf__" + selfID + "[__tail__" + selfID + "]), " +
                           "____n, " + fpName(fw) + ");");
                 p.println("__tail__" + selfID + "+=____n;");
             }
             p.outdent();
             p.println("#endif");
      */

      if (KjcOptions.numbers > 0) {
        p.println("  stats_output_count++;");
      }
    }
  }