/* * 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++;"); } } }
/* * File reader code currently works by using fread. * * The File* is declared outside any function / method. * * This code follows the model in the library of stalling (not pushing) * at end of file (detected by fread returning 0). * */ private static void genFileReaderWork( SIRFileReader filter, Tape outputTape, 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 = "" + filter.getOutputType(); // dispatch to special routine for bit type if (theType.equals("bit")) { genFileReaderWorkBit(filter, outputTape, selfID, p); return; } // get source and destination ids of outgoing tape Tape out = RegisterStreams.getFilterOutStream(filter); int s = out.getSource(); int d = out.getDest(); // template code to generate, using symbolic free vars above. StringBuilder sb = new StringBuilder(); sb.append("\n int __index;\n"); sb.append(" for (__index=0; __index < ____n; __index++) {\n"); if (KjcOptions.compressed) { sb.append(" unsigned char __temp = 0;\n"); sb.append(" FileReader_read(__file_descr__"); sb.append(selfID); sb.append(", &__temp, 1);\n"); sb.append(" PUSH(__temp);\n"); sb.append(" unsigned int __frame_size = __temp;\n"); sb.append(" FileReader_read(__file_descr__"); sb.append(selfID); sb.append(", &__temp, 1);\n"); sb.append(" PUSH(__temp);\n"); sb.append(" __frame_size <<= 8;\n"); sb.append(" __frame_size += __temp;\n"); sb.append(" FileReader_read(__file_descr__"); sb.append(selfID); sb.append(", &__temp, 1);\n"); sb.append(" PUSH(__temp);\n"); sb.append(" __frame_size <<= 8;\n"); sb.append(" __frame_size += __temp;\n"); sb.append(" FileReader_read(__file_descr__"); sb.append(selfID); sb.append(", &__temp, 1);\n"); sb.append(" PUSH(__temp);\n"); sb.append(" __frame_size <<= 8;\n"); sb.append(" __frame_size += __temp;\n"); // the frame size includes the four bytes used to state the frame size sb.append(" FileReader_read(__file_descr__"); sb.append(selfID); sb.append(", (void *)(BUFFER + HEAD), __frame_size - 4);\n"); sb.append(" HEAD += __frame_size - 4;\n"); } else { sb.append(" PUSH(FileReader_read<"); sb.append(theType); sb.append(">(__file_descr__"); sb.append(selfID); sb.append("));\n"); } sb.append(" }\n"); String template = sb.toString(); /* " #ifdef FUSED" + "\n" + " #ifdef NOMOD" + "\n" + " // read directly into buffer" + "\n" + " if (fread(&(BUFFER[HEAD]), sizeof(BUFFER[HEAD]), ____n, FILEREADER)) {" + "\n" + " HEAD+=____n;" + "\n" + " }" + "\n" + " #else" + "\n" + " // wraparound buffer, might have to read in two pieces (but never" + "\n" + " // more, since we would overwrote what we already wrote on this call)" + "\n" + " if (HEAD+____n <= __BUF_SIZE_MASK) {" + "\n" + " // no overflow" + "\n" + " if (fread(&(BUFFER[HEAD]), sizeof(BUFFER[HEAD]), ____n, FILEREADER)) {" + "\n" + " HEAD+=____n;" + "\n" + " }" + "\n" + " } else {" + "\n" + " // overflow, need two pieces" + "\n" + " int piece1 = __BUF_SIZE_MASK - HEAD;" + "\n" + " int piece2 = ____n - piece1;" + "\n" + " if (fread(&(BUFFER[HEAD]), sizeof(BUFFER[HEAD]), piece1, FILEREADER)) {" + "\n" + " if (fread(&(BUFFER[0]), sizeof(BUFFER[HEAD]), piece2, FILEREADER)) {" + "\n" + " HEAD+=____n;" + "\n" + " HEAD&=__BUF_SIZE_MASK;" + "\n" + " }" + "\n" + " }" + "\n" + " }" + "\n" + " #endif" + "\n" + " #else" + "\n" + " // read as a block" + "\n" + " TYPE __buffer[____n];" + "\n" + " int __index;" + "\n" + " if (fread(__buffer, sizeof(__buffer[0]), ____n, FILEREADER)) {" + "\n" + " for (__index=0; __index < ____n; __index++) {" + "\n" + " // should move push out of loop, but not clear if mult-push implemented yet" + "\n" + " PUSH(__buffer[__index]);" + "\n" + " }" + "\n" + " }" + "\n" + " #endif"; */ // set values of free variables String TYPE = theType; String FILEREADER = fpName(filter); String FUSED = "__FUSED_" + s + "_" + d; String NOMOD = "__NOMOD_" + s + "_" + d; String BUFFER = "BUFFER_" + s + "_" + d; String HEAD = "HEAD_" + s + "_" + d; String BUF_SIZE_MASK = "__BUF_SIZE_MASK_" + s + "_" + d; String PUSH = outputTape.getPushName(); // replace templates with correct values template = Utils.replaceAll(template, "TYPE", TYPE); template = Utils.replaceAll(template, "FILEREADER", FILEREADER); template = Utils.replaceAll(template, "FUSED", FUSED); template = Utils.replaceAll(template, "NOMOD", NOMOD); template = Utils.replaceAll(template, "BUFFER", BUFFER); template = Utils.replaceAll(template, "HEAD", HEAD); template = Utils.replaceAll(template, "BUF_SIZE_MASK", BUF_SIZE_MASK); template = Utils.replaceAll(template, "PUSH", PUSH); // output code p.println(template); }