#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>

#include <qbt/parser.h>
#include <qbt/opt.h>
#include <qbt/asm.h>
#include <qbt/debug.h>

static char *read_file(const char *file, FILE *f)
{
	fseek(f, 0, SEEK_END);
	/** @todo check how well standardized this actually is */
	long s = ftell(f);
	if (s == LONG_MAX) {
		error("%s might be a directory", file);
		return NULL;
	}

	fseek(f, 0, SEEK_SET);

	char *buf = malloc(s + 1);
	if (!buf)
		return NULL;

	fread(buf, s + 1, 1, f);
	/* remember terminating null */
	buf[s] = 0;
	return buf;
}

static void usage(FILE *f)
{
	fprintf(f, "qbt <file>.qbt\n");
}

int main(int argc, char *argv[])
{
	if (argc != 2) {
		usage(stdout);
		return -1;
	}

	char *fname = argv[1];
	FILE *f = fopen(fname, "rb");
	if (!f) {
		fprintf(stderr, "couldn't open %s\n", fname);
		return -1;
	}

	char *buf = read_file(fname, f);
	fclose(f);

	struct parser *p = create_parser();
	parse(p, fname, buf);

	int ret = 0;
	if (p->failed) {
		ret = -1;
		goto out;
	}

	foreach_fn(i, p->fns) {
		struct fn_map m = fn_at(p->fns, i);
		// also handles things like register mapping etc.
		optimize(m.fn);
		// for now
		output(m.fn, stdout);
	}

out:
	destroy_parser(p);
	free(buf);
	return ret;
}
