Skip to content

Commit

Permalink
impl resolver helper functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Aug 27, 2024
1 parent 50f14b1 commit 6c42b93
Show file tree
Hide file tree
Showing 6 changed files with 699 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
OBJECTS= ./build/compiler.o ./build/codegen.o ./build/stackframe.o ./build/array.o ./build/fixup.o ./build/helper.o ./build/scope.o ./build/symresolver.o ./build/cprocess.o ./build/datatype.o ./build/expressionable.o ./build/lexer.o ./build/token.o ./build/lex_process.o ./build/parser.o ./build/node.o ./build/helpers/buffer.o ./build/helpers/vector.o
OBJECTS= ./build/compiler.o ./build/codegen.o ./build/resolver.o ./build/stackframe.o ./build/array.o ./build/fixup.o ./build/helper.o ./build/scope.o ./build/symresolver.o ./build/cprocess.o ./build/datatype.o ./build/expressionable.o ./build/lexer.o ./build/token.o ./build/lex_process.o ./build/parser.o ./build/node.o ./build/helpers/buffer.o ./build/helpers/vector.o
INCLUDES= -I./

all: ${OBJECTS}
Expand All @@ -10,6 +10,9 @@ all: ${OBJECTS}
./build/codegen.o: ./codegen.c
gcc ./codegen.c ${INCLUDES} -o ./build/codegen.o -g -c

./build/resolver.o: ./resolver.c
gcc ./resolver.c ${INCLUDES} -o ./build/resolver.o -g -c

./build/stackframe.o: ./stackframe.c
gcc ./stackframe.c ${INCLUDES} -o ./build/stackframe.o -g -c

Expand Down
23 changes: 17 additions & 6 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ struct resolver_process {
} scopes;

// compile process
struct compile_process *process;
struct compile_process *compile_process;

// resolver callbacks
struct resolver_callbacks callbacks;
Expand Down Expand Up @@ -746,14 +746,11 @@ struct resolver_entity {
// array
struct resolver_array {
// data type of the array
struct datatype *type;
struct datatype type;

// array index node
struct node *array_index_node;

// multiplier for the index
size_t multiplier;

// index of the array
int index;
} array;
Expand Down Expand Up @@ -796,7 +793,7 @@ struct resolver_entity {
} last_resolve;

// data type of the entity
struct datatype *dtype;
struct datatype dtype;

// scope of the entity
struct resolver_scope *scope;
Expand Down Expand Up @@ -858,6 +855,11 @@ enum {
DATA_SIZE_QWORD = 8,
};

enum {
STRUCT_ACCESS_BACKWARDS = 0b00000001,
STRUCT_STOP_AT_POINTER_ACCESS = 0b00000010,
};

enum {
FUNCTION_NODE_FLAG_IS_NATIVE = 0b00000001,
};
Expand Down Expand Up @@ -952,6 +954,7 @@ struct node *variable_struct_or_union_body_node(struct node *node);
struct node *variable_node(struct node *node);
struct node *variable_node_or_list(struct node *node);
bool variable_node_is_primitive(struct node *node);
bool node_is_struct_or_union(struct node *node);
bool node_is_struct_or_union_variable(struct node *node);
bool node_is_expression_or_parentheses(struct node *node);
bool node_is_value_type(struct node *node);
Expand Down Expand Up @@ -999,6 +1002,14 @@ int align_value(int val, int to);
int align_value_treat_positive(int val, int to);
// get the sum of all the paddings of the variables in a vector
int compute_sum_padding(struct vector *vec);
int array_multiplier(struct datatype *dtype, int index, int index_val);
int array_offset(struct datatype *dtype, int index, int index_val);
int struct_offset(struct compile_process *compile_process,
const char *struct_name, const char *var_name,
struct node **var_node_out, int last_pos, int flags);
struct node *
variable_struct_or_union_largest_variable_node(struct node *var_node);
struct node *body_larest_variable_node(struct node *body_node);

void symresolver_init(struct compile_process *process);
void symresolver_new_table(struct compile_process *process);
Expand Down
95 changes: 95 additions & 0 deletions helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,98 @@ struct node *variable_struct_or_union_body_node(struct node *node) {

return NULL;
}

int array_multiplier(struct datatype *dtype, int index, int index_val) {
if (!(dtype->flags & DATATYPE_FLAG_IS_ARRAY)) {
return index_val;
}

vector_set_peek_pointer(dtype->array.brackets->n_brackets, index + 1);
int size_sum = index_val;
struct node *bracket_node =
vector_peek_ptr(dtype->array.brackets->n_brackets);
while (bracket_node) {
assert(bracket_node->bracket.inner->type == NODE_TYPE_NUMBER);
int declared_index = bracket_node->bracket.inner->llnum;
int size_val = declared_index;
size_sum *= size_val;
bracket_node = vector_peek_ptr(dtype->array.brackets->n_brackets);
}

return size_sum;
}

int array_offset(struct datatype *dtype, int index, int index_val) {
if (!(dtype->flags & DATATYPE_FLAG_IS_ARRAY) ||
(index == vector_count(dtype->array.brackets->n_brackets) - 1)) {
return index_val * datatype_element_size(dtype);
}

return array_multiplier(dtype, index, index_val) *
datatype_element_size(dtype);
}

struct node *body_larest_variable_node(struct node *body_node) {
if (!body_node) {
return NULL;
}

if (body_node->type != NODE_TYPE_BODY) {
return NULL;
}

return body_node->body.largest_variable_node;
}

struct node *
variable_struct_or_union_largest_variable_node(struct node *var_node) {
return body_larest_variable_node(
variable_struct_or_union_body_node(var_node));
}

int struct_offset(struct compile_process *compile_process,
const char *struct_name, const char *var_name,
struct node **var_node_out, int last_pos, int flags) {
struct symbol *struct_sym =
symresolver_get_symbol(compile_process, struct_name);
assert(struct_sym && struct_sym->type == SYMBOL_TYPE_NODE);
struct node *node = struct_sym->data;
assert(node_is_struct_or_union(node));

struct vector *struct_vars_vec = node->_struct.body_n->body.statements;
vector_set_peek_pointer(struct_vars_vec, 0);
if (flags & STRUCT_ACCESS_BACKWARDS) {
vector_set_peek_pointer_end(struct_vars_vec);
vector_set_flag(struct_vars_vec, VECTOR_FLAG_PEEK_DECREMENT);
}

struct node *var_node_cur = variable_node(vector_peek_ptr(struct_vars_vec));
struct node *var_node_last = NULL;
int position = last_pos;
*var_node_out = NULL;
while (var_node_cur) {
*var_node_out = var_node_cur;
if (var_node_last) {
position += variable_size(var_node_last);
if (variable_node_is_primitive(var_node_cur)) {
position =
align_value_treat_positive(position, var_node_cur->var.type.size);
} else {
position = align_value_treat_positive(
position,
variable_struct_or_union_largest_variable_node(var_node_cur)
->var.type.size);
}
}

if (S_EQ(var_node_cur->var.name, var_name)) {
break;
}

var_node_last = var_node_cur;
var_node_cur = variable_node(vector_peek_ptr(struct_vars_vec));
}

vector_unset_flag(struct_vars_vec, VECTOR_FLAG_PEEK_DECREMENT);
return position;
}
4 changes: 4 additions & 0 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ struct node *union_node_for_name(struct compile_process *process,
return node;
}

bool node_is_struct_or_union(struct node *node) {
return node->type == NODE_TYPE_STRUCT || node->type == NODE_TYPE_UNION;
}

bool node_is_struct_or_union_variable(struct node *node) {
if (node->type != NODE_TYPE_VARIABLE) {
return false;
Expand Down
Loading

0 comments on commit 6c42b93

Please sign in to comment.