summaryrefslogtreecommitdiffhomepage
path: root/src/driver/amgc_unit_ctx.c
blob: 537f851f05e157c762755172d1ddf2f61a314a09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**********************************************************/
/*  apimagic: cparser-based API normalization utility     */
/*  Copyright (C) 2015--2021  SysDeer Technologies, LLC   */
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
/**********************************************************/

#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>


#include <cparser/driver/driver.h>
#include <cparser/driver/c_driver.h>

#include <apimagic/apimagic.h>
#include "apimagic_driver_impl.h"
#include "apimagic_errinfo_impl.h"

static int amgc_free_unit_ctx_impl(struct amgc_unit_ctx_impl * ctx, int ret)
{
	if (ctx) {
		if (ctx->entities)
			amgc_free_unit_entities(ctx->entities);

		free(ctx);
	}

	return ret;
}

static int amgc_stdin_to_tmp(const struct amgc_driver_ctx * dctx)
{
	struct amgc_driver_ctx_impl *	ictx;
	uintptr_t			addr;
	int				fdtmp;

	ssize_t ret;
	ssize_t cnt;
	char *	ch;
	char	buf[4096];
	char	template[] = "/tmp/amgc_stdin_to_tmp_XXXXXX";

	addr = (uintptr_t)dctx - offsetof(struct amgc_driver_ctx_impl,ctx);
	ictx = (struct amgc_driver_ctx_impl *)addr;

	if (ictx->fdtmpin >= 0)
		return dup(ictx->fdtmpin);

	if ((fdtmp = mkstemp(template)) < 0)
		return -1;

	if ((ictx->fdtmpin = dup(fdtmp)) < 0) {
		close(fdtmp);
		return -1;
	}

	for (;;) {
		ret = read(0,buf,sizeof(buf)-1);

		while ((ret < 0) && (errno == EINTR))
			ret = read(0,buf,sizeof(buf)-1);

		if (ret < 0) {
			close(fdtmp);
			return -1;

		} else if (ret == 0) {
			return fdtmp;

		} else {
			ch  = buf;
			cnt = ret;
			ret = 0;

			for (; cnt; ) {
				ret = write(fdtmp,ch,cnt);

				while ((ret < 0) && (errno == EINTR))
					ret = write(fdtmp,ch,cnt);

				if (ret < 0) {
					close(fdtmp);
					return -1;
				}

				ch  += ret;
				cnt -= ret;
			}
		}
	}
}

static bool amgc_cparser_no_op(
	compilation_env_t *	env,
	compilation_unit_t *	unit)
{
	(void)env;
	(void)unit;
	return true;
}

static void amgc_init_cparser_unit(void)
{
	set_default_handlers();

	/* parse only */
	set_unit_handler(COMPILATION_UNIT_AST,
			build_firm_ir,true);

	/* assign no-op handler */
	set_unit_handler(COMPILATION_UNIT_PREPROCESSED_ASSEMBLER,
			amgc_cparser_no_op,true);

	set_unit_handler(COMPILATION_UNIT_OBJECT,
			amgc_cparser_no_op,true);
}

int amgc_get_unit_ctx(
	const struct amgc_driver_ctx *	dctx,
	const char *			path,
	struct amgc_unit_ctx **		pctx)
{
	struct amgc_unit_ctx_impl *	ctx;
	int				fd;

	amgc_init_cparser_unit();

	if (!dctx)
		return AMGC_CUSTOM_ERROR(
			dctx,AMGC_ERR_NULL_CONTEXT);

	else if (!(ctx = calloc(1,sizeof(*ctx))))
		return AMGC_SYSTEM_ERROR(dctx);

	amgc_driver_set_ectx(
		dctx,0,path);

	if (strcmp(path,"-"))
		fd = -1;

	else if ((fd = amgc_stdin_to_tmp(dctx)) < 0)
		return amgc_free_unit_ctx_impl(
			ctx,AMGC_SYSTEM_ERROR(dctx));

	else if (lseek(fd,0,SEEK_SET < 0))
		return amgc_free_unit_ctx_impl(
			ctx,AMGC_NESTED_ERROR(dctx));

	else if (!(ctx->ccunit.input = fdopen(fd,"r")))
		return amgc_free_unit_ctx_impl(
			ctx,AMGC_FILE_ERROR(dctx));

	/* compilation unit */
	ctx->ccunit.name		= path;
	ctx->ccunit.original_name	= path;
	ctx->ccunit.type		= COMPILATION_UNIT_C;
	ctx->ccunit.standard		= dctx->cctx->std;

	/* parse, generate ast, generate ir */
	if ((process_unit(ctx->cctx.ccenv,&ctx->ccunit)) == false) {
		if (ctx->ccunit.input)
			fclose(ctx->ccunit.input);
		return amgc_free_unit_ctx_impl(
			ctx,AMGC_CUSTOM_ERROR(dctx,AMGC_ERR_FLOW_ERROR));
	}

	memcpy(&ctx->cctx,dctx->cctx,
		sizeof(ctx->cctx));

	ctx->dctx	    = dctx;
	ctx->path	    = path;
	ctx->uctx.path	    = &ctx->path;
	ctx->uctx.cctx	    = &ctx->cctx;
	ctx->uctx.meta	    = &ctx->meta;
	ctx->uctx.ccunit    = &ctx->ccunit;

	if (amgc_get_unit_entities(&ctx->uctx,&ctx->meta,&ctx->entities))
		return amgc_free_unit_ctx_impl(
			ctx,AMGC_CUSTOM_ERROR(dctx,AMGC_ERR_FLOW_ERROR));

	ctx->uctx.entities  = ctx->entities;
	*pctx = &ctx->uctx;
	return 0;
}

void amgc_free_unit_ctx(struct amgc_unit_ctx * ctx)
{
	struct amgc_unit_ctx_impl *	ictx;
	uintptr_t			addr;

	if (ctx) {
		addr = (uintptr_t)ctx - offsetof(struct amgc_unit_ctx_impl,uctx);
		ictx = (struct amgc_unit_ctx_impl *)addr;
		amgc_free_unit_ctx_impl(ictx,0);
	}
}