Example #1
0
/*
 * returns the string representation of the variable
 */
char *v_str(var_t *arg) {
  char *buffer;
  switch (arg->type) {
  case V_INT:
    buffer = malloc(64);
    ltostr(arg->v.i, buffer);
    break;
  case V_NUM:
    buffer = malloc(64);
    ftostr(arg->v.n, buffer);
    break;
  case V_STR:
    buffer = strdup(arg->v.p.ptr);
    break;
  case V_ARRAY:
  case V_MAP:
    buffer = map_to_str(arg);
    break;
  case V_FUNC:
  case V_PTR:
    buffer = malloc(5);
    strcpy(buffer, "func");
    break;
  default:
    buffer = malloc(1);
    buffer[0] = '\0';
    break;
  }
  return buffer;
}
Example #2
0
static SIM_RC
memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
		       char *arg, int is_command)
{
  switch (opt)
    {

    case OPTION_MEMORY_DELETE:
      if (strcasecmp (arg, "all") == 0)
	{
	  while (STATE_MEMOPT (sd) != NULL)
	    do_memopt_delete (sd,
			      STATE_MEMOPT (sd)->level,
			      STATE_MEMOPT (sd)->space,
			      STATE_MEMOPT (sd)->addr);
	  return SIM_RC_OK;
	}
      else
	{
	  int level = 0;
	  int space = 0;
	  address_word addr = 0;
	  parse_addr (arg, &level, &space, &addr);
	  return do_memopt_delete (sd, level, space, addr);
	}

    case OPTION_MEMORY_REGION:
      {
	char *chp = arg;
	int level = 0;
	int space = 0;
	address_word addr = 0;
	address_word nr_bytes = 0;
	unsigned modulo = 0;
	/* parse the arguments */
	chp = parse_addr (chp, &level, &space, &addr);
	if (*chp != ',')
	  {
	    /* let the file autosize */
	    if (mmap_next_fd == -1)
	      {
		sim_io_eprintf (sd, "Missing size for memory-region\n");
		return SIM_RC_FAIL;
	      }
	  }
	else
	  chp = parse_size (chp + 1, &nr_bytes, &modulo);
	/* old style */
	if (*chp == ',')
	  modulo = strtoul (chp + 1, &chp, 0);
	/* try to attach/insert it */
	do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
		       &STATE_MEMOPT (sd), NULL);
	return SIM_RC_OK;
      }

    case OPTION_MEMORY_ALIAS:
      {
	char *chp = arg;
	int level = 0;
	int space = 0;
	address_word addr = 0;
	address_word nr_bytes = 0;
	unsigned modulo = 0;
	sim_memopt *entry;
	/* parse the arguments */
	chp = parse_addr (chp, &level, &space, &addr);
	if (*chp != ',')
	  {
	    sim_io_eprintf (sd, "Missing size for memory-region\n");
	    return SIM_RC_FAIL;
	  }
	chp = parse_size (chp + 1, &nr_bytes, &modulo);
	/* try to attach/insert the main record */
	entry = do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
			       &STATE_MEMOPT (sd),
			       NULL);
	/* now attach all the aliases */
	while (*chp == ',')
	  {
	    int a_level = level;
	    int a_space = space;
	    address_word a_addr = addr;
	    chp = parse_addr (chp + 1, &a_level, &a_space, &a_addr);
	    do_memopt_add (sd, a_level, a_space, a_addr, nr_bytes, modulo,
			   &entry->alias, entry->buffer);
	  }
	return SIM_RC_OK;
      }

    case OPTION_MEMORY_SIZE:
      {
	int level = 0;
	int space = 0;
	address_word addr = 0;
	address_word nr_bytes = 0;
	unsigned modulo = 0;
	/* parse the arguments */
	parse_size (arg, &nr_bytes, &modulo);
	/* try to attach/insert it */
	do_memopt_add (sd, level, space, addr, nr_bytes, modulo,
		       &STATE_MEMOPT (sd), NULL);
	return SIM_RC_OK;
      }

    case OPTION_MEMORY_CLEAR:
      {
	fill_byte_value = (unsigned8) 0;
	fill_byte_flag = 1;
	return SIM_RC_OK;
	break;
      }

    case OPTION_MEMORY_FILL:
      {
	unsigned long fill_value;
	parse_ulong_value (arg, &fill_value);
	if (fill_value > 255)
	  {
	    sim_io_eprintf (sd, "Missing fill value between 0 and 255\n");
	    return SIM_RC_FAIL;
	  }
	fill_byte_value = (unsigned8) fill_value;
	fill_byte_flag = 1;
	return SIM_RC_OK;
	break;
      }

    case OPTION_MEMORY_MAPFILE:
      {
	if (mmap_next_fd >= 0)
	  {
	    sim_io_eprintf (sd, "Duplicate memory-mapfile option\n");
	    return SIM_RC_FAIL;
	  }

	mmap_next_fd = open (arg, O_RDWR);
	if (mmap_next_fd < 0)
	  {
	    sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
			    arg, strerror (errno));
	    return SIM_RC_FAIL;
	  }

	return SIM_RC_OK;
      }

    case OPTION_MEMORY_INFO:
      {
	sim_memopt *entry;
	sim_io_printf (sd, "Memory maps:\n");
	for (entry = STATE_MEMOPT (sd); entry != NULL; entry = entry->next)
	  {
	    sim_memopt *alias;
	    sim_io_printf (sd, " memory");
	    if (entry->alias == NULL)
	      sim_io_printf (sd, " region ");
	    else
	      sim_io_printf (sd, " alias ");
	    if (entry->space != 0)
	      sim_io_printf (sd, "0x%lx:", (long) entry->space);
	    sim_io_printf (sd, "0x%08lx", (long) entry->addr);
	    if (entry->level != 0)
	      sim_io_printf (sd, "@0x%lx", (long) entry->level);
	    sim_io_printf (sd, ",0x%lx",
			   (long) entry->nr_bytes);
	    if (entry->modulo != 0)
	      sim_io_printf (sd, "%%0x%lx", (long) entry->modulo);
	    for (alias = entry->alias;
		 alias != NULL;
		 alias = alias->next)
	      {
		if (alias->space != 0)
		  sim_io_printf (sd, "0x%lx:", (long) alias->space);
		sim_io_printf (sd, ",0x%08lx", (long) alias->addr);
		if (alias->level != 0)
		  sim_io_printf (sd, "@0x%lx", (long) alias->level);
	      }
	    sim_io_printf (sd, "\n");
	  }
	return SIM_RC_OK;
	break;
      }

    case OPTION_MAP_INFO:
      {
	sim_core *memory = STATE_CORE (sd);
	unsigned nr_map;

	for (nr_map = 0; nr_map < nr_maps; ++nr_map)
	  {
	    sim_core_map *map = &memory->common.map[nr_map];
	    sim_core_mapping *mapping = map->first;

	    if (!mapping)
	      continue;

	    sim_io_printf (sd, "%s maps:\n", map_to_str (nr_map));
	    do
	      {
		unsigned modulo;

		sim_io_printf (sd, " map ");
		if (mapping->space != 0)
		  sim_io_printf (sd, "0x%x:", mapping->space);
		sim_io_printf (sd, "0x%08lx", (long) mapping->base);
		if (mapping->level != 0)
		  sim_io_printf (sd, "@0x%x", mapping->level);
		sim_io_printf (sd, ",0x%lx", (long) mapping->nr_bytes);
		modulo = mapping->mask + 1;
		if (modulo != 0)
		  sim_io_printf (sd, "%%0x%x", modulo);
		sim_io_printf (sd, "\n");

		mapping = mapping->next;
	      }
	    while (mapping);
	  }

	return SIM_RC_OK;
	break;
      }

    default:
      sim_io_eprintf (sd, "Unknown memory option %d\n", opt);
      return SIM_RC_FAIL;

    }

  return SIM_RC_FAIL;
}
Example #3
0
/*
 * add two variables
 * result = a + b
 */
void v_add(var_t *result, var_t *a, var_t *b) {
  char tmpsb[INT_STR_LEN];

  if (a->type == V_STR && b->type == V_STR) {
    int length = strlen(a->v.p.ptr) + strlen(b->v.p.ptr);
    result->type = V_STR;
    result->v.p.ptr = malloc(length + 1);
    strcpy(result->v.p.ptr, a->v.p.ptr);
    strcat(result->v.p.ptr, b->v.p.ptr);
    result->v.p.ptr[length] = '\0';
    result->v.p.length = length + 1;
    return;
  } else if (a->type == V_INT && b->type == V_INT) {
    result->type = V_INT;
    result->v.i = a->v.i + b->v.i;
    return;
  } else if (a->type == V_NUM && b->type == V_NUM) {
    result->type = V_NUM;
    result->v.n = a->v.n + b->v.n;
    return;
  } else if (a->type == V_NUM && b->type == V_INT) {
    result->type = V_NUM;
    result->v.n = a->v.n + b->v.i;
    return;
  } else if (a->type == V_INT && b->type == V_NUM) {
    result->type = V_NUM;
    result->v.n = a->v.i + b->v.n;
    return;
  } else if (a->type == V_STR && (b->type == V_INT || b->type == V_NUM)) {
    if (is_number(a->v.p.ptr)) {
      result->type = V_NUM;
      if (b->type == V_INT) {
        result->v.n = b->v.i + v_getval(a);
      } else {
        result->v.n = b->v.n + v_getval(a);
      }
    } else {
      result->type = V_STR;
      result->v.p.ptr = (char *)malloc(strlen(a->v.p.ptr) + INT_STR_LEN);
      strcpy(result->v.p.ptr, a->v.p.ptr);
      if (b->type == V_INT) {
        ltostr(b->v.i, tmpsb);
      } else {
        ftostr(b->v.n, tmpsb);
      }
      strcat(result->v.p.ptr, tmpsb);
      result->v.p.length = strlen(result->v.p.ptr) + 1;
    }
  } else if ((a->type == V_INT || a->type == V_NUM) && b->type == V_STR) {
    if (is_number(b->v.p.ptr)) {
      result->type = V_NUM;
      if (a->type == V_INT) {
        result->v.n = a->v.i + v_getval(b);
      } else {
        result->v.n = a->v.n + v_getval(b);
      }
    } else {
      result->type = V_STR;
      result->v.p.ptr = (char *)malloc(strlen(b->v.p.ptr) + INT_STR_LEN);
      if (a->type == V_INT) {
        ltostr(a->v.i, tmpsb);
      } else {
        ftostr(a->v.n, tmpsb);
      }
      strcpy(result->v.p.ptr, tmpsb);
      strcat(result->v.p.ptr, b->v.p.ptr);
      result->v.p.length = strlen(result->v.p.ptr) + 1;
    }
  } else if (b->type == V_MAP) {
    char *map = map_to_str(b);
    v_set(result, a);
    v_strcat(result, map);
    free(map);
  }
}