aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.c
blob: f04be03eea052c28c38432f201f333b873d4efe5 (plain) (blame)
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
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <gmp.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/Core.h>

#include "alloc.h"
#include "codegen.h"
#include "common.h"
#include "errors.h"
#include "parser.h"
#include "types.h"

typedef mpq_t constval;

typedef struct constmap {
    struct constmap *child[4];
    struct strview key;
    constval val;
} constmap;

/* A context structure we can pass to all the codegen functions just so they
   have easy access to everything */
struct cgctx {
	LLVMContextRef ctx;
	LLVMModuleRef mod;
	LLVMBuilderRef bob;
	struct strview namespace;
	constmap *consts;
	arena a;
};

static void codegenprog(struct cgctx, struct type *, struct ast,
                        struct lexemes)
	__attribute__((nonnull));
// static size_t codegenexpr(struct cgctx, struct type *, struct ast,
//                           struct lexemes, size_t, LLVMValueRef *)
// 	__attribute__((nonnull));

static LLVMTypeRef type2llvm(struct type);

static bool constmap_equals(struct strview, struct strview);
static uint64_t constmap_hash(struct strview);
static constval *constmap_upsert(constmap **, struct strview, arena *)
	__attribute__((nonnull(1)));

/* TODO: Don’t do this? */
#define lengthof(xs) (sizeof(xs) / sizeof(*(xs)))

void
codegen(const char *file, struct type *types, struct ast ast,
        struct lexemes toks)
{
	struct cgctx ctx = {0};
	ctx.ctx = LLVMContextCreate();
	ctx.mod = LLVMModuleCreateWithNameInContext("oryx", ctx.ctx);
	ctx.bob = LLVMCreateBuilderInContext(ctx.ctx);
	LLVMSetSourceFileName(ctx.mod, file, strlen(file));

	codegenprog(ctx, types, ast, toks);

	arena_free(&ctx.a);

	LLVMDisposeBuilder(ctx.bob);

	char *error = NULL;
	LLVMVerifyModule(ctx.mod, LLVMAbortProcessAction, &error);
	LLVMDisposeMessage(error);

	LLVMDumpModule(ctx.mod);
	LLVMDisposeModule(ctx.mod);
	LLVMContextDispose(ctx.ctx);
}

void
interp_const_expr(struct cgctx ctx, struct type *types, struct ast ast,
                  struct lexemes toks, idx_t_ i, constval v)
{
	switch (ast.kinds[i]) {
	case ASTNUMLIT: {
		mpf_t f;
		mpq_init(v);

		struct strview sv = toks.strs[ast.lexemes[i]];
		char *buf = bufalloc(NULL, sv.len + 1, 1);
		memcpy(buf, sv.p, sv.len);
		buf[sv.len] = 0;

		if (mpf_init_set_str(f, buf, 10) == -1)
			err("mpf_init_set_str: Invalid input ‘%s’", buf);
		free(buf);

		mpq_set_f(v, f);
		mpf_clear(f);
		break;
	}
	case ASTIDENT: {
		struct strview sv = toks.strs[ast.lexemes[i]];
		constval *w = constmap_upsert(&ctx.consts, sv, NULL);
		v = *w;
		break;
	}
	default:
		err("codegen: Not implemented");
	}
}

void
register_const(struct cgctx ctx, struct type *types, struct ast ast,
               struct lexemes toks, idx_t_ i)
{
	struct pair p = ast.kids[i];
	assert(p.rhs != AST_EMPTY);

	constval v;
	interp_const_expr(ctx, types, ast, toks, p.rhs, v);

	struct strview sv = toks.strs[ast.lexemes[i]];
	*constmap_upsert(&ctx.consts, sv, &ctx.a) = v;

	/* TODO: Remove */
	printf("%.*s = ", (int)sv.len, sv.p);
	mpq_out_str(stdout, 10, v);
	putchar('\n');
	mpq_clear(v);
}

void
codegenprog(struct cgctx ctx, struct type *types, struct ast ast,
            struct lexemes toks)
{
	for (size_t i = 0; i < ast.len; i = fwdnode(ast, i)) {
		if ((ast.kinds[i] | 1) == ASTPCDECL)
			register_const(ctx, types, ast, toks, i);

		// mpq_t v = interp_const_expr(ctx, ast, toks, p.rhs);
		// constmap_upsert(&ctx.consts, ast.lexemes[p.lhs], &ctx.a);
	}

	// for (size_t i = 0; i < ast.len;) {
	// 	assert(ast.kinds[i] <= _AST_DECLS_END);
	// 	i = codegenprog(ctx, types, ast, toks, i);
	// }
}

// size_t
// codegenexpr(struct cgctx ctx, struct type *types, struct ast ast,
//             struct lexemes toks, size_t i, LLVMValueRef *v)
// {
// 	(void)ctx;
// 	switch (ast.kinds[i]) {
// 	case ASTNUMLIT: {
// 		/* TODO: Arbitrary precision? */
// 		struct strview sv = toks.strs[ast.lexemes[i]];
//
// 		bool has_sep = memchr(sv.p, '\'', sv.len) != NULL;
//
// 		/* TODO: Temporary one-time-use allocator? */
// 		if (has_sep) {
// 			size_t len = 0;
// 			char *p = bufalloc(NULL, sv.len, 1);
// 			for (size_t i = 0; i < sv.len; i++) {
// 				if (sv.p[i] != '\'')
// 					p[len++] = sv.p[i];
// 			}
//
// 			*v = LLVMConstIntOfStringAndSize(type2llvm(types[i]), p, len, 10);
// 			free(p);
// 		} else {
// 			*v = LLVMConstIntOfStringAndSize(type2llvm(types[i]), sv.p, sv.len,
// 			                                 10);
// 		}
// 		return i + 1;
// 	}
// 	case ASTIDENT:
// 		err("codegen: %s: Not implemented", __func__);
// 	default:
// 		assert(!"unreachable");
// 		__builtin_unreachable();
// 	}
// }

LLVMTypeRef
type2llvm(struct type t)
{
	switch (t.kind) {
	case TYPE_UNSET:
	case TYPE_CHECKING:
		assert(!"codegen: Hit TYPE_UNSET or TYPE_CHECKING");
		__builtin_unreachable();
	case TYPE_FN:
		err("codegen: %s: Not implemented", __func__);
	case TYPE_NUM:
		/* TODO: Floats */
		/* TODO: Arbitrary precision */
		if (t.size == 0)
			return LLVMInt64Type();
		return LLVMIntType((unsigned)t.size * 8);
	default:
		__builtin_unreachable();
	}
}

bool
constmap_equals(struct strview x, struct strview y)
{
	return x.len == y.len && memcmp(x.p, y.p, x.len) == 0;
}

uint64_t
constmap_hash(struct strview sv)
{
	uint64_t h = 0x100;
	for (size_t i = 0; i < sv.len; i++) {
		h ^= sv.p[i];
		h *= 1111111111111111111u;
	}
	return h;
}

constval *
constmap_upsert(constmap **m, struct strview key, arena *a)
{
	for (uint64_t h = constmap_hash(key); *m; h <<= 2) {
		if (constmap_equals(key, (*m)->key))
			return &(*m)->val;
		m = &(*m)->child[h >> 62];
	}
	if (a == NULL)
		return NULL;
	*m = arena_new(a, constmap, 1);
	(*m)->key = key;
	return &(*m)->val;
}