summaryrefslogtreecommitdiff
path: root/6/main.c
blob: 3034277a41688971823ab42cb4a038f740f82f27 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>

#include "util.h"
#include "parse.h"
#include "mi.h"
#include "asm.h"

#include "../config.h"

/* FIXME: move into one place...? */
Node *file;
char debugopt[128];
int writeasm;
int extracheck = 1;
int p9asm;
char *outfile;
char *objdir;
char **incpaths;
char *localincpath;
size_t nincpaths;
Asmsyntax asmsyntax;

static void
usage(char *prog)
{
	printf("%s [-?|-h] [-o outfile] [-O dir] [-d[dbgopts]] inputs\n", prog);
	printf("\t-?|-h\tPrint this help\n");
	printf("\t-o\tOutput to outfile\n");
	printf("\t-O dir\tOutput to dir\n");
	printf("\t-S\tGenerate assembly source alongside object code\n");
	printf("\t-T\tCompile in test mode\n");
	printf("\t-c\tEnable additional (possibly flaky) checking\n");
	printf("\t-I path\tAdd 'path' to use search path\n");
	printf("\t-d\tPrint debug dumps. Recognized options: f r p i\n");
	printf("\t-G\tGenerate asm in gas syntax\n");
	printf("\t-9\tGenerate asm in plan 9 syntax\n");
	printf("\t-d opts: additional debug logging. Options are listed below:\n");
	printf("\t\tf: log folded trees\n");
	printf("\t\tl: log lowered pre-cfg trees\n");
	printf("\t\tT: log tree immediately\n");
	printf("\t\tr: log register allocation activity\n");
	printf("\t\ti: log instruction selection activity\n");
	printf("\t\tu: log type unifications\n");
}

static void
swapout(char* buf, size_t sz, char* suf) {
	char* psuffix;
	psuffix = strrchr(outfile, '.');
	if (psuffix != NULL)
		swapsuffix(buf, sz, outfile, psuffix, suf);
	else
		bprintf(buf, sz, "%s%s", outfile, suf);
}

static void
mkpath(char *p)
{
	char *e, path[256];

	e = p;
	assert(strlen(p) < sizeof(path));
	while ((e = strstr(e, "/")) != NULL) {
		memcpy(path, p, (e - p));
		path[e - p] = 0;
		mkdir(path, 0755);
		e++;
	}
}

static void
assemble(char *asmsrc, char *path)
{
	char *asmcmd[] = Asmcmd;
	char objfile[256];
	char *psuffix;
	char **p, **cmd;
	size_t ncmd, i;
	int pid, status;

	if (outfile != NULL)
		strncpy(objfile, outfile, sizeof(objfile));
	else {
		psuffix = strrchr(path, '+');
		i = 0;
		if (objdir)
			i = bprintf(objfile, sizeof objfile, "%s/", objdir);
		if (psuffix != NULL)
			swapsuffix(objfile + i, sizeof objfile - i, path, psuffix, Objsuffix);
		else
			swapsuffix(objfile + i, sizeof objfile - i, path, ".myr", Objsuffix);
	}
	mkpath(objfile);

	cmd = NULL;
	ncmd = 0;
	for (p = asmcmd; *p != NULL; p++)
		lappend(&cmd, &ncmd, *p);
	lappend(&cmd, &ncmd, objfile);
	lappend(&cmd, &ncmd, asmsrc);
	lappend(&cmd, &ncmd, NULL);

	pid = fork();
	if (pid == -1) {
		die("couldn't fork");
	} else if (pid == 0) {
		if (execvp(cmd[0], cmd) == -1)
			die("Couldn't exec assembler\n");
	} else {
		waitpid(pid, &status, 0);
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
			die("Couldn't run assembler");
	}
	/*
	We don't want to keep the asm source around, but it's useful
	for deubgging without mapping to line numbers.
	*/
	if (asmsyntax != Plan9 && !writeasm)
		unlink(asmsrc);
}

static char *
dirname(char *path)
{
	char *p;

	p = strrchr(path, '/');
	if (p)
		return strdupn(path, p - path);
	else
		return xstrdup(".");
}

static char *
gentempfile(char *buf, size_t bufsz, char *path, char *suffix)
{
	char *tmpdir;
	char *base;
	struct timeval tv;

	tmpdir = getenv("TMPDIR");
	if (!tmpdir)
		tmpdir = "/tmp";
	base = strrchr(path, '/');
	if (base)
		base++;
	else
		base = path;
	gettimeofday(&tv, NULL);
	bprintf(buf, bufsz, "%s/tmp%lx%lx-%s%s", tmpdir, (long)tv.tv_sec, (long)tv.tv_usec, base, suffix);
	return buf;
}

static int
hasmain(Node *file)
{
	Node *n, *name;

	name = mknsname(Zloc, NULL, "main");
	n = getdcl(file->file.globls, name);
	if (!n)
		return 0;
	n = n->decl.name;
	if (n->name.ns)
		return 0;
	return 1;
}

static void
genuse(char *path)
{
	FILE *f;
	char buf[256];
	char *psuffix;
	size_t i;

	if (outfile != NULL)
		swapout(buf, 1024, ".use");
	else {
		psuffix = strrchr(path, '+');
		i = 0;
		if (objdir)
			i = bprintf(buf, sizeof buf, "%s/", objdir);
		if (psuffix != NULL)
			swapsuffix(buf + i, sizeof buf - i, path, psuffix, ".use");
		else
			swapsuffix(buf + i, sizeof buf - i, path, ".myr", ".use");
	}
	mkpath(buf);
	f = fopen(buf, "w");
	if (!f) {
		fprintf(stderr, "could not open path %s\n", buf);
		exit(1);
	}
	writeuse(f, file);
	fclose(f);
}

int
main(int argc, char **argv)
{
	char buf[1024];
	Stab *globls;
	Optctx ctx;
	size_t i;

	outfile = NULL;

	optinit(&ctx, "cd:?hSo:I:9G:O:T", argv, argc);
	asmsyntax = Defaultasm;
	sizefn = size;
	while (!optdone(&ctx)) {
		switch (optnext(&ctx)) {
		case 'O':
			objdir = ctx.optarg;
			break;
		case 'o':
			outfile = ctx.optarg;
			break;
		case 'S':
			writeasm = 1;
			break;
		case '?':
		case 'h':
			usage(argv[0]);
			exit(0);
			break;
		case 'c':
			extracheck = 1;
			break;
		case 'd':
			while (ctx.optarg && *ctx.optarg)
				debugopt[*ctx.optarg++ & 0x7f]++;
			break;
		case '9':
			asmsyntax = Plan9;
			break;
		case 'G':
			if (!strcmp(ctx.optarg, "e"))
				asmsyntax = Gnugaself;
			else if (!strcmp(ctx.optarg, "m"))
				asmsyntax = Gnugasmacho;
			else
				die("unknown gnu syntax flavor");
			break;
		case 'I':
			lappend(&incpaths, &nincpaths, ctx.optarg);
			break;
		case 'T':
			allowhidden++;
			break;
		default:
			usage(argv[0]);
			exit(0);
			break;
		}
	}

	/* Sysinit is arbitrary code defined by the configure script */
	Sysinit;

	lappend(&incpaths, &nincpaths, Instroot "/lib/myr");

	if (ctx.nargs == 0) {
		fprintf(stderr, "No input files given\n");
		exit(1);
	} else if (ctx.nargs > 1)
		outfile = NULL;

	for (i = 0; i < ctx.nargs; i++) {
		if (outfile)
			localincpath = dirname(outfile);
		else
			localincpath = dirname(ctx.args[i]);

		globls = mkstab(0);
		tyinit(globls);
		tokinit(ctx.args[i]);
		file = mkfile(ctx.args[i]);
		file->file.globls = globls;
		yyparse();

		/* before we do anything to the parse */
		if (debugopt['T'])
			dump(file, stdout);
		loaduses();
		if (hasmain(file))
			geninit();
		infer();
		tagexports(0);
		/* after all type inference */
		if (debugopt['t'])
			dump(file, stdout);

		if (writeasm) {
			if (outfile != NULL)
				swapout(buf, sizeof buf, ".s");
			else
				swapsuffix(buf, sizeof buf, ctx.args[i], ".myr", ".s");
		} else {
			gentempfile(buf, sizeof buf, ctx.args[i], ".s");
		}
		genuse(ctx.args[i]);
		gen(file, buf);
		assemble(buf, ctx.args[i]);

		free(localincpath);
	}

	return 0;
}