aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/codegen.cpp
blob: 412daae3dfad9c312afd48e8838e9c564c69c5a9 (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
325
326
327
#include "ssa.cpp"
#include "print_llvm.cpp"

struct ssaGen {
	ssaModule module;
	gbFile output_file;
};

b32 ssa_gen_init(ssaGen *s, Checker *c) {
	if (c->error_collector.count > 0)
		return false;

	gb_for_array(i, c->parser->files) {
		AstFile *f = &c->parser->files[i];
		if (f->error_collector.count > 0)
			return false;
		if (f->tokenizer.error_count > 0)
			return false;
	}

	ssa_module_init(&s->module, c);

	gbFileError err = gb_file_create(&s->output_file, "../examples/test.ll");
	if (err != gbFileError_None)
		return false;

	return true;
}

void ssa_gen_destroy(ssaGen *s) {
	ssa_module_destroy(&s->module);
	gb_file_close(&s->output_file);
}

void ssa_gen_code(ssaGen *s) {
	if (v_zero == NULL) {
		v_zero   = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(0));
		v_one    = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(1));
		v_zero32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(0));
		v_one32  = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(1));
		v_two32  = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(2));
	}

	ssaModule *m = &s->module;
	CheckerInfo *info = m->info;
	gbAllocator a = m->allocator;

	gb_for_array(i, info->entities.entries) {
		auto *entry = &info->entities.entries[i];
		Entity *e = cast(Entity *)cast(uintptr)entry->key;
		DeclInfo *decl = entry->value;

		String name = e->token.string;

		switch (e->kind) {
		case Entity_TypeName: {
			ssaValue *t = ssa_make_value_type_name(a, e);
			map_set(&m->members, hash_string(name), t);
		} break;

		case Entity_Variable: {
			ssaValue *g = ssa_make_value_global(a, e, NULL);
			map_set(&m->values, hash_pointer(e), g);
			map_set(&m->members, hash_string(name), g);
		} break;

		case Entity_Procedure: {
			ssaValue *p = ssa_make_value_procedure(a, e, decl, m);
			map_set(&m->values, hash_pointer(e), p);
			map_set(&m->members, hash_string(name), p);
		} break;
		}
	}

	gb_for_array(i, m->members.entries) {
		auto *entry = &m->members.entries[i];
		ssaValue *v = entry->value;
		if (v->kind == ssaValue_Proc)
			ssa_build_proc(v);
	}

	ssa_print_llvm_ir(&s->output_file, &s->module);
}





#if 0
#include "type.cpp"
#include "ir.cpp"

struct Codegen {
	Checker *checker;
	gbFile file;
	gbAllocator allocator;

	irModule module;

	ErrorCollector error_collector;
};

b32 init_codegen(Codegen *c, Checker *checker) {
	c->checker = checker;

	if (c->error_collector.count != 0)
		return false;
	for (isize i = 0; i < gb_array_count(checker->parser->files); i++) {
		AstFile *f = &checker->parser->files[i];
		if (f->error_collector.count != 0)
			return false;
		if (f->tokenizer.error_count != 0)
			return false;
	}

	c->allocator = gb_heap_allocator();

	ir_module_init(&c->module, c->checker);

	return true;
}

void destroy_codegen(Codegen *c) {
	ir_module_destroy(&c->module);
}

b32 is_blank_identifier(AstNode *identifier) {
	if (identifier->kind == AstNode_Identifier) {
		return are_strings_equal(identifier->identifier.token.string, make_string("_"));
	}
	return false;
}


irValue *ir_add_basic_block(gbAllocator a, irValue *p, String label) {
	irValue *b = ir_make_value_basic_block(a, gb_array_count(p->procedure.blocks), label, p);
	gb_array_append(p->procedure.blocks, b);
	return b;
}

irValue *ir_emit_from_block(irValue *b, irInstruction *i) {
	GB_ASSERT(b->kind == irValue_BasicBlock);
	i->block = b;
	gb_array_append(b->basic_block.instructions, i);
	return ir_make_value_instruction(gb_heap_allocator(), i);
}


irValue *ir_emit(irValue *p, irInstruction *i) {
	GB_ASSERT(p->kind == irValue_Procedure);
	return ir_emit_from_block(p->procedure.curr_block, i);
}


irInstruction *ir_add_local(irValue *p, Type *type, TokenPos pos) {
	irInstruction *i = ir_alloc_instruction(gb_heap_allocator(), irInstruction_Alloca);
	i->reg.type = type;
	i->reg.pos = pos;
	gb_array_append(p->procedure.locals, ir_emit(p, i));
	return i;
}

irInstruction *ir_add_named_local(irValue *p, Entity *e) {
	irInstruction *i = ir_add_local(p, e->type, e->token.pos);
	i->alloca.label = e->token.string;
	// map_set(&p->procedure.variables, hash_pointer(e), );
	return i;
}

irInstruction *ir_add_local_for_identifier(irValue *p, AstNode *i) {
	GB_ASSERT(p->kind == irValue_Procedure);
	GB_ASSERT(i->kind == AstNode_Identifier);
	auto *found = map_get(&p->procedure.module->checker->definitions, hash_pointer(i));
	return ir_add_named_local(p, *found);
}


void ir_build_variable_declaration(irValue *p, AstNode *d) {
	GB_ASSERT(p->kind == irValue_Procedure);
	auto *vd = &d->variable_declaration;

	if (vd->name_count == vd->value_count) {
		AstNode *name = vd->name_list;
		AstNode *value = vd->value_list;
		for (;
		     name != NULL && value != NULL;
		     name = name->next, value = value->next) {
			if (!is_blank_identifier(name)) {
				ir_add_local_for_identifier(p, name);
			}
			// auto lvalue = build_address(p, name, false);
			// build_assignment(p, lvalue, value, true, NULL);
		}
	} else if (vd->value_count == 0) {
		AstNode *name = vd->name_list;
		for (;
		     name != NULL;
		     name = name->next) {
			if (!is_blank_identifier(name)) {

			}

			// build_assignment(p, )
		}
	} else {
		// TODO(bill): Tuple
	}

}


void ir_build_expression(irValue *p, AstNode *e) {
	GB_ASSERT(p->kind == irValue_Procedure);

}


void ir_build_statement(irValue *p, AstNode *s);

void ir_build_statement_list(irValue *p, AstNode *list) {
	GB_ASSERT(p->kind == irValue_Procedure);
	for (AstNode *item = list; item != NULL; item = item->next) {
		ir_build_statement(p, item);
	}
}

void ir_build_statement(irValue *p, AstNode *s) {
	GB_ASSERT(p->kind == irValue_Procedure);

	switch (s->kind) {
	case AstNode_EmptyStatement:
		break;

	case AstNode_VariableDeclaration: {
		auto *vd = &s->variable_declaration;
		if (vd->kind == Declaration_Mutable) {
			ir_build_variable_declaration(p, s);
		}
	} break;


	case AstNode_ExpressionStatement:
		ir_build_expression(p, s->expression_statement.expression);
		break;

	case AstNode_BlockStatement:
		ir_build_statement_list(p, s->block_statement.list);
		break;
	}

}





void ir_begin_procedure_body(irValue *p) {
	gbAllocator a = gb_heap_allocator();
	p->procedure.curr_block = ir_add_basic_block(a, p, make_string("entry"));
	map_init(&p->procedure.variables, a);
}

void ir_end_procedure_body(irValue *p) {
	p->procedure.curr_block = NULL;
	map_destroy(&p->procedure.variables);
}


void ir_build_procedure(irModule *m, irValue *p) {
	if (p->procedure.blocks != NULL)
		return;
	AstNode *proc_type = NULL;
	AstNode *body = NULL;
	switch (p->procedure.node->kind) {
	case AstNode_ProcedureDeclaration:
		proc_type = p->procedure.node->procedure_declaration.procedure_type;
		body = p->procedure.node->procedure_declaration.body;
		break;
	case AstNode_ProcedureLiteral:
		proc_type = p->procedure.node->procedure_literal.type;
		body = p->procedure.node->procedure_literal.body;
		break;
	default:
		return;
	}

	if (body == NULL) {
		// NOTE(bill): External procedure
		return;
	}

	defer (gb_printf("build procedure %.*s\n", LIT(p->procedure.token.string)));


	ir_begin_procedure_body(p);
	ir_build_statement(p, body);
	ir_end_procedure_body(p);
}

void ir_build_proc_decl(irModule *m, AstNode *decl) {
	GB_ASSERT(decl != NULL);
	auto *pd = &decl->procedure_declaration;
	if (is_blank_identifier(pd->name))
		return;

	Entity *e = entity_of_identifier(m->checker, pd->name);
	irValue *p = *map_get(&m->values, hash_pointer(e));
	ir_build_procedure(m, p);
}



void generate_code(Codegen *c) {
	gbAllocator a = gb_heap_allocator();

	ir_module_create(&c->module);

	for (isize i = 0; i < gb_array_count(c->module.values.entries); i++) {
		irValue *v = c->module.values.entries[i].value;
		switch (v->kind) {
		case irValue_Procedure:
			ir_build_proc_decl(&c->module, v->procedure.node);
			break;
		}
	}
}
#endif