libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
mir.h File Reference
#include "types/mir.h"
Include dependency graph for mir.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void mir_init (mir_t *mir)
 Initializes a MIR struct.
 
void mir_close (mir_t *mir)
 Closes the given MIR instruction.
 
mir_bitfield_tmir_add_bitfield (mir_t *mir, const char *name, uint32_t size)
 Add a new bitfield declaration.
 
mir_bitfield_tmir_get_bitfield (mir_t *mir, const char *name)
 Get the bitfield declaration with the given name.
 
mir_bitfield_field_tmir_bitfield_add_field (mir_bitfield_t *bitfield, const char *name, uint32_t size)
 Add a new field to a bitfield declaration.
 
mir_bitfield_field_tmir_bitfield_get_field (mir_bitfield_t *bitfield, const char *name)
 Get the field with the given name from the bitfield declaration.
 
void mir_bitfield_close (mir_bitfield_t *bitfield)
 Closes the given bitfield.
 
mir_register_tmir_add_register (mir_t *mir, const char *name, uint32_t size)
 Add a new register declaration for the specified MIR.
 
mir_register_tmir_get_register (mir_t *mir, const char *name)
 Get a register declaration from the specified MIR.
 
void mir_register_close (mir_register_t *reg)
 Closes the given register.
 
mir_inst_tmir_add_instruction (mir_t *mir, const char *name, uint32_t size)
 Add a new instruction declaration to MIR.
 
mir_inst_tmir_get_instruction (mir_t *mir, const char *name)
 Get an instruction declaration.
 
mir_inst_param_tmir_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.
 
mir_inst_param_tmir_instruction_get_param (mir_inst_t *inst, const char *name)
 Get a parameter from an instruction declaration.
 
mir_bitfield_spec_tmir_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.
 
mir_bitfield_spec_tmir_instruction_get_field (mir_inst_t *inst, const char *name)
 Get the body field from the instruction.
 
void mir_instruction_close (mir_inst_t *inst)
 Closes the given instruction.
 
mir_bitfield_spec_tmir_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.
 
mir_bitfield_spec_tmir_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.
 
mir_bitfield_spec_tmir_bitfield_spec_get_field (mir_bitfield_spec_t *spec, const char *name)
 Get a bitfield spec's field from the given bitfield spec.
 
void mir_bitfield_spec_close (mir_bitfield_spec_t *spec)
 Closes the given bitfield spec.
 

Function Documentation

◆ mir_add_bitfield()

mir_bitfield_t * mir_add_bitfield ( mir_t * mir,
const char * name,
uint32_t size )

Add a new bitfield declaration.

Parameters
mirThe MIR where the bitfield will be added.
nameThe name of the bitfield.
sizeThe size in bits of the bitfield.
Returns
Pointer for the new bitfield declaration.

Definition at line 58 of file mir.c.

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}
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 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_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
Mya in-memory intermediate representation.
Definition mir.h:117
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
unsigned int _bitfields_size
The size of the allocated memory for bitfields list.
Definition mir.h:126
#define MIR_LIST_INCREMENT_SIZE
Definition mir.h:9
struct mir_bitfield mir_bitfield_t
A bitfield declaration.

◆ mir_add_instruction()

mir_inst_t * mir_add_instruction ( mir_t * mir,
const char * name,
uint32_t size )

Add a new instruction declaration to MIR.

Parameters
mirThe MIR where to add the instruction.
nameName of the instruction.
sizeThe size in bits of the instruction.
Returns
Pointer to new added instruction.

Definition at line 181 of file mir.c.

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}
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
unsigned int _instructions_size
The size of the allocated memory for instructions list.
Definition mir.h:128
unsigned int instructions_length
The number of declared instructions.
Definition mir.h:125
hashtable_t _instructions_index
Instructions indexes.
Definition mir.h:132
mir_inst_t * instructions
List of instructions.
Definition mir.h:120
struct mir_inst mir_inst_t
Instruction declaration.

◆ mir_add_register()

mir_register_t * mir_add_register ( mir_t * mir,
const char * name,
uint32_t size )

Add a new register declaration for the specified MIR.

Parameters
mirThe MIR where do add the register declaration.
nameThe name of the register.
sizeThe size in bits of the register.
Returns
Pointer for the new register declaration.

Definition at line 136 of file mir.c.

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}
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
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
unsigned int registers_length
The number of declared registers.
Definition mir.h:124
unsigned int _registers_size
The size of the allocated memory for registers list.
Definition mir.h:127
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.

◆ mir_bitfield_add_field()

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.

Parameters
bitfieldThe bitfield declaration.
nameName of the field.
sizeSize in bits of the field.
Returns
Pointer for the new field.

Definition at line 94 of file mir.c.

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}
uint32_t size
Size in bits of the field.
Definition mir.h:44
dstring_t name
Name of the field.
Definition mir.h:43
struct mir_bitfield_field mir_bitfield_field_t
A bitfield's field declaration.

◆ mir_bitfield_close()

void mir_bitfield_close ( mir_bitfield_t * bitfield)

Closes the given bitfield.

Parameters
bitfieldThe bitfield to be closed.

Definition at line 124 of file mir.c.

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}
void dstring_close(dstring_t *string)
Closes the dynamic string.
Definition dstring.c:18

◆ mir_bitfield_get_field()

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.

Parameters
bitfieldThe bitfield declaration.
nameName of the field.
Returns
Pointer for the field.
NULL when the field doesn't exists.

Definition at line 112 of file mir.c.

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}
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12

◆ mir_bitfield_spec_add_field()

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.

It's used on bitfield specs with fields. Example: Opcode { a = 1, b = 2, }

Where Opcode { ... } itself is a bitfield spec, and a = 1 and b = 2 are bitfield specs too.

Parameters
specThe bitfield spec where to add the field spec.
nameName of the field.
typeType of the field.
Returns
Pointer for the new bitfield spec's field.

Definition at line 304 of file mir.c.

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}
mir_bitfield_spec_type_t type
Specify the type of this bitfield specification.
Definition mir.h:70
struct mir_bitfield_spec mir_bitfield_spec_t
A bitfield or bitfield's field declaration.

◆ mir_bitfield_spec_close()

void mir_bitfield_spec_close ( mir_bitfield_spec_t * spec)

Closes the given bitfield spec.

Parameters
specThe bitfield spec to be closed.

Definition at line 356 of file mir.c.

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 mir_bitfield_spec_close(mir_bitfield_spec_t *spec)
Closes the given bitfield spec.
Definition mir.c:356

◆ mir_bitfield_spec_get_field()

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.

Parameters
specThe bitfield spec where to get the field.
nameName of the field.
Returns
Pointer for the field.
NULL when the field doesn't exists.

Definition at line 344 of file mir.c.

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}

◆ mir_bitfield_spec_set_spec()

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.

This function is used to set types FT_SPEC, when the bitfield spec value is another bitfield spec. Example: a = Reg{2}

When a = Reg{2} itself is a bitfield spec, and Reg{2} is it's value (another bitfield spec).

Parameters
specThe bitfield spec where to set the sub bitfield spec.
nameName of the sub bitfield spec.
typeType of the sub bitfield spec.
Returns
Pointer for the sub bitfield spec.

Definition at line 326 of file mir.c.

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}

◆ mir_close()

void mir_close ( mir_t * mir)

Closes the given MIR instruction.

Parameters
mirThe struct to be closed.

Definition at line 30 of file mir.c.

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}
void hashtable_close(hashtable_t *hashtable)
Close the given hashtable.
Definition hashtable.c:26
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
void mir_register_close(mir_register_t *reg)
Closes the given register.
Definition mir.c:174
hashtable_t variables
Hashtable of variables.
Definition mir.h:121

◆ mir_get_bitfield()

mir_bitfield_t * mir_get_bitfield ( mir_t * mir,
const char * name )

Get the bitfield declaration with the given name.

Parameters
mirThe MIR where to get the bitfield.
nameName of the bitifield.
Returns
Pointer fot the bitfield declaration.
NULL when the bitfield doesn't exists.

Definition at line 82 of file mir.c.

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}
@ ERR_OK
Definition err.h:15
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

◆ mir_get_instruction()

mir_inst_t * mir_get_instruction ( mir_t * mir,
const char * name )

Get an instruction declaration.

Parameters
mirThe MIR where to get the instruction.
nameName of the instruction.
Returns
Pointer for the instruction.
NULL when the instruction doesn't exists.

Definition at line 208 of file mir.c.

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}

◆ mir_get_register()

mir_register_t * mir_get_register ( mir_t * mir,
const char * name )

Get a register declaration from the specified MIR.

Parameters
mirThe MIR where to get the register declaration.
nameThe name of the register.
Returns
Pointer for the register.
NULL when the register doesn't exists.

Definition at line 162 of file mir.c.

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}

◆ mir_init()

void mir_init ( mir_t * mir)

Initializes a MIR struct.

Parameters
mirThe struct to be initialized.

Definition at line 9 of file mir.c.

10{
14
18
22
27}
void hashtable_init(hashtable_t *hashtable, unsigned int size)
Initializes a hashtable.
Definition hashtable.c:18
#define MIR_LIST_INITIAL_SIZE
Definition mir.h:8
#define MIR_REGISTER_HASHTABLE_SIZE
Definition mir.h:12
#define MIR_VARIABLE_HASHTABLE_SIZE
Definition mir.h:13
#define MIR_BITFIELD_HASHTABLE_SIZE
Definition mir.h:10
#define MIR_INSTRUCTION_HASHTABLE_SIZE
Definition mir.h:11

◆ mir_instruction_add_field()

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.

Parameters
instThe instruction where to add the field.
nameName of the field.
typeType of the field.
Returns
Pointer to new added body field.

Definition at line 251 of file mir.c.

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}

◆ mir_instruction_add_param()

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.

Parameters
instThe instruction where to add the parameter.
nameName of the paramameter.
typeType of the parameter.
sizeSize in bits of the parameter.
Returns
Pointer to new added parameter.

Definition at line 220 of file mir.c.

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}
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
struct mir_inst_param mir_inst_param_t
An instruction's parameter.

◆ mir_instruction_close()

void mir_instruction_close ( mir_inst_t * inst)

Closes the given instruction.

Parameters
instInstruction to be closed.

Definition at line 285 of file mir.c.

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}

◆ mir_instruction_get_field()

mir_bitfield_spec_t * mir_instruction_get_field ( mir_inst_t * inst,
const char * name )

Get the body field from the instruction.

Parameters
instThe instruction where to get the field.
nameName of the field.
Returns
Pointer for the field.
NULL when the field doesn't exists.

Definition at line 273 of file mir.c.

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}

◆ mir_instruction_get_param()

mir_inst_param_t * mir_instruction_get_param ( mir_inst_t * inst,
const char * name )

Get a parameter from an instruction declaration.

Parameters
instThe instruction where to get the parameter.
nameThe parameter name.
Returns
Pointer for the parameter.
NULL when the parameter doesn't exists.

Definition at line 239 of file mir.c.

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}

◆ mir_register_close()

void mir_register_close ( mir_register_t * reg)

Closes the given register.

Parameters
regThe register to be closed.

Definition at line 174 of file mir.c.

175{
176 dstring_close(&reg->name);
178}