libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
mir.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <string.h>
3
4#include "dstring.h"
5#include "hashtable.h"
6#include "mir.h"
7
8void
28
29void
31{
32 for (int i = 0; i < mir->bitfields_length; i++) {
34 }
35
36 for (int i = 0; i < mir->registers_length; i++) {
38 }
39
40 for (int i = 0; i < mir->instructions_length; i++) {
42 }
43
44 free(mir->bitfields);
45 free(mir->registers);
46 free(mir->instructions);
47 mir->bitfields = NULL;
48 mir->registers = NULL;
49 mir->instructions = NULL;
50
55}
56
58mir_add_bitfield(mir_t* mir, const char* name, uint32_t size)
59{
60 mir_bitfield_t* bitfield;
61
65 }
66
67 bitfield = &mir->bitfields[mir->bitfields_length];
68 bitfield->fields = NULL;
69 bitfield->fields_length = 0;
70 bitfield->_fields_size = 0;
71 bitfield->size = size;
72 dstring_init(&bitfield->name, 0);
73 dstring_copy(&bitfield->name, name);
74
76
78 return bitfield;
79}
80
82mir_get_bitfield(mir_t* mir, const char* name)
83{
84 int64_t index;
85
86 if (hashtable_get(&mir->_bitfields_index, name, &index) != ERR_OK) {
87 return NULL;
88 }
89
90 return &mir->bitfields[index];
91}
92
94mir_bitfield_add_field(mir_bitfield_t* bitfield, const char* name, uint32_t size)
95{
97
98 if (bitfield->_fields_size < bitfield->fields_length + 1) {
100 bitfield->fields = realloc(bitfield->fields, sizeof(mir_bitfield_field_t) * bitfield->_fields_size);
101 }
102
103 field = &bitfield->fields[bitfield->fields_length++];
104 field->size = size;
105 dstring_init(&field->name, 0);
106 dstring_copy(&field->name, name);
107
108 return field;
109}
110
112mir_bitfield_get_field(mir_bitfield_t* bitfield, const char* name)
113{
114 for (int i = 0; i < bitfield->fields_length; i++) {
115 if (strcmp(bitfield->fields[i].name.data, name) == 0) {
116 return &bitfield->fields[i];
117 }
118 }
119
120 return NULL;
121}
122
123void
125{
126 for (int i = 0; i < bitfield->fields_length; i++) {
127 dstring_close(&bitfield->fields[i].name);
128 }
129
130 dstring_close(&bitfield->name);
131 free(bitfield->fields);
132 bitfield->fields = NULL;
133}
134
136mir_add_register(mir_t* mir, const char* name, uint32_t size)
137{
138 mir_register_t* reg;
139
143 }
144
146 reg->size = size;
147 reg->spec.spec = NULL;
148 reg->spec.spec_length = 0;
149 reg->spec._spec_size = 0;
150 dstring_init(&reg->spec.name, 0);
151 dstring_init(&reg->spec.identifier, 0);
152 dstring_init(&reg->name, 0);
153 dstring_copy(&reg->name, name);
154
156
158 return reg;
159}
160
162mir_get_register(mir_t* mir, const char* name)
163{
164 int64_t index;
165
166 if (hashtable_get(&mir->_registers_index, name, &index) != ERR_OK) {
167 return NULL;
168 }
169
170 return &mir->registers[index];
171}
172
173void
179
181mir_add_instruction(mir_t* mir, const char* name, uint32_t size)
182{
183 mir_inst_t* inst;
184
188 }
189
191 inst->size = size;
192 inst->fields = NULL;
193 inst->fields_length = 0;
194 inst->_fields_size = 0;
195 inst->parameters = NULL;
196 inst->parameters_length = 0;
197 inst->_parameters_size = 0;
198 dstring_init(&inst->name, 0);
199 dstring_copy(&inst->name, name);
200
202
204 return inst;
205}
206
208mir_get_instruction(mir_t* mir, const char* name)
209{
210 int64_t index;
211
212 if (hashtable_get(&mir->_instructions_index, name, &index) != ERR_OK) {
213 return NULL;
214 }
215
216 return &mir->instructions[index];
217}
218
220mir_instruction_add_param(mir_inst_t* inst, const char* name, mir_type_t type, uint32_t size)
221{
222 mir_inst_param_t* param;
223
224 if (inst->_parameters_size < inst->parameters_length + 1) {
226 inst->parameters = realloc(inst->parameters, sizeof(mir_inst_param_t) * inst->_parameters_size);
227 }
228
229 param = &inst->parameters[inst->parameters_length++];
230 param->size = size;
231 param->type = type;
232 dstring_init(&param->name, 0);
233 dstring_copy(&param->name, name);
234
235 return param;
236}
237
239mir_instruction_get_param(mir_inst_t* inst, const char* name)
240{
241 for (int i = 0; i < inst->parameters_length; i++) {
242 if (strcmp(inst->parameters[i].name.data, name) == 0) {
243 return &inst->parameters[i];
244 }
245 }
246
247 return NULL;
248}
249
252{
253 mir_bitfield_spec_t* field;
254
255 if (inst->_fields_size < inst->fields_length + 1) {
257 inst->fields = realloc(inst->fields, sizeof(mir_bitfield_spec_t) * inst->_fields_size);
258 }
259
260 field = &inst->fields[inst->fields_length++];
261 field->type = type;
262 field->spec = NULL;
263 field->spec_length = 0;
264 field->_spec_size = 0;
265 dstring_init(&field->identifier, 0);
266 dstring_init(&field->name, 0);
267 dstring_copy(&field->name, name);
268
269 return field;
270}
271
273mir_instruction_get_field(mir_inst_t* inst, const char* name)
274{
275 for (int i = 0; i < inst->fields_length; i++) {
276 if (strcmp(inst->fields[i].name.data, name) == 0) {
277 return &inst->fields[i];
278 }
279 }
280
281 return NULL;
282}
283
284void
286{
287 dstring_close(&inst->name);
288
289 for (int i = 0; i < inst->parameters_length; i++) {
290 dstring_close(&inst->parameters[i].name);
291 }
292
293 for (int i = 0; i < inst->fields_length; i++) {
295 }
296
297 free(inst->parameters);
298 free(inst->fields);
299 inst->parameters = NULL;
300 inst->fields = NULL;
301}
302
305{
306 mir_bitfield_spec_t* field;
307
308 if (spec->_spec_size < spec->spec_length + 1) {
310 spec->spec = realloc(spec->spec, sizeof(mir_bitfield_spec_t) * spec->_spec_size);
311 }
312
313 field = &spec->spec[spec->spec_length++];
314 field->type = type;
315 field->spec = NULL;
316 field->spec_length = 0;
317 field->_spec_size = 0;
318 dstring_init(&field->identifier, 0);
319 dstring_init(&field->name, 0);
320 dstring_copy(&field->name, name);
321
322 return field;
323}
324
327{
328 spec->spec = malloc(sizeof(mir_bitfield_spec_t));
329 spec->spec_length = 1;
330 spec->_spec_size = 1;
331
332 spec->spec->type = type;
333 spec->spec->spec = NULL;
334 spec->spec->spec_length = 0;
335 spec->spec->_spec_size = 0;
336 dstring_init(&spec->spec->identifier, 0);
337 dstring_init(&spec->spec->name, 0);
338 dstring_copy(&spec->spec->name, name);
339
340 return spec->spec;
341}
342
345{
346 for (int i = 0; i < spec->spec_length; i++) {
347 if (strcmp(spec->spec[i].name.data, name) == 0) {
348 return &spec->spec[i];
349 }
350 }
351
352 return NULL;
353}
354
355void
357{
358 dstring_close(&spec->name);
360
361 for (int i = 0; i < spec->spec_length; i++) {
362 mir_bitfield_spec_close(&spec->spec[i]);
363 }
364
365 if (spec->spec_length > 0 && spec->spec != NULL) {
366 free(spec->spec);
367 spec->spec = NULL;
368 }
369}
void dstring_init(dstring_t *string, unsigned int buffer_size)
Initializes a dynamic string (dstring).
Definition dstring.c:10
void dstring_copy(dstring_t *string, const char *source)
Copies the content of source to the dstring.
Definition dstring.c:50
void dstring_close(dstring_t *string)
Closes the dynamic string.
Definition dstring.c:18
@ ERR_OK
Definition err.h:15
void hashtable_init(hashtable_t *hashtable, unsigned int size)
Initializes a hashtable.
Definition hashtable.c:18
error_code_t hashtable_get(hashtable_t *hashtable, const char *key, int64_t *value)
Get the value of the specified key inside the hashtable.
Definition hashtable.c:73
void hashtable_close(hashtable_t *hashtable)
Close the given hashtable.
Definition hashtable.c:26
void hashtable_set(hashtable_t *hashtable, const char *key, int64_t value)
Set the value of the specified key inside the hashtable.
Definition hashtable.c:51
mir_register_t * mir_add_register(mir_t *mir, const char *name, uint32_t size)
Add a new register declaration for the specified MIR.
Definition mir.c:136
void mir_bitfield_spec_close(mir_bitfield_spec_t *spec)
Closes the given bitfield spec.
Definition mir.c:356
mir_register_t * mir_get_register(mir_t *mir, const char *name)
Get a register declaration from the specified MIR.
Definition mir.c:162
void mir_instruction_close(mir_inst_t *inst)
Closes the given instruction.
Definition mir.c:285
void mir_bitfield_close(mir_bitfield_t *bitfield)
Closes the given bitfield.
Definition mir.c:124
mir_inst_t * mir_add_instruction(mir_t *mir, const char *name, uint32_t size)
Add a new instruction declaration to MIR.
Definition mir.c:181
void mir_close(mir_t *mir)
Closes the given MIR instruction.
Definition mir.c:30
mir_bitfield_t * mir_get_bitfield(mir_t *mir, const char *name)
Get the bitfield declaration with the given name.
Definition mir.c:82
mir_bitfield_spec_t * mir_bitfield_spec_get_field(mir_bitfield_spec_t *spec, const char *name)
Get a bitfield spec's field from the given bitfield spec.
Definition mir.c:344
mir_bitfield_spec_t * mir_bitfield_spec_set_spec(mir_bitfield_spec_t *spec, const char *name, mir_bitfield_spec_type_t type)
Set the bitfield spec's sub bitfield spec.
Definition mir.c:326
mir_bitfield_spec_t * mir_bitfield_spec_add_field(mir_bitfield_spec_t *spec, const char *name, mir_bitfield_spec_type_t type)
Add a new bitfield spec's field to the given bitfield spec.
Definition mir.c:304
void mir_register_close(mir_register_t *reg)
Closes the given register.
Definition mir.c:174
mir_inst_t * mir_get_instruction(mir_t *mir, const char *name)
Get an instruction declaration.
Definition mir.c:208
void mir_init(mir_t *mir)
Initializes a MIR struct.
Definition mir.c:9
mir_bitfield_field_t * mir_bitfield_add_field(mir_bitfield_t *bitfield, const char *name, uint32_t size)
Add a new field to a bitfield declaration.
Definition mir.c:94
mir_inst_param_t * mir_instruction_add_param(mir_inst_t *inst, const char *name, mir_type_t type, uint32_t size)
Add a new parameter specification to an instruction.
Definition mir.c:220
mir_bitfield_spec_t * mir_instruction_add_field(mir_inst_t *inst, const char *name, mir_bitfield_spec_type_t type)
Add a new body field to the given instruction.
Definition mir.c:251
mir_bitfield_t * mir_add_bitfield(mir_t *mir, const char *name, uint32_t size)
Add a new bitfield declaration.
Definition mir.c:58
mir_inst_param_t * mir_instruction_get_param(mir_inst_t *inst, const char *name)
Get a parameter from an instruction declaration.
Definition mir.c:239
mir_bitfield_spec_t * mir_instruction_get_field(mir_inst_t *inst, const char *name)
Get the body field from the instruction.
Definition mir.c:273
mir_bitfield_field_t * mir_bitfield_get_field(mir_bitfield_t *bitfield, const char *name)
Get the field with the given name from the bitfield declaration.
Definition mir.c:112
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12
uint32_t size
Size in bits of the field.
Definition mir.h:44
dstring_t name
Name of the field.
Definition mir.h:43
mir_bitfield_spec_type_t type
Specify the type of this bitfield specification.
Definition mir.h:70
unsigned int _spec_size
Size of the allocated memory for spec array.
Definition mir.h:75
dstring_t identifier
Bitfield's value identifier.
Definition mir.h:71
struct mir_bitfield_spec * spec
Bitfield's list of field specifications.
Definition mir.h:73
unsigned int spec_length
Number of elements on the spec array.
Definition mir.h:74
dstring_t name
Name of the bitfield.
Definition mir.h:69
mir_bitfield_field_t * fields
Array of fields.
Definition mir.h:54
unsigned int fields_length
Number of elements on the fields array.
Definition mir.h:55
uint32_t size
Size in bits of the bitfield.
Definition mir.h:53
unsigned int _fields_size
Size of the allocated memory for fields array.
Definition mir.h:56
dstring_t name
Name of the bitfield.
Definition mir.h:52
mir_type_t type
Type of the parameter.
Definition mir.h:94
uint32_t size
Size in bits of the parameter.
Definition mir.h:95
dstring_t name
Name of the parameter.
Definition mir.h:93
dstring_t name
Instruction name.
Definition mir.h:103
unsigned int fields_length
Number of elements on the fields array.
Definition mir.h:107
unsigned int _fields_size
Size of the allocated memory for fields array.
Definition mir.h:109
unsigned int _parameters_size
Size of the allocated memory for parameters array.
Definition mir.h:110
mir_inst_param_t * parameters
List of instruction's parameters.
Definition mir.h:105
uint32_t size
Instruction size in bits.
Definition mir.h:104
mir_bitfield_spec_t * fields
Instruction field's specification list.
Definition mir.h:106
unsigned int parameters_length
Number of paramenters on the parameters array.
Definition mir.h:108
uint32_t size
Register size in bits.
Definition mir.h:84
mir_bitfield_spec_t spec
Register's bitfield specification.
Definition mir.h:85
dstring_t name
Register name.
Definition mir.h:83
Mya in-memory intermediate representation.
Definition mir.h:117
unsigned int _instructions_size
The size of the allocated memory for instructions list.
Definition mir.h:128
unsigned int registers_length
The number of declared registers.
Definition mir.h:124
hashtable_t variables
Hashtable of variables.
Definition mir.h:121
unsigned int instructions_length
The number of declared instructions.
Definition mir.h:125
mir_bitfield_t * bitfields
List of bitfields.
Definition mir.h:118
unsigned int bitfields_length
The number of declared bitfields.
Definition mir.h:123
hashtable_t _bitfields_index
Bitfields indexes.
Definition mir.h:130
hashtable_t _instructions_index
Instructions indexes.
Definition mir.h:132
mir_inst_t * instructions
List of instructions.
Definition mir.h:120
unsigned int _registers_size
The size of the allocated memory for registers list.
Definition mir.h:127
unsigned int _bitfields_size
The size of the allocated memory for bitfields list.
Definition mir.h:126
mir_register_t * registers
List of registers.
Definition mir.h:119
hashtable_t _registers_index
Registers indexes.
Definition mir.h:131
struct mir_register mir_register_t
Register declaration.
enum mir_type mir_type_t
struct mir_bitfield_field mir_bitfield_field_t
A bitfield's field declaration.
#define MIR_LIST_INITIAL_SIZE
Definition mir.h:8
enum mir_bitfield_spec_type mir_bitfield_spec_type_t
A bitfield spec type.
#define MIR_REGISTER_HASHTABLE_SIZE
Definition mir.h:12
#define MIR_VARIABLE_HASHTABLE_SIZE
Definition mir.h:13
#define MIR_LIST_INCREMENT_SIZE
Definition mir.h:9
struct mir_inst_param mir_inst_param_t
An instruction's parameter.
struct mir_bitfield mir_bitfield_t
A bitfield declaration.
struct mir_bitfield_spec mir_bitfield_spec_t
A bitfield or bitfield's field declaration.
#define MIR_BITFIELD_HASHTABLE_SIZE
Definition mir.h:10
struct mir_inst mir_inst_t
Instruction declaration.
struct mir mir_t
Mya in-memory intermediate representation.
#define MIR_INSTRUCTION_HASHTABLE_SIZE
Definition mir.h:11