Example #1
0
int main(int argc, char *argv[])
{
    FILE *f = fopen(argv[1], "r");
    if ( f!=NULL ) {
        VM *vm = vm_load(f);
        vm_exec(vm, false);
    }
    return 0;
}
Example #2
0
int main(int argc, char *argv[]) {
	cunit_setup = setup;
	cunit_teardown = teardown;
	char samplesdir[2000];
	char samplesfile[2000];

	char* wichruntime = getenv("WICHRUNTIME"); // set in intellij "test_vm_samples" environment variable config area
	if ( wichruntime==NULL ) {
		fprintf(stderr, "environment variable WICHRUNTIME not set to root of runtime area\n");
		return -1;
	}
	strcpy(samplesdir, wichruntime);
	strcat(samplesdir, "/vm/test/samples");

	struct dirent *dp;
	DIR *dir = opendir(samplesdir);
	if ( dir!=NULL ) {
		dp = readdir(dir);
		while (dp != NULL) {
			char *filename = dp->d_name;
			if ( strstr(filename, ".wasm")!=NULL ) {
				printf("loading %s\n", filename);
				strcpy(samplesfile, samplesdir);
				strcat(samplesfile, "/");
				strcat(samplesfile, filename);
				FILE *f = fopen(samplesfile, "r");
				VM *vm = vm_load(f);
				vm_exec(vm, false);
				fclose(f);
			}
			dp = readdir(dir);
		}
		closedir(dir);
	}
	else {
		fprintf(stderr, "can't find samples dir\n");
	}
	return 0;
}
Example #3
0
File: vm.c Project: terrayi/vm
int vm_quick_run(VM *vm, byte *code, size_t code_length)
{
	int ret = VM_RET_ERROR;

	vm_init(vm);

	ret = vm_load(vm, code, code_length);

	if (ret != VM_RET_OK)
	{
		return ret;
	}

	ret = vm_run(vm);

	if (ret != VM_RET_OK)
	{
		return ret;
	}

	vm_uninit(vm);

	return VM_RET_OK;
}
Example #4
0
File: kramvm.c Project: kisom/kram
static int
run(const char *filespec)
{
	struct stat	 st;
	int		 i;
	int		 res;
	int		 status = EXIT_FAILURE;
	VM		 vm = NULL;
	FILE		*bin = NULL;
	uint8_t		*prog = NULL;

	if (-1 == stat(filespec, &st))
		goto exit;

	printf("Loading %lu byte program.\n", st.st_size);
	if (st.st_size > memory) {
		fprintf(stderr, "Out of memory (prog=%uB, vm=%uB).\n",
		    (unsigned)st.st_size, memory);
		return EXIT_FAILURE;
	}

	prog = malloc(st.st_size);
	if (NULL == prog)
		goto exit;

	if (NULL == (bin = fopen(filespec, "rb")))
		goto exit;

	if (st.st_size != (off_t)fread(prog, sizeof(uint8_t), st.st_size, bin))
		goto exit;

	vm = vm_new_with(memory, sp, entry);
	if (NULL == vm)
		goto exit;

	printf("Starting VM.\n");
	vm_load(vm, prog, st.st_size);
	for (i = 0; i < 72; i++) printf("-");
	printf("\n");
	res = vm_run(vm);
	for (i = 0; i < 72; i++) printf("-");
	printf("\n");
	if (VM_ERR == res)
		goto exit;

	printf("OK\n");
	status = EXIT_SUCCESS;

exit:
	if (prog)
		free(prog);

	if (vm) {
		if (debug) {
			vm_dump_registers(vm);
		}
		vm_destroy(vm);
	}

	if (bin)
		fclose(bin);
	return status;
}
Example #5
0
bool test(const char* filename, FILE* fout)
{
    FILE* fp = fopen(filename, "r");
    if (fp == NULL) {
        perror(filename);
        return false;
    }

    int fmt;

    int suc = fscanf(fp, "%d", &fmt);
    if (suc == EOF || suc != 1) {
        fclose(fp);
        return false;
    }
    if (fclose(fp) != 0) {
        perror(filename);
    }

    switch (fmt) {
        case FMT_VECTOR: {
            Vector vect = { .count = 0, .items = NULL };
            if (!vector_load(&vect, filename)) {
                return false;
            }

            vector_print(&vect, fout);
            vector_free(&vect);
        }   break;

        case FMT_MATRIX: {
            Matrix mat = { .rows = 0, .cols = 0, .items = NULL };
            if (!matrix_load(&mat, filename)) {
                return false;
            }

            matrix_print(&mat, fout);
            matrix_free(&mat);
        }   break;

        case FMT_VECTOR_OF_MATRICES: {
            VectorOfMatrices vm = { .count = 0, .rows = 0, .cols = 0, .items = NULL };
            if (!vm_load(&vm, filename)) {
                return false;
            }

            vm_print(&vm, fout);
            vm_free(&vm);
        }   break;

        default:
            return false;
    }

    return true;
}

bool vector_load(Vector* vect, const char* filename)
{
    assert(vect != NULL);

    FILE* fp = fopen(filename, "r");
    if (fp == NULL) {
        perror(filename);
        goto return_false;
    }

    int fmt;
    unsigned long count;

    int suc = fscanf(fp, "%d", &fmt);
    CHECK_IO(suc, 1, return_false);
    if (fmt != FMT_VECTOR) {
        goto return_false;
    }

    suc = fscanf(fp, "%lu", &count);
    CHECK_IO(suc, 1, return_false);
    if (count <= 0) {
        goto return_false;
    }
    vect->count = count;
    if (!vector_alloc(vect)) {
        goto return_false;
    }

    int* pVect = vect->items;
    for (size_t i = 0; i < vect->count; ++i) {
        suc = fscanf(fp, "%d", pVect++);
        CHECK_IO(suc, 1, return_false);
    }

    if (fclose(fp) != 0) {
        perror(filename);
    }

    return true;

return_false:
    if (fp != NULL && fclose(fp) != 0) {
        perror(filename);
    }

    vector_free(vect);
    return false;
}

void vector_print(const Vector* vect, FILE* fp)
{
    assert(vect != NULL);
    assert(fp != NULL);
    assert(vect->count > 0);
    assert(vect->items != NULL);

    fprintf(fp, "%d\n", FMT_VECTOR);
    fprintf(fp, "%zu\n", vect->count);

    int* pVect = vect->items;
    for (size_t i = 0; i < vect->count; ++i) {
        fprintf(fp, "%d ", *pVect++);
    }
}

void noop_print(FILE* fp)
{
    assert(fp != NULL);
    fprintf(fp, "false\n");
}