Blame src/driver/amgc_driver_ctx.c

383aa6
/**********************************************************/
383aa6
/*  apimagic: cparser-based API normalization utility     */
383aa6
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
383aa6
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
383aa6
/**********************************************************/
383aa6
383aa6
#include <stdint.h>
383aa6
#include <unistd.h>
383aa6
#include <fcntl.h>
383aa6
f2e43d
#include <cparser/ast/type_t.h>
fd0662
#include <cparser/driver/c_driver.h>
1fd161
#include <cparser/driver/driver.h>
1fd161
#include <cparser/driver/driver_t.h>
43c8c9
#include <cparser/driver/target.h>
1fd161
#include <cparser/driver/tempfile.h>
1fd161
#include <cparser/firm/ast2firm.h>
1fd161
#include <cparser/firm/firm_opt.h>
1fd161
#include <cparser/parser/parser.h>
1fd161
#include <cparser/parser/preprocessor.h>
1fd161
383aa6
#define ARGV_DRIVER
383aa6
383aa6
#include <apimagic/apimagic.h>
383aa6
#include "apimagic_driver_impl.h"
383aa6
#include "argv/argv.h"
383aa6
383aa6
extern const struct argv_option amgc_default_options[];
383aa6
383aa6
struct amgc_driver_ctx_alloc {
383aa6
	struct argv_meta *		meta;
6adb90
	struct amgc_action *		actions;
383aa6
	struct amgc_driver_ctx_impl	ctx;
383aa6
	uint64_t			guard;
383aa6
	const char *			units[];
383aa6
};
383aa6
383aa6
static uint32_t amgc_argv_flags(uint32_t flags)
383aa6
{
383aa6
	uint32_t ret = 0;
383aa6
383aa6
	if (flags & AMGC_DRIVER_VERBOSITY_NONE)
383aa6
		ret |= ARGV_VERBOSITY_NONE;
383aa6
383aa6
	if (flags & AMGC_DRIVER_VERBOSITY_ERRORS)
383aa6
		ret |= ARGV_VERBOSITY_ERRORS;
383aa6
383aa6
	if (flags & AMGC_DRIVER_VERBOSITY_STATUS)
383aa6
		ret |= ARGV_VERBOSITY_STATUS;
383aa6
383aa6
	return ret;
383aa6
}
383aa6
383aa6
static int amgc_driver_usage(
383aa6
	const char *			program,
383aa6
	const char *			arg,
383aa6
	const struct argv_option *	options,
383aa6
	struct argv_meta *		meta)
383aa6
{
383aa6
	char header[512];
383aa6
383aa6
	snprintf(header,sizeof(header),
383aa6
		"Usage: %s [options] <file>...\n" "Options:\n",
383aa6
		program);
383aa6
383aa6
	argv_usage(stdout,header,options,arg);
383aa6
	argv_free(meta);
383aa6
383aa6
	return AMGC_USAGE;
383aa6
}
383aa6
383aa6
static struct amgc_driver_ctx_impl * amgc_driver_ctx_alloc(
383aa6
	struct argv_meta *		meta,
383aa6
	const struct amgc_common_ctx *	cctx,
6adb90
	size_t				nunits,
6adb90
	size_t				nactions)
383aa6
{
383aa6
	struct amgc_driver_ctx_alloc *	ictx;
6adb90
	struct amgc_action *		actions;
383aa6
	size_t				size;
383aa6
	struct argv_entry *		entry;
383aa6
	const char **			units;
383aa6
383aa6
	size =  sizeof(struct amgc_driver_ctx_alloc);
383aa6
	size += (nunits+1)*sizeof(const char *);
383aa6
890152
	if (!(ictx = calloc(1,size)))
383aa6
		return 0;
383aa6
6adb90
	if (!(actions = calloc(nactions+1,sizeof(*ictx->actions)))) {
6adb90
		free(ictx);
6adb90
		return 0;
6adb90
	}
6adb90
383aa6
	if (cctx)
383aa6
		memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
383aa6
383aa6
	for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
383aa6
		if (!entry->fopt)
383aa6
			*units++ = entry->arg;
383aa6
383aa6
	ictx->meta = meta;
389acb
	ictx->ctx.fdtmpin = -1;
6adb90
	ictx->actions = actions;
6adb90
	ictx->ctx.actions = actions;
383aa6
	ictx->ctx.ctx.units = ictx->units;
383aa6
	return &ictx->ctx;
383aa6
}
383aa6
383aa6
static int amgc_get_driver_ctx_fail(struct argv_meta * meta)
383aa6
{
383aa6
	argv_free(meta);
383aa6
	return -1;
383aa6
}
383aa6
1fd161
static int amgc_init_cparser(void)
1fd161
{
1fd161
	init_temp_files();
1fd161
	init_default_driver();
1fd161
	init_driver();
1fd161
	init_preprocessor();
1fd161
	init_parser();
1fd161
	init_ast();
1fd161
	init_gen_firm();
1fd161
43c8c9
	target_set_defaults();
43c8c9
	target_setup();
43c8c9
1fd161
	return 0;
1fd161
}
1fd161
383aa6
int amgc_get_driver_ctx(
10a518
	char **				argv,
10a518
	char **				envp,
383aa6
	uint32_t			flags,
383aa6
	struct amgc_driver_ctx **	pctx)
383aa6
{
383aa6
	struct amgc_driver_ctx_impl *	ctx;
383aa6
	struct amgc_common_ctx		cctx;
383aa6
	const struct argv_option *	options;
383aa6
	struct argv_meta *		meta;
383aa6
	struct argv_entry *		entry;
383aa6
	size_t				nunits;
6adb90
	size_t				nactions;
383aa6
	const char *			program;
383aa6
383aa6
	options = amgc_default_options;
383aa6
383aa6
	if (!(meta = argv_get(argv,options,amgc_argv_flags(flags))))
383aa6
		return -1;
383aa6
383aa6
	nunits	= 0;
6adb90
	nactions= 0;
383aa6
	program = argv_program_name(argv[0]);
383aa6
	memset(&cctx,0,sizeof(cctx));
383aa6
383aa6
	if (!argv[1] && (flags & AMGC_DRIVER_VERBOSITY_USAGE))
383aa6
		return amgc_driver_usage(program,0,options,meta);
383aa6
fd0662
	/* compiler defaults */
fd0662
	cctx.std = STANDARD_C99;
fd0662
383aa6
	/* get options, count units */
383aa6
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
383aa6
		if (entry->fopt) {
383aa6
			switch (entry->tag) {
383aa6
				case TAG_HELP:
383aa6
					if (flags & AMGC_DRIVER_VERBOSITY_USAGE)
383aa6
						return amgc_driver_usage(program,entry->arg,options,meta);
383aa6
383aa6
				case TAG_VERSION:
383aa6
					cctx.drvflags |= AMGC_DRIVER_VERSION;
383aa6
					break;
fd0662
fd0662
				case TAG_LANG_STD:
fd0662
					cctx.std = amgc_lang_std_from_string(entry->arg);
fd0662
					break;
6adb90
6adb90
				default:
6adb90
					nactions++;
6adb90
					break;
383aa6
			}
383aa6
		} else
383aa6
			nunits++;
383aa6
	}
383aa6
1fd161
	if (amgc_init_cparser())
1fd161
		return amgc_get_driver_ctx_fail(meta);
1fd161
6adb90
	if (!(ctx = amgc_driver_ctx_alloc(meta,&cctx,nunits,nactions)))
383aa6
		return amgc_get_driver_ctx_fail(meta);
383aa6
6adb90
	/* create action vector */
6adb90
	for (entry=meta->entries,nactions=0; entry->fopt || entry->arg; entry++)
6adb90
		if (entry->fopt)
6adb90
			switch (entry->tag) {
6adb90
				case TAG_PRINT_ENUMS:
6adb90
					ctx->actions[nactions].type = AMGC_ACTION_OUTPUT;
6adb90
					ctx->actions[nactions++].action = AMGC_OUTPUT_ENUM;
6adb90
					break;
2e879c
2e879c
				case TAG_LIST_ENUMS:
2e879c
					ctx->actions[nactions].type = AMGC_ACTION_OUTPUT;
2e879c
					ctx->actions[nactions++].action = AMGC_LIST_ENUM;
2e879c
					break;
f2e43d
f2e43d
				case TAG_PRINT_TYPEDEFS:
f2e43d
					ctx->actions[nactions].type = AMGC_ACTION_OUTPUT;
f2e43d
					ctx->actions[nactions].action = AMGC_OUTPUT_TYPEDEF;
f2e43d
f2e43d
					if (!strcmp("primary",entry->arg))
f2e43d
						ctx->actions[nactions++].subset = TYPE_ATOMIC;
f2e43d
					break;
6adb90
			}
6adb90
383aa6
	ctx->ctx.program	= program;
383aa6
	ctx->ctx.cctx		= &ctx->cctx;
6adb90
	ctx->cctx.actions	= ctx->actions;
43c8c9
	ctx->cctx.ccenv		= &ctx->ccenv;
383aa6
383aa6
	*pctx = &ctx->ctx;
383aa6
	return AMGC_OK;
383aa6
}
383aa6
383aa6
int amgc_create_driver_ctx(
383aa6
	const struct amgc_common_ctx *	cctx,
383aa6
	struct amgc_driver_ctx **	pctx)
383aa6
{
383aa6
	struct argv_meta *		meta;
383aa6
	struct amgc_driver_ctx_impl *	ctx;
10a518
	char *				argv[] = {"apimagic_driver",0};
383aa6
383aa6
	if (!(meta = argv_get(argv,amgc_default_options,0)))
383aa6
		return -1;
383aa6
6adb90
	if (!(ctx = amgc_driver_ctx_alloc(meta,cctx,0,0)))
383aa6
		return amgc_get_driver_ctx_fail(0);
383aa6
383aa6
	ctx->ctx.cctx = &ctx->cctx;
383aa6
	memcpy(&ctx->cctx,cctx,sizeof(*cctx));
383aa6
	*pctx = &ctx->ctx;
383aa6
	return AMGC_OK;
383aa6
}
383aa6
1fd161
static void amgc_exit_cparser(void)
1fd161
{
1fd161
	exit_gen_firm();
1fd161
	exit_ast2firm();
1fd161
	exit_ast();
1fd161
	exit_parser();
1fd161
	exit_preprocessor();
1fd161
	exit_driver();
1fd161
	exit_default_driver();
1fd161
	exit_temp_files();
1fd161
}
1fd161
383aa6
static void amgc_free_driver_ctx_impl(struct amgc_driver_ctx_alloc * ictx)
383aa6
{
383aa6
	argv_free(ictx->meta);
6adb90
	free(ictx->actions);
383aa6
	free(ictx);
1fd161
	amgc_exit_cparser();
383aa6
}
383aa6
383aa6
void amgc_free_driver_ctx(struct amgc_driver_ctx * ctx)
383aa6
{
383aa6
	struct amgc_driver_ctx_alloc *	ictx;
383aa6
	uintptr_t			addr;
383aa6
383aa6
	if (ctx) {
383aa6
		addr = (uintptr_t)ctx - offsetof(struct amgc_driver_ctx_alloc,ctx);
383aa6
		addr = addr - offsetof(struct amgc_driver_ctx_impl,ctx);
383aa6
		ictx = (struct amgc_driver_ctx_alloc *)addr;
383aa6
		amgc_free_driver_ctx_impl(ictx);
383aa6
	}
383aa6
}