summaryrefslogtreecommitdiff
path: root/src/irm.c
blob: 6260ae0b027e42632f2cb70fda84a7a8e492c540 (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
#include "irm.h"

#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#include <backend.h>
#include <x86.h>

#define STACK_SUB(irm, syment, type) \
	do { \
	        irm->stackdepth += sizeof(type); \			
                syment->loc = irm->stackdepth; \
                syment->len = sizeof(type); \
    while(0)

// TODO: Add scopes.

void irm_init(struct irm *irm)
{
	assert(irm != NULL);
	irm->storage = malloc(1024 * 1024); // TODO: platform-agnostic
	irm->size = (1024 * 1024) / 8; 
	irm->curr_pos = 0;
	irm->state = IRM_GRACEFUL;
	symtab_init(&irm->symtab);
}

void irm_panic(struct irm *irm)
{
	irm->state = IRM_PANIC;
}

void irm_stmt_enter_scope(struct irm *irm)
{
    assert(irm != NULL);
    // Append 1 level.
}

void irm_stmt_leave_scope(struct irm *irm)
{
    assert(irm != NULL);	
}

void irm_stmt_var_decl(struct irm *irm, size_t *offset)
{
 	uint64_t* mem = irm->storage;

	uint64_t ltype = mem[*offset];
	(*offset)++;

	uint64_t symbol_type = mem[*offset];
	if(symbol_type != SYM_VAR) {
		DIE("Symbol type invalid! sym:'%x'", symbol_type);
	}
	(*offset)++;
	uint64_t symid = mem[*offset];
	
	if(irm->symtab.count < symid) {
		DIE("Symbol id: '%d' not found! This is a compiler bug.\n",
		    symid);
	}

	struct symbol_entry *syment = &irm->symtab.entries[symid];
	irm->stackdepth += sizeof(uint64_t);
	syment->loc = irm->stackdepth;
	
    DEBUG("Symbol id: %d\n", symid);
	(*offset)++;	
	(*offset)++;
	switch(ltype) {
	case VAR_DECL_QWORD: {
		uint64_t value = mem[*offset];
		syment->len = sizeof(uint64_t);
		(*offset)++;
		x86_mov_r_i64(RAX, value);
		x86_push_r64(RAX);
		break;
	}			
	case VAR_DECL_DWORD: {
		uint64_t value = mem[*offset];
		syment->len = sizeof(uint32_t);
		(*offset)++;
		x86_push_i32(value);
		break;
	}
	case VAR_DECL_WORD: {
		uint64_t value = mem[*offset];
		syment->len = sizeof(uint32_t);
		(*offset)++;
		x86_push_i32(value); // TODO: Replace with immediate 16 instruction
		break;
	}
	case VAR_DECL_BYTE: {		
		uint64_t value = mem[*offset];
		(*offset)++;
		syment->len = sizeof(uint16_t); 
		x86_push_i8(value);
		break;
	}
	default:
		DIE("Invalid type supplied! This is a compiler bug. type: %x.\n"
		    , ltype);
		break;
	}
}

void irm_stmt_var_assign(struct irm *irm, size_t *offset)
{
	assert(irm != NULL);
	if(offset == NULL) {
		DIE("offset must not be null!");
	}

	(*offset)++;
	uint64_t sym_id = irm->storage[*offset];
	if(sym_id != SYM_VAR) {
		DIE("Variable assignment only supports a symbol of type variable!");
	}

        (*offset)++;
	uint64_t symtab_id = irm->storage[*offset];
	struct symbol_entry *ent = &irm->symtab.entries[symtab_id];
	if(ent->symtype != SYM_VAR) {
		DIE("Symbol '%.*s' is not of type variable!", ent->name.len, ent->name.buf);
	}
	(*offset)++;
	uint64_t value_type = irm->storage[*offset];
	switch(value_type) {
	case NUMBER64: { // FIX STACK ISSUE
		DIE("TODO:: STACK ALGINMENT ISSUE!!");
		(*offset)++;
		uint64_t value = irm->storage[*offset];
	        DEBUG("Assigned nr64 literal %d to '%.*s'.\n", value, ent->name.len, ent->name.buf);
		(*offset)++;
		// TODO: mov [rsp - X], value
		// mov rax, rsp 
		emit8(0x48);
		emit8(0x8B); // MR
		emit8(0xC4); // rsp -> rax
		// sub rax, 8
		emit8(0x48);
		emit8(0x2d);
		emit32(ent->loc);
		// mov rbx, value
		emit8(0x48);
		emit8(0xBB); // b8 + rd
		emit64(value); // FALSE VALUE!! FIX!
		// mov qword ptr [rax], rbx
		emit8(0x48);
		emit8(0x89); // MR
		emit8(0x18); // rbx -> [rax]
		break;
	}
	default:
		DIE("Variable assignment only supports a number literal!");
	}
}

void irm_stmt_done(struct irm *irm)
{
	if(irm->state == IRM_PANIC) return;
        size_t offset = 0;
	
	while(offset < irm->curr_pos - 1) {
		uint64_t type = irm->storage[offset];	
		type &= 0xFFFF0000;

		switch(type) {
		case VAR_DECL:
			irm_stmt_var_decl(irm, &offset);
			break;
		case VAR_ASSIGN:
			irm_stmt_var_assign(irm, &offset);
			break;
		default:
			DIE("Statement identifier not found: %d.", type);
			break;
		}
		DEBUG("Offset: %d, Size: %d\n", offset, irm->curr_pos);
	}
	if(irm->storage[offset] != STMT_DONE) {
		DIE("Statement done was not received! This is a compiler bug.");
	}
	memset(irm->storage, 0, irm->curr_pos);
	irm->curr_pos = 0;
}

void irm_push(struct irm *irm, uint64_t type)
{
	assert(irm != NULL);
	uint64_t *mem = irm->storage + irm->curr_pos;
	mem[0] = type;
	irm->curr_pos += 1;
        DEBUG("added %x to irm.\n", type);
}

void irm_push_64v(struct irm *irm, uint64_t type, uint64_t value)
{
	assert(irm != NULL);
	if(irm->curr_pos + 2 > irm->size)
	        DIE("IRM OVERFLOW!");
	uint64_t* mem = irm->storage + irm->curr_pos;
	mem[0] = type;
	mem[1] = value;
	irm->curr_pos += 2;
        DEBUG("added %d to irm with value %d.\n", type, value);
}

void irm_push_lookup_sym(struct irm *irm, uint64_t type, struct string_view sym)
{
	assert(irm != NULL);

	uint64_t *mem = irm->storage + irm->curr_pos;
	mem[0] = type;
        struct symlookup_result ent = symtab_lookup(&irm->symtab, sym);

	if(ent.ent == NULL) {
		DIE("'%.*s' not found!", sym.len, sym.buf);
	}

	mem[1] = ent.i;
	irm->curr_pos+=2;
}

// TODO:
// 1. Push SYM_TYPE
// 2. Push SYMTAB offset (after adding the sym to it)
void irm_push_sym(struct irm *irm, uint64_t type, struct string_view sym)
{
	assert(irm != NULL);	
	struct symbol_entry entry;
	entry.name = sym;
	entry.loc = 0;
	entry.symtype = type;
	if(symtab_lookup(&irm->symtab, sym).ent != NULL) {
		DIE("Symbol '%.*s' already exists.\n", sym.len, sym.buf);
	}
	size_t symindex = symtab_append(&irm->symtab, entry);
	uint64_t* mem = irm->storage + irm->curr_pos;
	mem[0] = type;
	mem[1] = symindex;
	irm->curr_pos += 2;
	DEBUG("added %.*s to symbol table.\n", sym.len, sym.buf);
}