-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
133 lines (101 loc) · 3.44 KB
/
code.c
File metadata and controls
133 lines (101 loc) · 3.44 KB
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
#include "code.h"
struct IRProgram* IRProgramNew(void) {
struct IRProgram* program = malloc(sizeof(struct IRProgram));
program->func_cap = INST_INIT_CAP;
program->func_count = 0;
program->funcs = scalloc(program->func_cap, sizeof(struct IR*));
}
void IRProgramFreeAddIR(struct IRProgram* program, struct IR* ir) {
if (program->func_count >= program->func_cap) {
program->func_cap *= 2;
program->funcs = srealloc(program->funcs, program->func_cap);
}
program->funcs[program->func_count++] = ir;
}
void InstructionGrow(struct Instruction* inst, int need) {
if (inst->size + need <= inst->cap) return;
while (inst->size + need > inst->cap) {inst->cap *= 2;}
inst->code = srealloc(inst->code, inst->cap);
if (!inst->code) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
}
struct Instruction* InstructionNew(void) {
struct Instruction* inst = smalloc(sizeof(struct Instruction));
inst->cap = INST_INIT_CAP;
inst->size = 0;
inst->code = smalloc(inst->cap);
inst->code[0] = '\0';
return inst;
}
struct IR* IRNew(void) {
struct IR* ir = smalloc(sizeof(struct IR));
ir->argc = 0;
ir->name = "main";
ir->ret_t = 0;
ir->file = "stdin";
ir->entry = InstructionNew();
ir->local = InstructionNew();
ir->global = InstructionNew();
ir->param = InstructionNew();
ir->temp = InstructionNew();
return ir;
}
void InstructionFree(struct Instruction* inst) {
free(inst->code);
free(inst);
}
void InstructionEmit(struct Instruction* inst, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
va_list copy;
va_copy(copy, args);
int len = vsnprintf(NULL, 0, fmt, copy);
va_end(copy);
InstructionGrow(inst, len + 1);
vsnprintf(inst->code + inst->size,
inst->cap - inst->size,
fmt, args);
inst->size += len;
va_end(args);
}
void InstructionSub(struct Instruction* inst, char* code, int len) {
InstructionGrow(inst, len + 1);
memcpy(inst->code + inst->size, code, len);
inst->size += len;
inst->code[inst->size] = '\0';
}
char* IRToString(struct IR* ir) {
struct Instruction* code = InstructionNew();
InstructionEmit(code, "define [%d], %s() -> %s\n", ir->argc, ir->name, ir->ret_t);
if (ir->param && ir->param->size > 0) {
InstructionEmit(code, "param:\n");
InstructionSub(code, ir->param->code, ir->param->size);
}
if (ir->local && ir->local->size > 0) {
InstructionEmit(code, "local:\n");
InstructionSub(code, ir->local->code, ir->local->size);
}
if (ir->global && ir->global->size > 0) {
InstructionEmit(code, "global:\n");
InstructionSub(code, ir->global->code, ir->global->size);
}
if (ir->temp && ir->temp->size > 0) {
InstructionEmit(code, "temp:\n");
InstructionSub(code, ir->temp->code, ir->temp->size);
}
if (ir->entry && ir->entry->size > 0) {
InstructionEmit(code, "entry:\n");
InstructionSub(code, ir->entry->code, ir->entry->size);
}
char* c = strdup(code->code);
InstructionFree(code);
return c;
}
void IRSetup(struct IR* ir, char* name, int argc, char* ret_t, char* file) {
ir->argc = argc;
ir->name = strdup(name);
ir->ret_t = ret_t;
ir->file = strdup(file);
}