-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_utils.c
More file actions
158 lines (134 loc) · 3.83 KB
/
Copy pathcode_utils.c
File metadata and controls
158 lines (134 loc) · 3.83 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
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
#include "code_utils.h"
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "arena.h"
code_t *generate_code(char *mnemonic, const char *arg1, const char *arg2, const char *arg3)
{
static const code_t zero = {};
code_t *code = arena_alloc(allocator, sizeof(code_t));
*code = zero;
code->instruction.mnemonic = mnemonic;
if (arg1 != NULL)
{
code->instruction.arg1 = arg1;
}
if (arg2 != NULL)
{
code->instruction.arg2 = arg2;
}
if (arg3 != NULL)
{
code->instruction.arg3 = arg3;
}
return code;
}
char *generate_temporary()
{
static uint32_t current_temporary = 0;
char temp[MAX_TEMPORARY_LENGTH];
snprintf(temp, MAX_TEMPORARY_LENGTH, "r%" PRIu32, current_temporary++);
return arena_strdup(allocator, temp);
}
char *generate_label()
{
static uint32_t current_label = 0;
char label[MAX_LABEL_LENGTH];
snprintf(label, MAX_LABEL_LENGTH, "L%" PRIu32, current_label++);
return arena_strdup(allocator, label);
}
void generate_code_binary_operation(char *mnemonic, AsdTree *root, AsdTree *left, AsdTree *right)
{
root->location = generate_temporary();
root->code = concatenate_multiple_codes(
left->code, right->code, generate_code(mnemonic, left->location, right->location, root->location), NULL);
}
code_t *concatenate_code(code_t *first, code_t *second)
{
if (second == NULL)
{
return first;
}
if (first == NULL)
{
return second;
}
code_t *current_code = first;
while (current_code->next != NULL)
{
current_code = current_code->next;
}
current_code->next = second;
return first;
}
code_t *concatenate_multiple_codes(code_t *first, ...)
{
va_list ptr;
va_start(ptr, first);
code_t *tail = first;
if (tail != NULL)
{
while (tail->next != NULL)
{
tail = tail->next;
}
}
code_t *next_code;
while ((next_code = va_arg(ptr, code_t *)) != NULL)
{
if (!first)
{
first = tail = next_code;
}
else
{
tail->next = next_code;
}
while (tail->next != NULL)
{
tail = tail->next;
}
}
va_end(ptr);
return first;
}
char *code_to_string(const code_t *code)
{
if (code == NULL)
{
return arena_strdup(allocator, "");
}
const iloc_t *inst = &code->instruction;
const char *label = inst->label ? inst->label : "";
const char *colon = inst->label ? ":" : "";
const char *arg1 = inst->arg1 ? inst->arg1 : "";
const char *arg2 = inst->arg2 ? inst->arg2 : "";
const char *arg3 = inst->arg3 ? inst->arg3 : "";
char line[256];
if (strcmp(inst->mnemonic, "cbr") == 0 || strcmp(inst->mnemonic, "jumpI") == 0)
{
snprintf(line, sizeof(line), "%2s%1s %-8s %3s -> %3s%1s %3s\n", label, colon, inst->mnemonic, arg1, arg2,
inst->arg3 ? "," : "", arg3);
}
else if (strcmp(inst->mnemonic, "storeAI") == 0)
{
snprintf(line, sizeof(line), "%2s%1s %-8s %3s => %3s%1s %3s\n", label, colon, inst->mnemonic, arg1, arg2,
inst->arg2 ? "," : "", arg3);
}
else if (strcmp(inst->mnemonic, "nop") == 0)
{
snprintf(line, sizeof(line), "%2s%1s %-8s\n", label, colon, inst->mnemonic);
}
else
{
const char *arrow = (strncmp(inst->mnemonic, "cmp", 3) != 0) ? "=>" : "->";
snprintf(line, sizeof(line), "%2s%1s %-8s %3s%1s %3s %s %3s\n", label, colon, inst->mnemonic, arg1,
inst->arg2 ? "," : "", arg2, arrow, arg3);
}
char *rest = code_to_string(code->next);
size_t total_len = strlen(line) + strlen(rest) + 1;
char *result = arena_alloc(allocator, total_len);
snprintf(result, total_len, "%s%s", line, rest);
return result;
}