コード例 #1
0
ファイル: include_file.c プロジェクト: vhelin/wla-dx
int include_file(char *name) {

    static int first_load = 0;
    int file_size, id;
    char *tmp_b, *n, *tmp_c;
    FILE *f;


    /* create the full output file name */
    if (use_incdir == YES)
        tmp_c = ext_incdir;
    else
        tmp_c = include_dir;

    if (create_full_name(tmp_c, name) == FAILED)
        return FAILED;

    f = fopen(full_name, "rb");
    id = 0;

    if (f == NULL && (tmp_c == NULL || tmp_c[0] == 0)) {
        sprintf(emsg, "Error opening file \"%s\".\n", name);
        if (first_load == 0)
            fprintf(stderr, "INCLUDE_FILE: %s", emsg);
        else
            print_error(emsg, ERROR_INC);
        return FAILED;
    }

    /* if not found in ext_incdir silently try the include directory */
    if (f == NULL && use_incdir == YES) {
        if (create_full_name(include_dir, name) == FAILED)
            return FAILED;

        f = fopen(full_name, "rb");
        id = 0;

        if (f == NULL && (include_dir == NULL || include_dir[0] == 0)) {
            sprintf(emsg, "Error opening file \"%s\".\n", name);
            if (first_load == 0)
                fprintf(stderr, "INCLUDE_FILE: %s", emsg);
            else
                print_error(emsg, ERROR_INC);
            return FAILED;
        }
    }

    /* if failed try to find the file in the current directory */
    if (f == NULL) {
        fprintf(stderr, "%s:%d: ", get_file_name(active_file_info_last->filename_id), active_file_info_last->line_current);
        fprintf(stderr, "INCLUDE_FILE: Could not open \"%s\", trying \"%s\"... ", full_name, name);
        f = fopen(name, "rb");
        id = 1;
    }

    if (f == NULL) {
        fprintf(stderr, "not found.\n");
        sprintf(emsg, "Error opening file \"%s\".\n", full_name);
        if (first_load == 0)
            fprintf(stderr, "INCLUDE_FILE: %s", emsg);
        else
            print_error(emsg, ERROR_INC);
        return FAILED;
    }

    if (id == 1) {
        fprintf(stderr, "found.\n");
        strcpy(full_name, name);
    }

    first_load = 1;

    if (extra_definitions == ON) {
        redefine("WLA_FILENAME", 0.0, name, DEFINITION_TYPE_STRING, strlen(name));
        redefine("wla_filename", 0.0, name, DEFINITION_TYPE_STRING, strlen(name));
    }

    fseek(f, 0, SEEK_END);
    file_size = ftell(f);
    fseek(f, 0, SEEK_SET);

    active_file_info_tmp = malloc(sizeof(struct active_file_info));
    if (active_file_info_tmp == NULL) {
        sprintf(emsg, "Out of memory while trying allocate error tracking data structure for file \"%s\".\n", full_name);
        print_error(emsg, ERROR_INC);
        return FAILED;
    }
    active_file_info_tmp->next = NULL;

    if (active_file_info_first == NULL) {
        active_file_info_first = active_file_info_tmp;
        active_file_info_last = active_file_info_tmp;
        active_file_info_tmp->prev = NULL;
    }
    else {
        active_file_info_tmp->prev = active_file_info_last;
        active_file_info_last->next = active_file_info_tmp;
        active_file_info_last = active_file_info_tmp;
    }

    active_file_info_tmp->line_current = 1;

    /* name */
    file_name_info_tmp = file_name_info_first;
    id = 0;
    while (file_name_info_tmp != NULL) {
        if (strcmp(file_name_info_tmp->name, full_name) == 0) {
            id = file_name_info_tmp->id;
            active_file_info_tmp->filename_id = id;
            break;
        }
        file_name_info_tmp = file_name_info_tmp->next;
    }

    if (id == 0) {
        file_name_info_tmp = malloc(sizeof(struct file_name_info));
        n = malloc(strlen(full_name)+1);
        if (file_name_info_tmp == NULL || n == NULL) {
            if (file_name_info_tmp != NULL)
                free(file_name_info_tmp);
            if (n != NULL)
                free(n);
            sprintf(emsg, "Out of memory while trying allocate info structure for file \"%s\".\n", full_name);
            print_error(emsg, ERROR_INC);
            return FAILED;
        }
        file_name_info_tmp->next = NULL;

        if (file_name_info_first == NULL) {
            file_name_info_first = file_name_info_tmp;
            file_name_info_last = file_name_info_tmp;
        }
        else {
            file_name_info_last->next = file_name_info_tmp;
            file_name_info_last = file_name_info_tmp;
        }

        strcpy(n, full_name);
        file_name_info_tmp->name = n;
        active_file_info_tmp->filename_id = file_name_id;
        file_name_info_tmp->id = file_name_id;
        file_name_id++;
    }

    /* reallocate buffer */
    if (include_in_tmp_size < file_size) {
        if (include_in_tmp != NULL)
            free(include_in_tmp);

        include_in_tmp = malloc(sizeof(char) * file_size);
        if (include_in_tmp == NULL) {
            sprintf(emsg, "Out of memory while trying to allocate room for \"%s\".\n", full_name);
            print_error(emsg, ERROR_INC);
            return FAILED;
        }

        include_in_tmp_size = file_size;
    }

    /* read the whole file into a buffer */
    fread(include_in_tmp, 1, file_size, f);
    fclose(f);

    if (size == 0) {
        buffer = malloc(sizeof(char) * (file_size + 4));
        if (buffer == NULL) {
            sprintf(emsg, "Out of memory while trying to allocate room for \"%s\".\n", full_name);
            print_error(emsg, ERROR_INC);
            return FAILED;
        }

        /* preprocess */
        preprocess_file(include_in_tmp, include_in_tmp + file_size, buffer, &size, full_name);

        buffer[size++] = 0xA;
        buffer[size++] = '.';
        buffer[size++] = 'E';
        buffer[size++] = ' ';

        open_files++;

        return SUCCEEDED;
    }

    tmp_b = malloc(sizeof(char) * (size + file_size + 4));
    if (tmp_b == NULL) {
        sprintf(emsg, "Out of memory while trying to expand the project to incorporate file \"%s\".\n", full_name);
        print_error(emsg, ERROR_INC);
        return FAILED;
    }

    /* reallocate tmp_a */
    if (tmp_a_size < file_size + 4) {
        if (tmp_a != NULL)
            free(tmp_a);

        tmp_a = malloc(sizeof(char) * (file_size + 4));
        if (tmp_a == NULL) {
            sprintf(emsg, "Out of memory while allocating new room for \"%s\".\n", full_name);
            print_error(emsg, ERROR_INC);
            return FAILED;
        }

        tmp_a_size = file_size + 4;
    }

    /* preprocess */
    inz = 0;
    preprocess_file(include_in_tmp, include_in_tmp + file_size, tmp_a, &inz, full_name);

    tmp_a[inz++] = 0xA;
    tmp_a[inz++] = '.';
    tmp_a[inz++] = 'E';
    tmp_a[inz++] = ' ';

    open_files++;

    memcpy(tmp_b, buffer, i);
    memcpy(tmp_b + i, tmp_a, inz);
    memcpy(tmp_b + i + inz, buffer + i, size - i);

    free(buffer);

    size += inz;
    buffer = tmp_b;

    return SUCCEEDED;
}
コード例 #2
0
ファイル: sys_lxt2.c プロジェクト: funningboy/iverilog
static void scan_item(unsigned depth, vpiHandle item, int skip)
{
      struct t_cb_data cb;
      struct vcd_info* info;

      const char* name;
      const char* ident;
      int nexus_id;

      /* list of types to iterate upon */
      int i;
      static int types[] = {
	    /* Value */
	    vpiNet,
	    vpiReg,
	    vpiVariables,
	    /* Scope */
	    vpiFunction,
	    vpiModule,
	    vpiNamedBegin,
	    vpiNamedFork,
	    vpiTask,
	    -1
      };

      switch (vpi_get(vpiType, item)) {

	  case vpiMemoryWord:
	    if (vpi_get(vpiConstantSelect, item) == 0) {
		    /* Turn a non-constant array word select into a
		     * constant word select. */
		  vpiHandle array = vpi_handle(vpiParent, item);
		  PLI_INT32 idx = vpi_get(vpiIndex, item);
		  item = vpi_handle_by_index(array, idx);
	    }
	  case vpiIntegerVar:
	  case vpiBitVar:
	  case vpiByteVar:
	  case vpiShortIntVar:
	  case vpiIntVar:
	  case vpiLongIntVar:
	  case vpiTimeVar:
	  case vpiReg:
	  case vpiNet:

	      /* An array word is implicitly escaped so look for an
	       * escaped identifier that this could conflict with. */
            if (vpi_get(vpiType, item) == vpiMemoryWord &&
                vpi_handle_by_name(vpi_get_str(vpiFullName, item), 0)) {
		  vpi_printf("LXT2 warning: dumping array word %s will "
		             "conflict with an escaped identifier.\n",
		             vpi_get_str(vpiFullName, item));
            }

            if (skip || vpi_get(vpiAutomatic, item)) break;

	    name = vpi_get_str(vpiName, item);
	    nexus_id = vpi_get(_vpiNexusId, item);
	    if (nexus_id) {
		  ident = find_nexus_ident(nexus_id);
	    } else {
		  ident = 0;
	    }

	    if (!ident) {
		  char*tmp = create_full_name(name);
		  ident = strdup_sh(&name_heap, tmp);
		  free(tmp);

		  if (nexus_id) set_nexus_ident(nexus_id, ident);

		  info = new_vcd_info();

		  info->item  = item;
		  info->sym   = lxt2_wr_symbol_add(dump_file, ident,
		                                   0 /* array rows */,
		                                   vpi_get(vpiLeftRange, item),
		                                   vpi_get(vpiRightRange, item),
		                                   LXT2_WR_SYM_F_BITS);
		  info->dmp_next = 0;

		  cb.time      = 0;
		  cb.user_data = (char*)info;
		  cb.value     = NULL;
		  cb.obj       = item;
		  cb.reason    = cbValueChange;
		  cb.cb_rtn    = variable_cb_1;

		  info->cb    = vpi_register_cb(&cb);

	    } else {
		  char *n = create_full_name(name);
		  lxt2_wr_symbol_alias(dump_file, ident, n,
				       vpi_get(vpiSize, item)-1, 0);
		  free(n);
            }

	    break;

	  case vpiRealVar:

            if (skip || vpi_get(vpiAutomatic, item)) break;

	    name = vpi_get_str(vpiName, item);
	    { char*tmp = create_full_name(name);
	      ident = strdup_sh(&name_heap, tmp);
	      free(tmp);
	    }
	    info = new_vcd_info();

	    info->item = item;
	    info->sym  = lxt2_wr_symbol_add(dump_file, ident,
	                                    0 /* array rows */,
	                                    vpi_get(vpiSize, item)-1,
	                                    0, LXT2_WR_SYM_F_DOUBLE);
	    info->dmp_next = 0;

	    cb.time      = 0;
	    cb.user_data = (char*)info;
	    cb.value     = NULL;
	    cb.obj       = item;
	    cb.reason    = cbValueChange;
	    cb.cb_rtn    = variable_cb_1;

	    info->cb    = vpi_register_cb(&cb);

	    break;

	  case vpiModule:
	  case vpiNamedBegin:
	  case vpiTask:
	  case vpiFunction:
	  case vpiNamedFork:

	    if (depth > 0) {
		  int nskip;
		  vpiHandle argv;

		  const char* fullname =
			vpi_get_str(vpiFullName, item);

#if 0
		  vpi_printf("LXT2 info: scanning scope %s, %u levels\n",
		             fullname, depth);
#endif
		  nskip = vcd_scope_names_test(fullname);

		  if (!nskip)
			vcd_scope_names_add(fullname);
		  else
		    vpi_printf("LXT2 warning: ignoring signals in "
		               "previously scanned scope %s\n", fullname);

		  name = vpi_get_str(vpiName, item);

                  push_scope(name);

		  for (i=0; types[i]>0; i++) {
			vpiHandle hand;
			argv = vpi_iterate(types[i], item);
			while (argv && (hand = vpi_scan(argv))) {
			      scan_item(depth-1, hand, nskip);
			}
		  }

                  pop_scope();
	    }
	    break;

	  default:
	    vpi_printf("LXT2 warning: $dumpvars: Unsupported parameter "
	               "type (%s)\n", vpi_get_str(vpiType, item));
      }

}
コード例 #3
0
ファイル: include_file.c プロジェクト: vhelin/wla-dx
int incbin_file(char *name, int *id, int *swap, int *skip, int *read, struct macro_static **macro) {

    struct incbin_file_data *ifd;
    char *in_tmp, *n, *tmp_c;
    int file_size, q;
    FILE *f;


    /* create the full output file name */
    if (use_incdir == YES)
        tmp_c = ext_incdir;
    else
        tmp_c = include_dir;

    if (create_full_name(tmp_c, name) == FAILED)
        return FAILED;

    f = fopen(full_name, "rb");
    q = 0;

    if (f == NULL && (tmp_c == NULL || tmp_c[0] == 0)) {
        sprintf(emsg, "Error opening file \"%s\".\n", name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* if not found in ext_incdir silently try the include directory */
    if (f == NULL && use_incdir == YES) {
        if (create_full_name(include_dir, name) == FAILED)
            return FAILED;

        f = fopen(full_name, "rb");
        q = 0;

        if (f == NULL && (include_dir == NULL || include_dir[0] == 0)) {
            sprintf(emsg, "Error opening file \"%s\".\n", name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* if failed try to find the file in the current directory */
    if (f == NULL) {
        fprintf(stderr, "%s:%d: ", get_file_name(active_file_info_last->filename_id), active_file_info_last->line_current);
        fprintf(stderr, "INCBIN_FILE: Could not open \"%s\", trying \"%s\"... ", full_name, name);
        f = fopen(name, "rb");
        q = 1;
    }

    if (f == NULL) {
        fprintf(stderr, "not found.\n");
        sprintf(emsg, "Error opening file \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    if (q == 1) {
        fprintf(stderr, "found.\n");
        strcpy(full_name, name);
    }

    fseek(f, 0, SEEK_END);
    file_size = ftell(f);
    fseek(f, 0, SEEK_SET);

    ifd = (struct incbin_file_data *)malloc(sizeof(struct incbin_file_data));
    n = malloc(sizeof(char) * (strlen(full_name)+1));
    in_tmp = (char *)malloc(sizeof(char) * file_size);
    if (ifd == NULL || n == NULL || in_tmp == NULL) {
        if (ifd != NULL)
            free(ifd);
        if (n != NULL)
            free(n);
        if (in_tmp != NULL)
            free(in_tmp);
        sprintf(emsg, "Out of memory while allocating data structure for \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* read the whole file into a buffer */
    fread(in_tmp, 1, file_size, f);
    fclose(f);

    /* complete the structure */
    ifd->next = NULL;
    ifd->size = file_size;
    strcpy(n, full_name);
    ifd->name = n;
    ifd->data = in_tmp;

    /* find the index */
    q = 0;
    if (incbin_file_data_first != NULL) {
        ifd_tmp = incbin_file_data_first;
        while (ifd_tmp->next != NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp = ifd_tmp->next;
            q++;
        }
        if (ifd_tmp->next == NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp->next = ifd;
            q++;
        }
    }
    else
        incbin_file_data_first = ifd;

    *id = q;

    /* SKIP bytes? */
    if (compare_next_token("SKIP", 4) == FAILED)
        *skip = 0;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of skipped bytes.\n", ERROR_DIR);
            return FAILED;
        }

        *skip = d;

        if (d >= file_size) {
            sprintf(emsg, "SKIP value (%d) is more than the size (%d) of file \"%s\".\n", d, file_size, full_name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* READ bytes? */
    if (compare_next_token("READ", 4) == FAILED)
        *read = file_size - *skip;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of bytes for reading.\n", ERROR_DIR);
            return FAILED;
        }

        *read = d;

        if (*skip + *read > file_size) {
            sprintf(emsg, "Overreading file \"%s\" by %d bytes.\n", full_name, *skip + *read - file_size);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* SWAP bytes? */
    if (compare_next_token("SWAP", 4) == FAILED)
        *swap = 0;
    else {
        if ((*read & 1) == 1) {
            sprintf(emsg, "The read size of file \"%s\" is odd (%d)! Cannot perform SWAP.\n", full_name, *read);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
        *swap = 1;
        skip_next_token();
    }

    /* FSIZE? */
    if (compare_next_token("FSIZE", 5) == SUCCEEDED) {
        skip_next_token();

        /* get the definition label */
        if (get_next_token() == FAILED)
            return FAILED;

        add_a_new_definition(tmp, (double)file_size, NULL, DEFINITION_TYPE_VALUE, 0);
    }

    /* FILTER? */
    if (compare_next_token("FILTER", 6) == SUCCEEDED) {
        skip_next_token();

        /* get the filter macro name */
        if (get_next_token() == FAILED)
            return FAILED;

        *macro = macro_get(tmp);

        if (*macro == NULL) {
            sprintf(emsg, "No MACRO \"%s\" defined.\n", tmp);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }
    else
        *macro = NULL;

    return SUCCEEDED;
}