/* this function does the transformation involved in replacing an * expression */ static void replace_expression(expr_t* expr,mloop_t* mloop) { symboltable_t *new_temp; expr_t *new_expr; char *new_id = (char *)malloc(10); statement_t *body=T_MLOOP_BODY(mloop); /* drop out if replacement expr already exists */ if (find_replacement(mloop, expr)) return; /* create a new temporary variable */ sprintf(new_id,"__temp%d",idnum); new_temp = alloc_loc_st (S_VARIABLE,new_id); S_DTYPE(new_temp) = T_TYPEINFO(expr); S_VAR_INIT(new_temp) = NULL; S_INIT(new_temp) = build_init(expr,NULL); S_PROHIBITIONS(new_temp) |= TILED_OUTER_MLOOP_FLAG; new_expr = build_0ary_op(VARIABLE,new_temp); idnum++; /* replace the variable */ T_ADD_MLOOP_VAR (mloop, new_temp, T_MLOOP_ORDER(mloop, T_MLOOP_RANK(mloop)-1)); replace_expr(expr, copy_expr(new_expr)); /*add_replacement(mloop,copy_expr(expr),copy_expr(new_expr));*/ /* if necessary, reassign the variable */ if (is_written(new_expr,body)) { statement_t* post; expr_t* reassign; reassign=build_binary_op(BIASSIGNMENT,copy_expr(new_expr),copy_expr(expr)); post=build_expr_statement(reassign,T_LINENO(body),T_FILENAME(body)); T_PROHIBITIONS(post) |= TILED_OUTER_MLOOP_FLAG; T_ADD_MLOOP_POST(mloop,post,T_MLOOP_ORDER(mloop, T_MLOOP_RANK(mloop)-1)); } } /* replace_expression(expr,mloop) */
/* this function determines whether the expr is assigned to in the stmtls, * returning 1 if yes or unsure, 0 if no */ static int is_written(expr_t *expr, statement_t *stmtls) { statement_t *temp; expr_t *stmt_expr; for (temp=stmtls; temp; temp=T_NEXT(temp)) { switch(T_TYPE(temp)) { case S_EXPR: stmt_expr = T_EXPR(temp); if (T_TYPE(stmt_expr)==FUNCTION) { expr_t *args; for (args=nthoperand(stmt_expr,2); args; args=T_NEXT(args)) { if (expr_equal(args,expr)) { return(1); } } } if (expr_equal(left_expr(stmt_expr),expr)) { return(1); } break; case S_IF: if (is_written(expr,T_THEN(T_IF(temp)))) return(1); if (is_written(expr,T_ELSE(T_IF(temp)))) return(1); break; case S_LOOP: if (is_written(expr,T_BODY(T_LOOP(temp)))) return(1); break; case S_NLOOP: if (is_written(expr,T_NLOOP_BODY(T_NLOOP(temp)))) return(1); break; case S_CONTINUE: break; default: printf("**** %d ****/n", T_TYPE(temp)); exit(1); return(1); } } return(0); } /* is_written(expr,stmtls) */
bool find_and_write(uint32_t frame) { pcap_hdr_t pcap_hdr; pcaprec_hdr_t pcap_rec_hdr; uint8_t data[65536]; int32_t fdin; uint32_t fileno, nframe; uint32_t i, n_read, n_write, n_size, n_extracted; char filename[128]; // check if the frame has already been written if (is_written(frame)) { fprintf(stderr, "frame(%d) already written\n", frame); return true; } i = 0; nframe = frame % FRAMES_PER_FILE; fileno = ceil(frame * 1.0 / FRAMES_PER_FILE) - 1; // numbering starts from 00000 snprintf(filename, 128, "%s/%s-%05d.pcap", PCAP_DIR_NAME, PCAP_BASE_NAME, fileno); if (access(filename, F_OK) == -1) { printf("file(%s) for frame(%d) does not exist", filename, frame); return false; } fdin = open(filename, O_RDONLY); if (fdin == -1) { printf("error:(%s)\n", strerror(errno)); return false; } n_size = sizeof(pcap_hdr_t); n_read = read(fdin, (void *) &pcap_hdr, n_size); if (n_read < n_size) { printf("fread(%d).pcap_hdr_t = %d\n", n_size, n_read); return false; } if (!wrote_pcap_hdr) { write(fdout, &pcap_hdr, sizeof(pcap_hdr_t)); wrote_pcap_hdr = true; } printf("frame(%d) @ filename(%s:%d) ", frame, filename, nframe); while (1) { i++; // read pcap record header n_size = sizeof(pcaprec_hdr_t); n_read = read(fdin, &pcap_rec_hdr, n_size); if (n_read < n_size) { // reached the end of the file printf("not found\n"); return false; } if (i != nframe) { lseek(fdin, pcap_rec_hdr.incl_len, SEEK_CUR); continue; } // printf("pcap.rec[%03d], timestamp[%d.%d] incl=%d orig=%d\n", // i, pcap_rec_hdr.ts_sec, pcap_rec_hdr.ts_usec, pcap_rec_hdr.incl_len, pcap_rec_hdr.orig_len); printf("found\n"); n_read = read(fdin, data, pcap_rec_hdr.incl_len); n_write = write(fdout, &pcap_rec_hdr, sizeof(pcaprec_hdr_t)); n_write = write(fdout, data, n_read); written[n_written++] = frame; break; } close(fdin); return true; }