VarType CodeBlock::make_cast(SymbolPtr p, VarType v1, VarType v2) {
	if (v1 != v2) {
		if (v1 == INTEGER && v2 == FLOATING) {
			// cast back to integer
			write_raw("CASTSI");
			return INTEGER;
		} else if (v1 == FLOATING && v2 == INTEGER) {
			// cast back to floating
			write_raw("CASTSF");
			return FLOATING;
		} else if ((v1 == STRING && v2 != STRING)
				   || (v1 != STRING && v2 == STRING)
				   || (v1 == VOID || v2 == VOID)) {
			if ((v1 == STRING_LITERAL && v2 == STRING) || (v1 == STRING && v2 == STRING_LITERAL)) {
				return STRING;
			}
			// need to catch boolean casting issues here...
			this->valid = false;
			report_error_lc("Semantic Error", "Unable to cast "
							+ var_type_to_string(v1)
							+ " to " + var_type_to_string(v2),
							p->get_row(), p->get_col());
			return VOID;
		} else if ((v1 == BOOLEAN && v2 != BOOLEAN)
				   || (v1 != BOOLEAN && v2 == BOOLEAN)) {
			return v2;
		} else {
			// fine
			return v2;
		}
	} else {
		return v2;
	}
	return VOID;
}
void ConditionalBlock::generate_post() {
	// generate the final condition
	if (this->cond == COND_ELSE) {
		// if there's an else statement...
		// break from previous if statement to exit
		write_raw("BR " + this->exit_label + "\n");
		// write an exit label (end of the if statement)
		// beginning of the else statement
		write_raw(this->exit_label + ":\n");
	} else if (this->cond == COND_IF) {
		if (this->connected != nullptr) {
			// if there is an else statement
			ConditionalBlockPtr extender = static_pointer_cast<ConditionalBlock>(this->connected);
			if (extender->get_conditional_type() == COND_ELSE) {
				// at the end of the else, branch to the exit
				write_raw("BR " + extender->exit_label + "\n");
				// write the exit label
				write_raw(extender->else_label + ":\n");
			}
		} else {
			// end of if statement with no else
			write_raw(this->exit_label + ":\n");
		}
	}
}
void AssignmentBlock::generate_post() {
	// pop into assigner
	SymDataPtr post_assigner = static_pointer_cast<SymData>(this->assigner);
	make_cast(post_assigner, post_assigner->get_var_type(), this->expr_type);
	write_raw("POP " + post_assigner->get_address());
	write_raw("");
}
void ConditionalBlock::generate_pre() {
	// generate condition if
	if (this->get_conditional_type() == COND_IF) {
		VarType result = this->generate_expr(this->get_symbol_list());
		if (result != BOOLEAN) {
			report_error_lc("Semantic Error",
							"Conditional expression doesn't evaluate to boolean value.",
							(*this->get_symbol_list()->begin())->get_row(),
							(*this->get_symbol_list()->begin())->get_col());
		}
		// true, jump to the body
		write_raw("\nBRTS " + this->body_label);
		if (this->connected != nullptr) {
			ConditionalBlockPtr extender = static_pointer_cast<ConditionalBlock>(this->connected);
			if (extender->get_conditional_type() == COND_ELSE) {
				// if there's an else part, jump on false to else
				write_raw("BR " + extender->else_label + "\n");
			}
		} else {
			// beginning of if statement with no else
			write_raw("BR " + this->exit_label + "\n");
		}
		// begin the if body part with a label
		write_raw(this->body_label + ":\n");
	}
}
// Activation block types (body and call)
void ActivationBlock::generate_pre() {
	// do nothing at the moment
	if (this->activity == DEFINITION) {
		// write begin label
		write_raw(this->begin_label + ":\n");
		// get locals
		string name = this->record->get_symbol_name();
		SymbolListPtr named_items = this->get_analyzer()->get_symtable()->find(name);
		SymbolListPtr locals = SymTable::filter_nest_level(SymTable::filter_data(named_items), this->get_nesting_level());
		// generate code to push them
		if (locals->size() > 0) {
			for (auto i = locals->begin(); i != locals->end(); i++) {
				if (static_pointer_cast<SymData>(*i)->get_var_type() == STRING) {
					write_raw("PUSH #\"\"");
				} else if (static_pointer_cast<SymData>(*i)->get_var_type() == FLOATING) {
					write_raw("PUSH #0.0");
				} else {
					write_raw("PUSH #0");
				}
			}
		}
	} else {
		// call
		if (this->activation == PROCEDURE) {
			
		} else if (this->activation == FUNCTION) {
			
		}
	}
}
示例#6
0
	void write_raw(std::ostream& os, const std::string& s)
	{
		for (int i = 0; i < s.size(); ++i)
		{
			write_raw(os, s[i]);
		}
		write_raw(os, '\0');
	}
void LoopBlock::generate_pre() {
	if (this->type == RPTUNTLLOOP) {
		write_raw(this->body_label + ":\n");
	} else if (this->type == WHILELOOP) {
		write_raw(this->cond_label + ":\n");
		VarType result = this->generate_expr(this->get_symbol_list());
		if (result != BOOLEAN) {
			report_error_lc("Semantic Error",
							"Conditional expression doesn't evaluate to boolean value.",
							(*this->get_symbol_list()->begin())->get_row(),
							(*this->get_symbol_list()->begin())->get_col());
		}
		write_raw("\nBRTS " + this->body_label);
		write_raw("BR " + this->exit_label);
		write_raw(this->body_label + ":\n");
	} else if (this->type == FORLOOP) {
		// parse the assignment
		// process the assignment
		AssignmentBlockPtr assignment = AssignmentBlockPtr(new AssignmentBlock(false));
		assignment->set_analyzer(this->get_analyzer());
		for (auto i = 0; i < 3; i++) {
			assignment->catch_token((*this->get_unprocessed())[i]);
		}
		// generate its code
		assignment->preprocess();
		assignment->generate_pre();
		assignment->generate_post();
		// generate the condition label
		write_raw(this->cond_label + ":\n");
		// process the ordinal expression
		AssignmentBlockPtr ordinal_expr = AssignmentBlockPtr(new AssignmentBlock(true));
		ordinal_expr->set_analyzer(this->get_analyzer());
		for (unsigned int i = 4; i < this->get_unprocessed()->size(); i++) {
			ordinal_expr->catch_token((*this->get_unprocessed())[i]);
		}
		// get the comparison components for the ordinal expr
		TokenPtr incrementer = (*this->get_unprocessed())[3];
		if (incrementer->get_token() == MP_TO) {
			ordinal_expr->catch_token(TokenPtr(new Token(MP_EQUALS, "=", -1, -1)));
		} else if (incrementer->get_token() == MP_DOWNTO) {
			ordinal_expr->catch_token(TokenPtr(new Token(MP_EQUALS, "=", -1, -1)));
		}
		ordinal_expr->catch_token((*this->get_unprocessed())[0]);
		if (incrementer->get_token() == MP_TO) {
			ordinal_expr->catch_token(TokenPtr(new Token (MP_MINUS, "-", -1, -1)));
			ordinal_expr->catch_token(TokenPtr(new Token (MP_INT_LITERAL, "1", -1, -1)));
		} else if (incrementer->get_token() == MP_DOWNTO) {
			ordinal_expr->catch_token(TokenPtr(new Token (MP_PLUS, "+", -1, -1)));
			ordinal_expr->catch_token(TokenPtr(new Token (MP_INT_LITERAL, "1", -1, -1)));
		}
		// generate its code
		ordinal_expr->preprocess();
		ordinal_expr->generate_pre();
		write_raw("\nBRFS " + this->body_label);
		write_raw("BR " + this->exit_label + "\n");
		write_raw(this->body_label + ":\n");
	}
}
示例#8
0
文件: eraseblk.cpp 项目: vobject/ffsp
ssize_t write_meta_data(fs_context& fs)
{
    /*
     * Copy erase block usage info and the content of the inode map
     * into one continuous buffer so that we can initiate one
     * cluster-aligned write request into the first erase block.
     */

    size_t eb_usage_size = fs.neraseblocks * sizeof(eraseblock);
    memcpy(fs.buf, fs.eb_usage.data(), eb_usage_size);

    size_t ino_map_size = fs.nino * sizeof(uint32_t);
    memcpy(fs.buf + eb_usage_size, fs.ino_map.data(), ino_map_size);

    size_t meta_data_size = eb_usage_size + ino_map_size;
    uint64_t offset = fs.clustersize;

    ssize_t rc = write_raw(*fs.io_ctx, fs.buf, meta_data_size, offset);
    if (rc < 0)
    {
        log().error("writing meta data to first erase block failed");
        return rc;
    }
    debug_update(fs, debug_metric::write_raw, static_cast<uint64_t>(rc));
    return rc;
}
示例#9
0
void dump_frame(BIFSVID b2v, char *conv_buf, char *out_path, u32 dump_type, avi_t *avi_out, u32 frameNum)
{
	u32 k;
	M4VideoSurface fb;

	/*lock it*/
	SR_GetScreenBuffer(b2v.sr, &fb);
	/*export frame*/
	switch (dump_type) {
	case 0:
		/*reverse frame*/
		for (k=0; k<fb.height; k++) {
			memcpy(conv_buf + k*fb.width*3, fb.video_buffer + (fb.height-k-1) * fb.pitch, sizeof(char) * fb.width  * 3);
		}
		if (AVI_write_frame(avi_out, conv_buf, fb.height*fb.width*3, 1) <0)
			printf("Error writing frame\n");
		break;
	case 2:
		write_raw(&fb, out_path, frameNum);
		break;
	case 1:
		write_bmp(&fb, out_path, frameNum);
		break;
	}
	/*unlock it*/
	SR_ReleaseScreenBuffer(b2v.sr, &fb);
}
示例#10
0
/* Consumes file name */
struct resource *
make_pid_file_resource(struct lsh_string *file)
{
  const char *cname = lsh_get_cstring(file);
  int fd;

  assert (cname);

  /* Try to open the file atomically. This provides sufficient locking
   * on normal (non-NFS) file systems. */

  fd = open(cname, O_WRONLY | O_CREAT | O_EXCL, 0644);

  if (fd < 0)
    {
      if (errno != EEXIST)
	werror("Failed to open pid file '%S': %e.\n",
		 file, errno);
      else
	/* FIXME: We could try to detect and ignore stale pid files. */
	werror("Pid file '%S' already exists.\n", file);
      
      lsh_string_length(file);
      return NULL;
    }
  else
    {
      struct lsh_string *pid = ssh_format("%di", getpid());

      if (!write_raw(fd, STRING_LD(pid)))
	{
	  werror("Writing pidfile `%S' failed: %e\n",
		 file, errno);

	  /* Attempt unlinking file */
	  if (unlink(cname) < 0)
	    werror("Unlinking pid file '%S' failed: %e.\n",
		   file, errno);

	  close(fd);
	  
	  lsh_string_free(pid);
	  lsh_string_free(file);

	  return NULL;
	}
      else
	{      
	  NEW(pid_file_resource, self);
	  init_resource(&self->super, do_kill_pid_file);

	  self->file = file;

	  lsh_string_free(pid);
	  close(fd);
	  
	  return &self->super;
	}
    }
}
示例#11
0
void OSD::write_S(uint8_t c){
  if(c == 0xff){
    row++;
    calc_pos();
  } else
    write_raw(c);
}
示例#12
0
文件: socket.c 项目: jspaleta/VT_RST3
int process_socket(int sock,int inpipe) {

    fd_set fdset; /* selected file descriptors */
    int poll,i;

    struct timeval tv;

    signal(SIGPIPE,SIG_IGN);
    signal(SIGUSR1,trap_reset);
    listen(sock,5);

    tv.tv_sec=0;
    tv.tv_usec=0;

    poll=0;
    runloop=1;
    do {

        FD_ZERO(&fdset);
        FD_SET(inpipe,&fdset);
        if (poll==0) {
            if (poll_sock(sock,NULL,&fdset) !=0) continue;
        } else poll_sock(sock,&tv,&fdset);

        /* open any new connections if possible */

        open_sock(sock,&fdset);

        poll=0;

        /* check to see if the root server has sent any data */

        if (FD_ISSET(inpipe,&fdset)) {
            int size;
            size=read(inpipe,mbuf,BUF_SIZE);
            if (size==0) break;
            write_raw(mbuf,size);
        }

        /* send the data to the clients */

        if (write_sock() !=0) poll=1;

        /* read back any data from the clients */

        read_sock(&fdset,tmpbuf,BUF_SIZE);

        /* decode the buffers here */

    } while(runloop);

    /* close all the clients down */

    for (i=0; i<msgmax; i++) {
        if (client[i].sock !=0) close(client[i].sock);
    }
    close(sock);
    return -1;
}
示例#13
0
文件: gcov.c 项目: bjzhang/xen
static int write_string(write_iter_t *iter, const char *s)
{
    int ret;
    size_t len = strlen(s);

    chk(write32(iter, len));
    return write_raw(iter, s, len);
}
示例#14
0
文件: gcov.c 项目: bjzhang/xen
static int write_gcov(write_iter_t *iter)
{
    struct gcov_info *info;
    int ret;

    /* reset offset */
    iter->write_offset = 0;

    /* dump all files */
    for ( info = info_list ; info; info = info->next )
    {
        const struct gcov_ctr_info *ctr;
        int type;
        size_t size_fn = sizeof(struct gcov_fn_info);

        align_iter(iter);
        chk(write32(iter, XENCOV_TAG_FILE));
        chk(write32(iter, info->version));
        chk(write32(iter, info->stamp));
        chk(write_string(iter, info->filename));

        /* dump counters */
        ctr = info->counts;
        for ( type = -1; next_type(info, &type) < XENCOV_COUNTERS; ++ctr )
        {
            align_iter(iter);
            chk(write32(iter, XENCOV_TAG_COUNTER(type)));
            chk(write32(iter, ctr->num));
            chk(write_raw(iter, ctr->values,
                          ctr->num * sizeof(ctr->values[0])));

            size_fn += sizeof(unsigned);
        }

        /* dump all functions together */
        align_iter(iter);
        chk(write32(iter, XENCOV_TAG_FUNC));
        chk(write32(iter, info->n_functions));
        chk(write_raw(iter, info->functions, info->n_functions * size_fn));
    }

    /* stop tag */
    align_iter(iter);
    chk(write32(iter, XENCOV_TAG_END));
    return 0;
}
示例#15
0
int main(int argc, char **argv)	// argv are the command-line arguments
{
 
	char infname[512], outfname[512];	// file names for input and output
	unsigned char imagetypein, imagetypeout;
	IMAGE *data;
	IMAGE *target;

	if(argc<5)		// too few command-line arguments?
	{
		printf("Command-line usage:\n");
		printf("   %s (inf) (outf) (x) (y)\n",argv[0]);
		printf("   (inf)  is the input file name\n");
		printf("   (outf) is the output file name\n");
		printf("   (x), (y) are the image dimensions\n");
		exit(0);
	}

	// Allocate local memory for struct pointers
	data = malloc(sizeof(IMAGE));

	// Handle Command Line args
	strcpy(infname,nextargs);	// read input file name
	strcpy(outfname,nextargs);	// read output file name
	data->xmax = nextargi;		// Read image dimensions
	data->ymax = nextargi; 
	//data->zmax = nextargi;
	data->zmax = 1;

	// params set image data types in and out
	imagetypein  = UCHARIMAGE;
	imagetypeout = FLOATIMAGE;

	// Read Image into Mem
	printf("Reading image %s with dimensions %d, %d, %d\n",infname,data->xmax,data->ymax,data->zmax);
	if(read_raw(infname, data)){  printf("Error reading file\n");  exit (1);  }

	// Set target params, Allocate local memory
  	target = malloc(sizeof(IMAGE));
	if(copyimage(target, data, imagetypeout)){  fprintf(stderr,"Could not Create Image: target\n");  exit(-1);  }

	/* Image Processing calls here */
	printf("   Converting Data Types...\n");
	//switch case with command line parameters to specify data type conversions
	if(uchar2float(target, data)){ printf("Error Converting\n");  exit(3); }

	// Write Image to File
	printf("Writing processed image %s with dimensions %d, %d, %d\n",outfname,target->xmax,target->ymax,target->zmax);
	if(write_raw(outfname, target)){  printf("Error writing file\n");  exit (4);  }

	// Free All Memory
	removeimage(data,imagetypein);
	removeimage(target,imagetypeout);
	printf("Program completed successfully\n");
	exit (0);

}
示例#16
0
文件: log.cpp 项目: gwozniak/twinkle
void t_log::write_report(const string &report, const string &func_name,
		t_log_class log_class, t_log_severity severity)
{
	if (log_disabled) return;

	write_header(func_name, log_class, severity);
	write_raw(report);
	write_endl();
	write_footer();
}
示例#17
0
文件: format.c 项目: renatomaia/TIER
static int writer_raw(lua_State* L)
{
	writer_t* writer = lua_touserdata(L, 1);
	size_t size;
	const char* str = lua_tolstring(L, 2, &size);
	if(str == NULL)	luaL_error(L, "string expected");
	
	write_raw(writer, (uint8_t*)str, size);
	return 0;
}
// Program Block stuff
void ProgramBlock::generate_pre() {
	// get the program id and set it as the open file
	TokenPtr p = *this->get_unprocessed()->begin();
	// open a file to write to
	this->get_analyzer()->open_file(p->get_lexeme());
	// generate program entry point
	write_raw("MOV SP D0");
	// push begin symbols
	for (auto i = get_symbol_list()->begin(); i != get_symbol_list()->end(); i++) {
		if (static_pointer_cast<SymData>(*i)->get_var_type() == STRING) {
			write_raw("PUSH #\"\"");
		} else if (static_pointer_cast<SymData>(*i)->get_var_type() == FLOATING) {
			write_raw("PUSH #0.0");
		} else {
			write_raw("PUSH #0");
		}
	}
	write_raw("");
}
int DFInstanceLinux::write_string(const VIRTADDR &addr, const QString &str) {
    // Ensure this operation is done as one transaction
    attach();
    VIRTADDR buffer_addr = get_string(str);
    if (buffer_addr)
        // This unavoidably leaks the old buffer; our own
        // cannot be deallocated anyway.
        write_raw(addr, sizeof(VIRTADDR), &buffer_addr);
    detach();
    return buffer_addr ? str.length() : 0;
}
// IO Block stuff
void IOBlock::generate_pre() {
	if (this->action == IO_WRITE) {
		for (auto i = this->expressions->begin();
			 i != this->expressions->end(); i++) {
			SymbolListPtr postfixes = this->convert_postfix(*i);
			this->generate_expr(postfixes);
			write_raw("WRTS");
		}
	} else if (this->action == IO_READ) {
		for (auto i = this->get_symbol_list()->begin();
			 i != this->get_symbol_list()->end(); i++) {
			if ((*i)->get_symbol_type() == SYM_DATA) {
				SymDataPtr p = static_pointer_cast<SymData>(*i);
				if (p->get_var_type() == INTEGER || p->get_var_type() == BOOLEAN) {
					write_raw("RD " + p->get_address());
				} else if (p->get_var_type() == FLOATING) {
					write_raw("RDF " + p->get_address());
				} else if (p->get_var_type() == STRING) {
					write_raw("RDS " + p->get_address());
				}
			} else if ((*i)->get_symbol_type() == SYM_CONSTANT) {
				report_error_lc("Semantic Error", "Cannot read to a constant.",
								(*i)->get_row(), (*i)->get_col());
			}
		}
	}
	if (this->line_terminator) {
		write_raw("PUSH #\"\"");
		write_raw("WRTLNS");
	}
}
示例#21
0
int main(int argc, char **argv)	// argv are the command-line arguments
{
 
	char infname[512], outfname[512];	// file names for input and output
	IMAGE *data;
	IMAGE *target;

	if(argc<5)		// too few command-line arguments?
	{
		printf("Command-line usage:\n");
		printf("   %s (inf) (outf) (x) (y)\n",argv[0]);
		printf("   (inf)  is the input file name\n");
		printf("   (outf) is the output file name\n");
		printf("   (x), (y) are the image dimensions\n");
		//printf("   (x), (y), (z) are the image dimensions\n");
		exit(0);
	}

	// Allocate local memory for struct pointers
	data = malloc(sizeof(IMAGE));

	// Handle Command Line args
	strcpy(infname,nextargs);	// read input file name
	strcpy(outfname,nextargs);	// read output file name
	data->xmax = nextargi;		// Read image dimensions
	data->ymax = nextargi; 
	data->zmax = 1;	//nextargi;

	// Read Image, Allocate Img mem
	printf("Reading image %s with dimensions %d, %d, %d\n",infname,data->xmax,data->ymax,data->zmax);
	if(read_raw(infname, data)){ printf("Error reading file\n");  exit (1);	}

	// Set target params, Allocate local memory
  	target = malloc(sizeof(IMAGE));
	if(copyimage(target, data)){ fprintf(stderr,"Could not Create Image: target\n");  exit(-1); }

	/* Image Processing calls here */
	printf("   Drawing AutoStereogram...\n");
	if(heightmap(data)){ printf("Error Making Height Map\n");  exit(3); }
	if(DrawAutoStereogram(target, data)){ printf("Error Generating SIRDS\n");  exit(3); }

	// Write Image
	printf("Writing processed image %s with dimensions %d, %d, %d\n",outfname,target->xmax,target->ymax,target->zmax);
	if(write_raw(outfname, target)){ printf("Error writing file\n");  exit (4); }

	// Free All Memory
	removeimage(data);
	removeimage(target);
	printf("Program completed successfully\n");
	exit (0);

}
示例#22
0
文件: format.c 项目: renatomaia/TIER
static int writer_stream(lua_State* L)
{
	writer_t* writer = lua_touserdata(L, 1);
	if(lua_type(L, 2) != LUA_TSTRING)
		luaL_error(L, "string expected");

	size_t size;
	const char* str = lua_tolstring(L, 2, &size);
	
	write_varint(writer, size);
	write_raw(writer, (uint8_t*)str, size);
	return 0;
}
示例#23
0
文件: httpconn.c 项目: bf4/pidgin-mac
static gboolean
msn_httpconn_poll(gpointer data)
{
	MsnHttpConn *httpconn;
	char *header;
	char *auth;

	httpconn = data;

	g_return_val_if_fail(httpconn != NULL, FALSE);

	if ((httpconn->host == NULL) || (httpconn->full_session_id == NULL))
	{
		/* There's no need to poll if the session is not fully established */
		return TRUE;
	}

	if (httpconn->waiting_response)
	{
		/* There's no need to poll if we're already waiting for a response */
		return TRUE;
	}

	auth = msn_httpconn_proxy_auth(httpconn);

	header = g_strdup_printf(
		"POST http://%s/gateway/gateway.dll?Action=poll&SessionID=%s HTTP/1.1\r\n"
		"Accept: */*\r\n"
		"Accept-Language: en-us\r\n"
		"User-Agent: MSMSGS\r\n"
		"Host: %s\r\n"
		"Proxy-Connection: Keep-Alive\r\n"
		"%s" /* Proxy auth */
		"Connection: Keep-Alive\r\n"
		"Pragma: no-cache\r\n"
		"Content-Type: application/x-msn-messenger\r\n"
		"Content-Length: 0\r\n\r\n",
		httpconn->host,
		httpconn->full_session_id,
		httpconn->host,
		auth ? auth : "");

	g_free(auth);

	if (write_raw(httpconn, header, strlen(header)))
		httpconn->waiting_response = TRUE;

	g_free(header);

	return TRUE;
}
示例#24
0
int main(int argc, char **argv)
{
  int nr_opts;


  nr_opts = process_args(argc, argv);

  if (argc - nr_opts == 0) {
    /* no filename given -> invent one */
    char my_hostname[100];
    gethostname(my_hostname, 100);
    if (strlen(file_ext) == 0)
      strcpy(file_ext, STD_EXT);
    sprintf(filename, "%s.%s.%d%s",
	    PROGNAME, my_hostname, (int)time(NULL), file_ext);
  } else {
    if (strlen(argv[nr_opts]) > FILENAME_LEN - strlen(RAW_EXT)
	                        - strlen(file_ext)) {
      fprintf(stderr, "Error: given filename too long\n");
      exit(1);
    }
    strcpy(filename, argv[nr_opts]);
    strcat(filename, file_ext);
  }
  strcpy(raw_filename, filename);
  strcat(raw_filename, RAW_EXT);
  strcpy(fb_filename, filename);
  strcat(fb_filename, FB_EXT);

  if (!opt_process_raw_input_only) {
    get_fb();

    if (opt_write_fb)
      write_fb(fb_mem, fb_size);

    convert_fb_to_raw();

    INFO("Writing raw picture data to %s\n", raw_filename);
    write_raw(raw_pic_mem, raw_pic_size);
  }

  if (opt_convert)
    convert_raw_to_pic(filename, raw_filename, xres, yres);

  if (opt_delete_raw)
    if (unlink(raw_filename) == -1)
      perror(raw_filename);
  
  return 0;
}
void LoopBlock::generate_post() {
	if (this->type == RPTUNTLLOOP) {
		write_raw(this->cond_label + ":\n");
		VarType result = this->generate_expr(this->get_symbol_list());
		if (result != BOOLEAN) {
			report_error_lc("Semantic Error",
							"Conditional expression doesn't evaluate to boolean value.",
							(*this->get_symbol_list()->begin())->get_row(),
							(*this->get_symbol_list()->begin())->get_col());
		}
		write_raw("\nBRFS " + this->body_label);
		write_raw("BR " + this->exit_label + "\n");
		write_raw(this->exit_label + ":\n");
	} else if (this->type == WHILELOOP) {
		write_raw("BR " + this->cond_label);
		write_raw(this->exit_label + ":\n");
	} else if (this->type == FORLOOP) {
		// get the incrementer token
		TokenPtr incrementer = (*this->get_unprocessed())[3];
		if (incrementer->get_token() == MP_TO) {
			// generate an incrementer
			AssignmentBlockPtr inc = AssignmentBlockPtr(new AssignmentBlock(false));
			inc->set_analyzer(this->get_analyzer());
			inc->catch_token((*this->get_unprocessed())[0]);
			inc->catch_token((*this->get_unprocessed())[0]);
			inc->catch_token(TokenPtr(new Token(MP_PLUS, "+", -1, -1)));
			inc->catch_token(TokenPtr(new Token(MP_INT_LITERAL, "1", -1, -1)));
			inc->preprocess();
			inc->generate_pre();
			inc->generate_post();
		} else if (incrementer->get_token() == MP_DOWNTO) {
			// generate a decrementer
			AssignmentBlockPtr dec = AssignmentBlockPtr(new AssignmentBlock(false));
			dec->set_analyzer(this->get_analyzer());
			dec->catch_token((*this->get_unprocessed())[0]);
			dec->catch_token((*this->get_unprocessed())[0]);
			dec->catch_token(TokenPtr(new Token(MP_MINUS, "-", -1, -1)));
			dec->catch_token(TokenPtr(new Token(MP_INT_LITERAL, "1", -1, -1)));
			dec->preprocess();
			dec->generate_pre();
			dec->generate_post();
		}
		write_raw("BR " + this->cond_label + "\n");
		write_raw(this->exit_label + ":\n");
	}
}
void ActivationBlock::generate_post() {
	if (this->activity == DEFINITION) {
		// get locals
		string name = this->record->get_symbol_name();
		SymbolListPtr named_items = this->get_analyzer()->get_symtable()->find(name);
		SymbolListPtr locals = SymTable::filter_nest_level(SymTable::filter_data(named_items), this->get_nesting_level());
		unsigned long locals_size = locals->size();
		// move stack ptr minus local variables
		if (locals_size > 0) {
			write_raw("PUSH SP");
			write_raw("PUSH #" + conv_string(locals_size));
			write_raw("SUBS ");
			write_raw("POP SP");
		}
		write_raw("RET\n");
	} else {
		// call
		if (this->activation == PROCEDURE) {
			
		} else if (this->activation == FUNCTION) {
			
		}
	}
}
/* Runs the filters on an input file */
static void test_file(const char *input_filename, const char *output_filename)
{
    size_t frames;
    int i;
    double NQ = 44100 / 2; /* nyquist frequency */
    struct timespec tp1, tp2;
    struct eq *eq;

    float *data = read_raw(input_filename, &frames);

    /* Set some data to 0 to test for denormals. */
    for (i = frames / 10; i < frames; i++)
        data[i] = 0.0;

    /* Left eq chain */
    eq = eq_new();
    eq_append_biquad(eq, BQ_PEAKING, 380/NQ, 3, -10);
    eq_append_biquad(eq, BQ_PEAKING, 720/NQ, 3, -12);
    eq_append_biquad(eq, BQ_PEAKING, 1705/NQ, 3, -8);
    eq_append_biquad(eq, BQ_HIGHPASS, 218/NQ, 0.7, -10.2);
    eq_append_biquad(eq, BQ_PEAKING, 580/NQ, 6, -8);
    eq_append_biquad(eq, BQ_HIGHSHELF, 8000/NQ, 3, 2);
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
    process(eq, data, frames);
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
    printf("processing takes %g seconds for %zu samples\n",
           tp_diff(&tp2, &tp1), frames);
    eq_free(eq);

    /* Right eq chain */
    eq = eq_new();
    eq_append_biquad(eq, BQ_PEAKING, 450/NQ, 3, -12);
    eq_append_biquad(eq, BQ_PEAKING, 721/NQ, 3, -12);
    eq_append_biquad(eq, BQ_PEAKING, 1800/NQ, 8, -10.2);
    eq_append_biquad(eq, BQ_PEAKING, 580/NQ, 6, -8);
    eq_append_biquad(eq, BQ_HIGHPASS, 250/NQ, 0.6578, 0);
    eq_append_biquad(eq, BQ_HIGHSHELF, 8000/NQ, 0, 2);
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp1);
    process(eq, data + frames, frames);
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tp2);
    printf("processing takes %g seconds for %zu samples\n",
           tp_diff(&tp2, &tp1), frames);
    eq_free(eq);

    write_raw(output_filename, data, frames);
    free(data);
}
示例#28
0
int main(int argc, char **argv)
{
  struct lsh_decode_key_options *options = make_lsh_decode_key_options();
  struct lsh_string *input;
  struct lsh_string *output;
  
  int out = STDOUT_FILENO;
  
  argp_parse(&main_argp, argc, argv, 0, NULL, options);

  if (options->file)
    {
      out = open(lsh_get_cstring(options->file),
                 O_WRONLY | O_CREAT, 0666);
      if (out < 0)
        {
          werror("Failed to open file `%S' for writing: %e.\n",
                 options->file, errno);
          return EXIT_FAILURE;
        }
    }

  input = io_read_file_raw(STDIN_FILENO, 3000);
  if (!input)
    {
      werror("Failed to read stdin: %e.\n", errno);
      return EXIT_FAILURE;
    }

  if (options->base64)
    {
      if (!lsh_string_base64_decode(input))
	{
          werror("Invalid base64 encoding.\n");

	  lsh_string_free(input);

          return EXIT_FAILURE;
        }
    }
  
  output = lsh_decode_key(input);
  lsh_string_free(input);

  if (!output)
    {
      werror("Invalid ssh2 key.\n");
      return EXIT_FAILURE;
    }
    
  if (!write_raw(out, STRING_LD(output)))
    {
      werror("Write failed: %e.\n", errno);
      return EXIT_FAILURE;
    }
  lsh_string_free(output);
  
  gc_final();
  
  return EXIT_SUCCESS;
}
示例#29
0
void kiss_session::write(frame_ptr fp) {
	std::vector<unsigned char> kiss_data;
	kiss_encode(fp->get_data().data(), fp->get_data().size(), &kiss_data);
	write_raw(kiss_data.data(), kiss_data.size());
}
示例#30
0
int main(int argc, char **argv)
{
	char *rawfilename = NULL;
	int numiter = 250;
	int use_apc = 1;
	int use_normalization = 0;
	conjugrad_float_t lambda_single = F001; // 0.01
	conjugrad_float_t lambda_pair = FInf;
	conjugrad_float_t lambda_pair_factor = F02; // 0.2
	int conjugrad_k = 5;
	conjugrad_float_t conjugrad_eps = 0.01;

	parse_option *optList, *thisOpt;

	char *optstr;
	char *old_optstr = malloc(1);
	old_optstr[0] = 0;
	optstr = concat("r:i:n:w:k:e:l:ARh?", old_optstr);
	free(old_optstr);

#ifdef OPENMP
	int numthreads = 1;
	old_optstr = optstr;
	optstr = concat("t:", optstr);
	free(old_optstr);
#endif

#ifdef CUDA
	int use_def_gpu = 0;
	old_optstr = optstr;
	optstr = concat("d:", optstr);
	free(old_optstr);
#endif

#ifdef MSGPACK
	char* msgpackfilename = NULL;
	old_optstr = optstr;
	optstr = concat("b:", optstr);
	free(old_optstr);
#endif

	optList = parseopt(argc, argv, optstr);
	free(optstr);

	char* msafilename = NULL;
	char* matfilename = NULL;
	char* initfilename = NULL;

	conjugrad_float_t reweighting_threshold = F08; // 0.8

	while(optList != NULL) {
		thisOpt = optList;
		optList = optList->next;

		switch(thisOpt->option) {
#ifdef OPENMP
			case 't':
				numthreads = atoi(thisOpt->argument);

#ifdef CUDA
				use_def_gpu = -1; // automatically disable GPU if number of threads specified
#endif
				break;
#endif
#ifdef CUDA
			case 'd':
				use_def_gpu = atoi(thisOpt->argument);
				break;
#endif
#ifdef MSGPACK
			case 'b':
				msgpackfilename = thisOpt->argument;
				break;
#endif
			case 'r':
				rawfilename = thisOpt->argument;
				break;
			case 'i':
				initfilename = thisOpt->argument;
				break;
			case 'n':
				numiter = atoi(thisOpt->argument);
				break;
			case 'w':
				reweighting_threshold = (conjugrad_float_t)atof(thisOpt->argument);
				break;
			case 'l':
				lambda_pair_factor = (conjugrad_float_t)atof(thisOpt->argument);
				break;
			case 'k':
				conjugrad_k = (int)atoi(thisOpt->argument);
				break;
			case 'e':
				conjugrad_eps = (conjugrad_float_t)atof(thisOpt->argument);
				break;
			case 'A':
				use_apc = 0;
				break;
			case 'R':
				use_normalization = 1;
				break;
			case 'h':
			case '?':
				usage(argv[0], 1);
				break;

			case 0:
				if(msafilename == NULL) {
					msafilename = thisOpt->argument;
				} else if(matfilename == NULL) {
					matfilename = thisOpt->argument;
				} else {
					usage(argv[0], 0);
				}
				break;
			default:
				die("Unknown argument"); 
		}

		free(thisOpt);
	}

	if(msafilename == NULL || matfilename == NULL) {
		usage(argv[0], 0);
	}


	FILE *msafile = fopen(msafilename, "r");
	if( msafile == NULL) {
		printf("Cannot open %s!\n\n", msafilename);
		return 2;
	}

#ifdef JANSSON
	char* metafilename = malloc(2048);
	snprintf(metafilename, 2048, "%s.meta.json", msafilename);
	
	FILE *metafile = fopen(metafilename, "r");
	json_t *meta;
	if(metafile == NULL) {
		// Cannot find .meta.json file - create new empty metadata
		meta = meta_create();
	} else {
		// Load metadata from matfile.meta.json
		meta = meta_read_json(metafile);
		fclose(metafile);
	}

	json_object_set(meta, "method", json_string("ccmpred"));

	json_t *meta_step = meta_add_step(meta, "ccmpred");
	json_object_set(meta_step, "version", json_string(__VERSION));

	json_t *meta_parameters = json_object();
	json_object_set(meta_step, "parameters", meta_parameters);

	json_t *meta_steps = json_array();
	json_object_set(meta_step, "iterations", meta_steps);

	json_t *meta_results = json_object();
	json_object_set(meta_step, "results", meta_results);

#endif

	int ncol, nrow;
	unsigned char* msa = read_msa(msafile, &ncol, &nrow);
	fclose(msafile);

	int nsingle = ncol * (N_ALPHA - 1);
	int nvar = nsingle + ncol * ncol * N_ALPHA * N_ALPHA;
	int nsingle_padded = nsingle + N_ALPHA_PAD - (nsingle % N_ALPHA_PAD);
	int nvar_padded = nsingle_padded + ncol * ncol * N_ALPHA * N_ALPHA_PAD;

#ifdef CURSES
	bool color = detect_colors();
#else
	bool color = false;
#endif

	logo(color);

#ifdef CUDA
	int num_devices, dev_ret;
	struct cudaDeviceProp prop;
	dev_ret = cudaGetDeviceCount(&num_devices);
	if(dev_ret != CUDA_SUCCESS) {
		num_devices = 0;
	}


	if(num_devices == 0) {
		printf("No CUDA devices available, ");
		use_def_gpu = -1;
	} else if (use_def_gpu < -1 || use_def_gpu >= num_devices) {
		printf("Error: %d is not a valid device number. Please choose a number between 0 and %d\n", use_def_gpu, num_devices - 1);
		exit(1);
	} else {
		printf("Found %d CUDA devices, ", num_devices);
	}

	if (use_def_gpu != -1) {
		cudaError_t err = cudaSetDevice(use_def_gpu);
		if(cudaSuccess != err) {
			printf("Error setting device: %d\n", err);
			exit(1);
		}
		cudaGetDeviceProperties(&prop, use_def_gpu);
		printf("using device #%d: %s\n", use_def_gpu, prop.name);

		size_t mem_free, mem_total;
		err = cudaMemGetInfo(&mem_free, &mem_total);
		if(cudaSuccess != err) {
			printf("Error getting memory info: %d\n", err);
			exit(1);
		}

		size_t mem_needed = nrow * ncol * 2 + // MSAs
		                    sizeof(conjugrad_float_t) * nrow * ncol * 2 + // PC, PCS
		                    sizeof(conjugrad_float_t) * nrow * ncol * N_ALPHA_PAD + // PCN
		                    sizeof(conjugrad_float_t) * nrow + // Weights
		                    (sizeof(conjugrad_float_t) * ((N_ALPHA - 1) * ncol + ncol * ncol * N_ALPHA * N_ALPHA_PAD)) * 4;

		setlocale(LC_NUMERIC, "");
		printf("Total GPU RAM:  %'17lu\n", mem_total);
		printf("Free GPU RAM:   %'17lu\n", mem_free);
		printf("Needed GPU RAM: %'17lu ", mem_needed);

		if(mem_needed <= mem_free) {
			printf("✓\n");
		} else {
			printf("⚠\n");
		}

#ifdef JANSSON
		json_object_set(meta_parameters, "device", json_string("gpu"));
		json_t* meta_gpu = json_object();
		json_object_set(meta_parameters, "gpu_info", meta_gpu);

		json_object_set(meta_gpu, "name", json_string(prop.name));
		json_object_set(meta_gpu, "mem_total", json_integer(mem_total));
		json_object_set(meta_gpu, "mem_free", json_integer(mem_free));
		json_object_set(meta_gpu, "mem_needed", json_integer(mem_needed));
#endif


	} else {
		printf("using CPU");
#ifdef JANSSON
		json_object_set(meta_parameters, "device", json_string("cpu"));
#endif

#ifdef OPENMP
		printf(" (%d thread(s))", numthreads);
#ifdef JANSSON
		json_object_set(meta_parameters, "cpu_threads", json_integer(numthreads));
#endif
#endif
		printf("\n");

	}
#else // CUDA
	printf("using CPU");
#ifdef JANSSON
	json_object_set(meta_parameters, "device", json_string("cpu"));
#endif
#ifdef OPENMP
	printf(" (%d thread(s))\n", numthreads);
#ifdef JANSSON
	json_object_set(meta_parameters, "cpu_threads", json_integer(numthreads));
#endif
#endif // OPENMP
	printf("\n");
#endif // CUDA

	conjugrad_float_t *x = conjugrad_malloc(nvar_padded);
	if( x == NULL) {
		die("ERROR: Not enough memory to allocate variables!");
	}
	memset(x, 0, sizeof(conjugrad_float_t) * nvar_padded);

	// Auto-set lambda_pair
	if(isnan(lambda_pair)) {
		lambda_pair = lambda_pair_factor * (ncol - 1);
	}

	// fill up user data struct for passing to evaluate
	userdata *ud = (userdata *)malloc( sizeof(userdata) );
	if(ud == 0) { die("Cannot allocate memory for user data!"); }
	ud->msa = msa;
	ud->ncol = ncol;
	ud->nrow = nrow;
	ud->nsingle = nsingle;
	ud->nvar = nvar;
	ud->lambda_single = lambda_single;
	ud->lambda_pair = lambda_pair;
	ud->weights = conjugrad_malloc(nrow);
	ud->reweighting_threshold = reweighting_threshold;

	if(initfilename == NULL) {
		// Initialize emissions to pwm
		init_bias(x, ud);
	} else {
		// Load potentials from file
		read_raw(initfilename, ud, x);
	}

	// optimize with default parameters
	conjugrad_parameter_t *param = conjugrad_init();

	param->max_iterations = numiter;
	param->epsilon = conjugrad_eps;
	param->k = conjugrad_k;
	param->max_linesearch = 5;
	param->alpha_mul = F05;
	param->ftol = 1e-4;
	param->wolfe = F02;


	int (*init)(void *) = init_cpu;
	int (*destroy)(void *) = destroy_cpu;
	conjugrad_evaluate_t evaluate = evaluate_cpu;

#ifdef OPENMP
	omp_set_num_threads(numthreads);
	if(numthreads > 1) {
		init = init_cpu_omp;
		destroy = destroy_cpu_omp;
		evaluate = evaluate_cpu_omp;
	}
#endif

#ifdef CUDA
	if(use_def_gpu != -1) {
		init = init_cuda;
		destroy = destroy_cuda;
		evaluate = evaluate_cuda;
	}
#endif

	init(ud);

#ifdef JANSSON


	json_object_set(meta_parameters, "reweighting_threshold", json_real(ud->reweighting_threshold));
	json_object_set(meta_parameters, "apc", json_boolean(use_apc));
	json_object_set(meta_parameters, "normalization", json_boolean(use_normalization));

	json_t *meta_regularization = json_object();
	json_object_set(meta_parameters, "regularization", meta_regularization);

	json_object_set(meta_regularization, "type", json_string("l2")); 
	json_object_set(meta_regularization, "lambda_single", json_real(lambda_single));
	json_object_set(meta_regularization, "lambda_pair", json_real(lambda_pair));
	json_object_set(meta_regularization, "lambda_pair_factor", json_real(lambda_pair_factor));

	json_t *meta_opt = json_object();
	json_object_set(meta_parameters, "optimization", meta_opt);

	json_object_set(meta_opt, "method", json_string("libconjugrad"));
	json_object_set(meta_opt, "float_bits", json_integer((int)sizeof(conjugrad_float_t) * 8));
	json_object_set(meta_opt, "max_iterations", json_integer(param->max_iterations));
	json_object_set(meta_opt, "max_linesearch", json_integer(param->max_linesearch));
	json_object_set(meta_opt, "alpha_mul", json_real(param->alpha_mul));
	json_object_set(meta_opt, "ftol", json_real(param->ftol));
	json_object_set(meta_opt, "wolfe", json_real(param->wolfe));


	json_t *meta_msafile = meta_file_from_path(msafilename);
	json_object_set(meta_parameters, "msafile", meta_msafile);
	json_object_set(meta_msafile, "ncol", json_integer(ncol));
	json_object_set(meta_msafile, "nrow", json_integer(nrow));

	if(initfilename != NULL) {
		json_t *meta_initfile = meta_file_from_path(initfilename);
		json_object_set(meta_parameters, "initfile", meta_initfile);
		json_object_set(meta_initfile, "ncol", json_integer(ncol));
		json_object_set(meta_initfile, "nrow", json_integer(nrow));
	}

	double neff = 0;
	for(int i = 0; i < nrow; i++) {
		neff += ud->weights[i];
	}

	json_object_set(meta_msafile, "neff", json_real(neff));

	ud->meta_steps = meta_steps;

#endif

	printf("\nWill optimize %d %ld-bit variables\n\n", nvar, sizeof(conjugrad_float_t) * 8);

	if(color) { printf("\x1b[1m"); }
	printf("iter\teval\tf(x)    \t║x║     \t║g║     \tstep\n");
	if(color) { printf("\x1b[0m"); }


	conjugrad_float_t fx;
	int ret;
#ifdef CUDA
	if(use_def_gpu != -1) {
		conjugrad_float_t *d_x;
		cudaError_t err = cudaMalloc((void **) &d_x, sizeof(conjugrad_float_t) * nvar_padded);
		if (cudaSuccess != err) {
			printf("CUDA error No. %d while allocation memory for d_x\n", err);
			exit(1);
		}
		err = cudaMemcpy(d_x, x, sizeof(conjugrad_float_t) * nvar_padded, cudaMemcpyHostToDevice);
		if (cudaSuccess != err) {
			printf("CUDA error No. %d while copying parameters to GPU\n", err);
			exit(1);
		}
		ret = conjugrad_gpu(nvar_padded, d_x, &fx, evaluate, progress, ud, param);
		err = cudaMemcpy(x, d_x, sizeof(conjugrad_float_t) * nvar_padded, cudaMemcpyDeviceToHost);
		if (cudaSuccess != err) {
			printf("CUDA error No. %d while copying parameters back to CPU\n", err);
			exit(1);
		}
		err = cudaFree(d_x);
		if (cudaSuccess != err) {
			printf("CUDA error No. %d while freeing memory for d_x\n", err);
			exit(1);
		}
	} else {
	ret = conjugrad(nvar_padded, x, &fx, evaluate, progress, ud, param);
	}
#else
	ret = conjugrad(nvar_padded, x, &fx, evaluate, progress, ud, param);
#endif

	printf("\n");
	printf("%s with status code %d - ", (ret < 0 ? "Exit" : "Done"), ret);

	if(ret == CONJUGRAD_SUCCESS) {
		printf("Success!\n");
	} else if(ret == CONJUGRAD_ALREADY_MINIMIZED) {
		printf("Already minimized!\n");
	} else if(ret == CONJUGRADERR_MAXIMUMITERATION) {
		printf("Maximum number of iterations reached.\n");
	} else {
		printf("Unknown status code!\n");
	}

	printf("\nFinal fx = %f\n\n", fx);

	FILE* out = fopen(matfilename, "w");
	if(out == NULL) {
		printf("Cannot open %s for writing!\n\n", matfilename);
		return 3;
	}

	conjugrad_float_t *outmat = conjugrad_malloc(ncol * ncol);

	FILE *rawfile = NULL;
	if(rawfilename != NULL) {
		printf("Writing raw output to %s\n", rawfilename);
		rawfile = fopen(rawfilename, "w");

		if(rawfile == NULL) {
			printf("Cannot open %s for writing!\n\n", rawfilename);
			return 4;
		}

		write_raw(rawfile, x, ncol);
	}

#ifdef MSGPACK

	FILE *msgpackfile = NULL;
	if(msgpackfilename != NULL) {
		printf("Writing msgpack raw output to %s\n", msgpackfilename);
		msgpackfile = fopen(msgpackfilename, "w");

		if(msgpackfile == NULL) {
			printf("Cannot open %s for writing!\n\n", msgpackfilename);
			return 4;
		}

#ifndef JANSSON
		void *meta = NULL;
#endif

	}
#endif

	sum_submatrices(x, outmat, ncol);

	if(use_apc) {
		apc(outmat, ncol);
	}

	if(use_normalization) {
		normalize(outmat, ncol);
	}

	write_matrix(out, outmat, ncol, ncol);

#ifdef JANSSON

	json_object_set(meta_results, "fx_final", json_real(fx));
	json_object_set(meta_results, "num_iterations", json_integer(json_array_size(meta_steps)));
	json_object_set(meta_results, "opt_code", json_integer(ret));

	json_t *meta_matfile = meta_file_from_path(matfilename);
	json_object_set(meta_results, "matfile", meta_matfile);

	if(rawfilename != NULL) {
		json_object_set(meta_results, "rawfile", meta_file_from_path(rawfilename));
	}

	if(msgpackfilename != NULL) {
		json_object_set(meta_results, "msgpackfile", meta_file_from_path(msgpackfilename));
	}

	fprintf(out, "#>META> %s", json_dumps(meta, JSON_COMPACT));
	if(rawfile != NULL) {
		fprintf(rawfile, "#>META> %s", json_dumps(meta, JSON_COMPACT));
	}
#endif


	if(rawfile != NULL) {
		fclose(rawfile);
	}

#ifdef MSGPACK
	if(msgpackfile != NULL) {
		write_raw_msgpack(msgpackfile, x, ncol, meta);
		fclose(msgpackfile);
	}

#endif

	fflush(out);
	fclose(out);

	destroy(ud);

	conjugrad_free(outmat);
	conjugrad_free(x);
	conjugrad_free(ud->weights);
	free(ud);
	free(msa);
	free(param);
	
	printf("Output can be found in %s\n", matfilename);

	return 0;
}