Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions .github/test.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/*
** EPITECH PROJECT, 2026
** mncc
** test.c
** File description:
** test
** Test file for the MNCC project
*/

//this file is used to test the lexer and the tokenization of the input string

#include "main.h"
#include "lexer/lexer.h"
#include "lexer/token.h"
#include <stdio.h>
#include <stdlib.h>

int main()
int get_ages_addition()
{
return 4;
int william = 18;
int alessandro = 18;
return william + alessandro;
}

//again a test file to test the lexer and the tokenization of the input string
int main()
{
int factor = 0;
int diff = 3;

factor = 2;
return diff + get_ages_addition() * factor;
}
12 changes: 4 additions & 8 deletions include/gen/gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@
#include "parser/parser.h"
#include "utils/array.h"

typedef struct stack_data_s {
typedef struct variable_s {
char *name;
int offset;
} stack_data_t;

typedef struct stack_s {
array_t *variables;
struct stack_s *parent;
} stack_t;
} variable_t;

typedef struct gen_s {
parser_t *parser;
FILE *out;
stack_t *stack;
array_t *variables;
} gen_t;

gen_t *gen_create(char *filename, parser_t *parser);
Expand All @@ -39,6 +34,7 @@ int gen_declaration(gen_t *gen, node_t *node);
int gen_assignement(gen_t *gen, node_t *node);
int gen_return(gen_t *gen, node_t *node);
int gen_block(gen_t *gen, node_t *node);
int gen_call(gen_t *gen, node_t *node);

// expression generation
int gen_expression(gen_t *gen, node_t *node);
Expand Down
1 change: 1 addition & 0 deletions include/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ node_t *parse_assignment(parser_t *parser);
node_t *parse_function(parser_t *parser);
node_t *parse_declaration(parser_t *parser);
node_t *parse_block(parser_t *parser);
node_t *parse_call(parser_t *parser);

// expression
node_t *parse_expression(parser_t *parser);
Expand Down
23 changes: 23 additions & 0 deletions src/gen/expression/gen_call.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
** EPITECH PROJECT, 2026
** gen_value.c
** File description:
** Generation of value nodes to assembly code
*/
#include <stdio.h>
#include "gen/gen.h"
#include "main.h"
#include "parser/node.h"

int gen_call(gen_t *gen, node_t *node)
{
for (int i = node->childs->count - 1; i >= 0; i--) {
if (gen_expression(gen, node->childs->data[i]) != SUCCESS)
return ERROR;
fprintf(gen->out, " push rax\n");
}
fprintf(gen->out, " call %s\n", node->name);

fprintf(gen->out, " add rsp, 0x%X\n", node->childs->count * 8);
return SUCCESS;
}
4 changes: 3 additions & 1 deletion src/gen/expression/gen_expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ int gen_expression(gen_t *gen, node_t *node)
return gen_value(gen, node);
case NODE_OPERATOR:
return gen_operator(gen, node);
case NODE_CALL:
return gen_call(gen, node);
default:
return get_error(EPAR,
return get_error(EGEN,
"unsupported node type in expression: '%s'",
node->name);
}
Expand Down
11 changes: 8 additions & 3 deletions src/gen/expression/gen_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ int gen_operator(gen_t *gen, node_t *node)
char *op_str = get_operator_str(node->op);

if (!op_str) {
get_error(EPAR, "invalid operator in code generation");
get_error(EGEN, "invalid operator in code generation");
return ERROR;
}
gen_expression(gen, node->left);
fprintf(gen->out, " push rax\n");
gen_expression(gen, node->right);
fprintf(gen->out, " push rax\n");
gen_expression(gen, node->left);
fprintf(gen->out, " pop rbx\n");
if (node->op == OP_DIV) {
fprintf(gen->out, " xor rdx, rdx\n");
fprintf(gen->out, " div rbx\n");
return SUCCESS;
}
fprintf(gen->out, " %s rax, rbx\n", op_str);
return SUCCESS;
}
18 changes: 11 additions & 7 deletions src/gen/expression/gen_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,33 @@

static int get_offset(gen_t *gen, const char *name)
{
stack_data_t *data = NULL;
variable_t *data = NULL;

for (int i = 0; i < gen->stack->variables->count; i++) {
data = gen->stack->variables->data[i];
for (int i = 0; i < gen->variables->count; i++) {
data = gen->variables->data[i];
if (strcmp(data->name, name) == 0)
return data->offset;
}
get_error(EPAR, "variable '%s' not found in stack", name);
get_error(EGEN, "variable '%s' not found in stack", name);
return -1;
}

int gen_value(gen_t *gen, node_t *node)
{
int offset = 0;

if (node->type == NODE_CONST)
if (node->type == NODE_CONST) {
fprintf(gen->out, " mov rax, %d\n", node->value);
return SUCCESS;
return SUCCESS;
}
if (node->type == NODE_VAR) {
offset = get_offset(gen, node->name);
if (offset < 0)
return ERROR;
fprintf(gen->out, " mov rax, [rbp - %d]\n", offset);
if (offset > 0)
fprintf(gen->out, " mov rax, [rbp - %d]\n", offset);
else
fprintf(gen->out, " mov rax, [rbp]\n");
}
return SUCCESS;
}
22 changes: 1 addition & 21 deletions src/gen/gen_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@
#include "gen/gen.h"
#include "utils/utils.h"

static stack_t *stack_create(void)
{
stack_t *stack = malloc(sizeof(stack_t));

if (!stack)
return NULL;
stack->parent = NULL;
stack->variables = array_create((array_element_destroy_t)free);
if (!stack->variables) {
free(stack);
get_error(ENOMEM, "code generation stack allocation");
return NULL;
}
return stack;
}

static FILE *open_file(const char *filename)
{
FILE *out = fopen(filename, "w");
Expand All @@ -50,10 +34,6 @@ gen_t *gen_create(char *filename, parser_t *parser)
free(gen);
return NULL;
}
gen->stack = stack_create();
if (!gen->stack) {
free(gen);
return NULL;
}
gen->variables = NULL;
return gen;
}
11 changes: 0 additions & 11 deletions src/gen/gen_destroy.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,13 @@
** File description:
** Generation context destruction and memory cleanup
*/
#include <errno.h>
#include <stdlib.h>
#include "gen/gen.h"
#include "utils/array.h"

void gen_destroy(gen_t *gen)
{
stack_t *stack = NULL;

if (!gen)
return;
while (gen->stack) {
if (gen->stack->variables)
array_destroy(gen->stack->variables, 1);
stack = gen->stack->parent;
free(gen->stack);
gen->stack = stack;
}
if (gen->out)
fclose(gen->out);
free(gen);
Expand Down
16 changes: 15 additions & 1 deletion src/gen/gen_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@
** File description:
** Generation of function node to assembly code
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "gen/gen.h"
#include "main.h"
#include "parser/node.h"
#include "utils/utils.h"

int gen_function(gen_t *gen, node_t *node)
{
variable_t *data = NULL;

gen->variables = array_create((array_element_destroy_t)free);
if (!gen->variables)
return get_error(ENOMEM, "generator function variables allocation");
fprintf(gen->out, "%s:\n", node->name);
fprintf(gen->out, " push rbp\n mov rbp, rsp\n");
gen_block(gen, node->childs->data[0]);
fprintf(gen->out, " pop rbp\n ret\n");
fprintf(gen->out, " leave\n ret\n");
fprintf(gen->out, "\n");
for (int i = 0; i < gen->variables->count; i++) {
data = gen->variables->data[i];
free(data->name);
}
array_destroy(gen->variables, 1);
gen->variables = NULL;
return SUCCESS;
}
1 change: 0 additions & 1 deletion src/gen/gen_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

int gen_header(gen_t *gen)
{
printf("header\n");
if (fprintf(gen->out, "section .text\n") < 0)
return ERROR;
if (fprintf(gen->out, "\t\tglobal _start\n\n") < 0)
Expand Down
2 changes: 1 addition & 1 deletion src/gen/gen_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int gen_run(gen_t *gen)
if (node->type == NODE_FUNCTION && gen_function(gen, node) != SUCCESS)
return ERROR;
if (node->type != NODE_FUNCTION) {
get_error(EPAR,
get_error(EGEN,
"unsupported node type in global scope: '%s'",
node->name);
return ERROR;
Expand Down
14 changes: 7 additions & 7 deletions src/gen/instruction/gen_assignement.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@

static int get_offset(gen_t *gen, const char *name)
{
stack_data_t *data = NULL;
variable_t *data = NULL;

for (int i = 0; i < gen->stack->variables->count; i++) {
data = gen->stack->variables->data[i];
for (int i = 0; i < gen->variables->count; i++) {
data = gen->variables->data[i];
if (strcmp(data->name, name) == 0)
return data->offset;
}
get_error(EPAR, "variable '%s' not found in stack", name);
get_error(EGEN, "variable '%s' not found in stack", name);
return -1;
}

int gen_assignement(gen_t *gen, node_t *node)
{
int offset = get_offset(gen, node->name);
int offset = get_offset(gen, node->left->name);

if (offset < 0)
return ERROR;
if (gen_expression(gen, node->childs->data[0]) != SUCCESS)
if (gen_expression(gen, node->right) != SUCCESS)
return ERROR;
fprintf(gen->out, " mov [rbp - %d], rax\n", offset);
fprintf(gen->out, " mov [rbp - 0x%X], rax\n", offset);
return SUCCESS;
}
39 changes: 33 additions & 6 deletions src/gen/instruction/gen_declaration.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,56 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gen/gen.h"
#include "main.h"
#include "parser/node.h"
#include "utils/utils.h"

static int get_offset(gen_t *gen, const char *name)
{
variable_t *data = NULL;

for (int i = 0; i < gen->variables->count; i++) {
data = gen->variables->data[i];
if (strcmp(data->name, name) == 0)
return data->offset;
}
get_error(EGEN, "variable '%s' not found in stack", name);
return -1;
}

static int create_variable(gen_t *gen, char *name)
{
stack_data_t *data = malloc(sizeof(stack_data_t));
variable_t *data = malloc(sizeof(variable_t));

if (!data)
return get_error(ENOMEM, "stack variable allocation");
data->name = name;
data->offset = gen->stack->variables->count * 8;
array_push(gen->stack->variables, data);
data->name = strdup(name);
data->offset = (gen->variables->count + 1) * 8;
array_push(gen->variables, data);
return SUCCESS;
}

int gen_declaration(gen_t *gen, node_t *node)
{
int offset = 0;

fprintf(gen->out, " sub rsp, 8\n");
if (create_variable(gen, node->name) != SUCCESS)
if (!node->left || !node->left->name)
return get_error(EGEN, "declaration node missing variable name");
if (create_variable(gen, node->left->name) != SUCCESS)
return ERROR;
if (node->childs->count > 0)
if (!node->right) {
offset = get_offset(gen, node->left->name);
if (offset < 0)
return ERROR;
if (offset > 0)
fprintf(gen->out, " mov [rbp - 0x%X], 0\n", offset);
else
fprintf(gen->out, " mov [rbp], 0\n");
}
if (node->right)
return gen_assignement(gen, node);
return SUCCESS;
}
2 changes: 1 addition & 1 deletion src/gen/instruction/gen_instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int gen_instruction(gen_t *gen, node_t *node)
case NODE_BLOCK:
return gen_block(gen, node);
default:
return get_error(EPAR,
return get_error(EGEN,
"unsupported node type in instruction: '%s'",
node->name);
}
Expand Down
Loading
Loading