-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdefault.c
190 lines (158 loc) · 6.57 KB
/
rdefault.c
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
#include "compiler.h"
#include <stdlib.h>
#include <memory.h>
#include <assert.h>
static void resolver_default_merge_array_calculate_out_offset(struct datatype* dtype, struct resolver_entity* entity, int* out_offset)
{
assert(entity->array.array_index_node->type == NODE_TYPE_NUMBER);
int index_val = entity->array.array_index_node->llnum;
*(out_offset) += array_offset(dtype, entity->array.index, index_val);
}
struct resolver_default_entity_data* resolver_default_entity_private(struct resolver_entity* entity)
{
return entity->private;
}
struct resolver_default_scope_data* resolver_default_scope_private(struct resolver_scope* scope)
{
return scope->private;
}
char* resolver_default_stack_asm_address(int stack_offset, char* out)
{
if (stack_offset < 0)
{
sprintf(out, "ebp%i", stack_offset);
return out;
}
sprintf(out, "ebp+%i", stack_offset);
return out;
}
void resolver_default_global_asm_address(const char* name, int offset, char* address_out)
{
if (offset == 0)
{
sprintf(address_out, "%s", name);
return;
}
sprintf(address_out, "%s+%i", name, offset);
}
void resolver_default_entity_data_set_address(struct resolver_default_entity_data* entity_data, struct node* var_node, int offset, int flags)
{
if (!var_node)
{
return;
}
if (!variable_node(var_node)->var.name)
{
return;
}
entity_data->offset = offset;
if (flags & RESOLVER_DEFAULT_ENTITY_FLAG_IS_LOCAL_STACK)
{
resolver_default_stack_asm_address(offset, entity_data->address);
sprintf(entity_data->base_address, "ebp");
}
else
{
resolver_default_global_asm_address(variable_node(var_node)->var.name, offset, entity_data->address);
sprintf(entity_data->base_address, "%s", variable_node(var_node)->var.name);
}
}
struct resolver_default_entity_data* resolver_default_new_entity_data()
{
struct resolver_default_entity_data* entity_data = calloc(1, sizeof(struct resolver_default_entity_data));
return entity_data;
}
void* resolver_default_make_private(struct resolver_entity* entity, struct node* node, int offset, struct resolver_scope* scope)
{
struct resolver_default_entity_data* entity_data = resolver_default_new_entity_data();
int entity_flags = 0x00;
if (entity->flags & RESOLVER_ENTITY_FLAG_IS_STACK)
{
entity_flags |= RESOLVER_DEFAULT_ENTITY_FLAG_IS_LOCAL_STACK;
}
entity_data->offset = offset;
entity_data->flags = entity_flags;
entity_data->type = entity->type;
if (variable_node(node))
{
resolver_default_entity_data_set_address(entity_data, variable_node(node), offset, entity_flags);
}
return entity_data;
}
void resolver_default_set_result_base(struct resolver_result* result, struct resolver_entity* base_entity)
{
struct resolver_default_entity_data* data = resolver_default_entity_private(base_entity);
if (!data)
{
return;
}
strncpy(result->base.base_address, data->base_address, sizeof(result->base.base_address));
strncpy(result->base.address, data->address, sizeof(result->base.address));
result->base.offset = data->offset;
}
struct resolver_default_entity_data* resolver_default_new_entity_data_for_var_node(struct node* var_node, int offset, int flags)
{
struct resolver_default_entity_data* entity_data = resolver_default_new_entity_data();
assert(variable_node(var_node));
entity_data->offset = offset;
entity_data->flags = flags;
entity_data->type = RESOLVER_DEFAULT_ENTITY_DATA_TYPE_VARIABLE;
resolver_default_entity_data_set_address(entity_data, variable_node(var_node), offset, flags);
return entity_data;
}
struct resolver_default_entity_data* resolver_default_new_entity_data_for_array_bracket(struct node* bracket_node)
{
struct resolver_default_entity_data* entity_data = resolver_default_new_entity_data();
entity_data->type = RESOLVER_DEFAULT_ENTITY_DATA_TYPE_ARRAY_BRACKET;
return entity_data;
}
struct resolver_default_entity_data* resolver_default_new_entity_data_for_function(struct node* func_node, int flags)
{
struct resolver_default_entity_data* entity_data = resolver_default_new_entity_data();
entity_data->flags = flags;
entity_data->type = RESOLVER_DEFAULT_ENTITY_DATA_TYPE_FUNCTION;
resolver_default_global_asm_address(func_node->func.name, 0, entity_data->address);
return entity_data;
}
struct resolver_entity* resolver_default_new_scope_entity(struct resolver_process* resolver, struct node* var_node, int offset, int flags)
{
assert(var_node->type == NODE_TYPE_VARIABLE);
struct resolver_default_entity_data* private = resolver_default_new_entity_data_for_var_node(var_node, offset, flags);
return resolver_new_entity_for_var_node(resolver, var_node, private, offset);
}
struct resolver_entity* resolver_default_register_function(struct resolver_process* resolver, struct node* func_node, int flags)
{
struct resolver_default_entity_data* private = resolver_default_new_entity_data_for_function(func_node, flags);
return resolver_register_function(resolver, func_node, private);
}
void resolver_default_new_scope(struct resolver_process* resolver, int flags)
{
struct resolver_default_scope_data* scope_data = calloc(1, sizeof(struct resolver_default_scope_data));
scope_data->flags = flags;
resolver_new_scope(resolver, scope_data, flags);
}
void resolver_default_finish_scope(struct resolver_process* resolver)
{
resolver_finish_scope(resolver);
}
void* resolver_default_new_array_entity(struct resolver_result* result, struct node* array_entity_node)
{
return resolver_default_new_entity_data_for_array_bracket(array_entity_node);
}
void resolver_default_delete_entity(struct resolver_entity* entity)
{
free(entity->private);
}
void resolver_default_delete_scope(struct resolver_scope* scope)
{
free(scope->private);
}
struct resolver_entity* resolver_default_merge_entities(struct resolver_process* resolver, struct resolver_result* result, struct resolver_entity* left_entity, struct resolver_entity* right_entity)
{
int new_pos = left_entity->offset + right_entity->offset;
return resolver_make_entity(resolver, result, &right_entity->dtype, left_entity->node, &(struct resolver_entity){.type=right_entity->type, .flags=left_entity->flags, .offset=new_pos, .array=right_entity->array}, left_entity->scope);
}
struct resolver_process* resolver_default_new_process(struct compile_process* compiler)
{
return resolver_new_process(compiler, &(struct resolver_callbacks){.new_array_entity=resolver_default_new_array_entity, .delete_entity=resolver_default_delete_entity, .delete_scope=resolver_default_delete_scope, .make_private=resolver_default_make_private, .set_result_base=resolver_default_set_result_base, .merge_entities=resolver_default_merge_entities});
}