Skip to content

Commit

Permalink
feat: Return apply_result instead of char*
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Oct 14, 2024
1 parent 7f84ee7 commit c9e4ddd
Show file tree
Hide file tree
Showing 18 changed files with 240 additions and 215 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $(BIN): *.c parser/*.c parser/expr/*.c codegen/*.c
$(CC) $(CFLAGS) *.c parser/*.c codegen/*.c parser/expr/*.c

run:
./a.out examples/inp.c > out.asm
./a.out examples/inp4.c --asm > out.asm
nasm -f elf64 out.asm -o out.o
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc -o out out.o
./out
8 changes: 4 additions & 4 deletions parser/expr/assign.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ void assign_debug(int depth, parser_node *node)
}
}

char *assign_apply(parser_node *node, context *ctx)
apply_result *assign_apply(parser_node *node, context *ctx)
{
node_assign *assign = (node_assign *)node->data;

char *val = assign->value->apply(assign->value, ctx);
apply_result *val = assign->value->apply(assign->value, ctx);
symbol *sym = find_symbol(ctx, assign->identity);

add_text(ctx, "mov rax, %s", val);
add_text(ctx, "mov rax, %s", val->code);
char *res = cc_asprintf("[rsp+%u]", sym->offset);
add_text(ctx, "mov %s, rax", res);

return res;
return new_result(res, NULL);
}

parser_node *parse_assign(typed_token **tkns_ptr)
Expand Down
14 changes: 10 additions & 4 deletions parser/expr/deref.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ void deref_debug(int depth, parser_node *node)
ref->var->debug(depth + 1, ref->var);
}

char *deref_apply(parser_node *node, context *ctx)
apply_result *deref_apply(parser_node *node, context *ctx)
{
node_deref *deref = (node_deref *)node->data;

char *loc = deref->var->apply(deref->var, ctx);
apply_result *loc = deref->var->apply(deref->var, ctx);

add_text(ctx, "mov rax, %s", loc);
add_text(ctx, "mov rax, %s", loc->code);
add_text(ctx, "mov rax, [rax]");

return "rax";
symbol *ret = new_temp_symbol(ctx, 8);

char *retloc = cc_asprintf("[rsp+%u]", ret->offset);

add_text(ctx, "mov %s, rax", retloc);

return new_result(retloc, NULL);
}

parser_node *parse_deref(typed_token **tkns_ptr)
Expand Down
32 changes: 16 additions & 16 deletions parser/expr/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ void binary_op_debug(int depth, parser_node *node)
binop->right->debug(depth + 2, binop->right);
}

char *binary_op_apply(parser_node *node, context *ctx)
apply_result *binary_op_apply(parser_node *node, context *ctx)
{
node_binary_op *binop = (node_binary_op *)node->data;
char *left = binop->left->apply(binop->left, ctx);
char *right = binop->right->apply(binop->right, ctx);
add_text(ctx, "mov rax, %s", left);
add_text(ctx, "mov rbx, %s", right);
apply_result *left = binop->left->apply(binop->left, ctx);
apply_result *right = binop->right->apply(binop->right, ctx);
add_text(ctx, "mov rax, %s", left->code);
add_text(ctx, "mov rbx, %s", right->code);

char *l1 = NULL;
char *l2 = NULL;
Expand Down Expand Up @@ -105,7 +105,7 @@ char *binary_op_apply(parser_node *node, context *ctx)
symbol *tmp = new_temp_symbol(ctx, 8);

add_text(ctx, "mov [rsp + %u], rax", tmp->offset);
return cc_asprintf("[rsp + %u]", tmp->offset);
return new_result(cc_asprintf("[rsp + %u]", tmp->offset), NULL);
}

void cond_debug(int depth, parser_node *node)
Expand All @@ -124,25 +124,25 @@ void cond_debug(int depth, parser_node *node)
cond->false_val->debug(depth + 2, cond->false_val);
}

char *cond_apply(parser_node *node, context *ctx)
apply_result *cond_apply(parser_node *node, context *ctx)
{
node_cond *cond = (node_cond *)node->data;
char *cond_res = cond->cond->apply(cond->cond, ctx);
char *yes_val = cond->true_val->apply(cond->true_val, ctx);
char *no_val = cond->false_val->apply(cond->false_val, ctx);
apply_result *cond_res = cond->cond->apply(cond->cond, ctx);
apply_result *yes_val = cond->true_val->apply(cond->true_val, ctx);
apply_result *no_val = cond->false_val->apply(cond->false_val, ctx);
char *l1 = new_label(ctx);
char *l2 = new_label(ctx);
add_text(ctx, "mov rax, %s", cond_res);
add_text(ctx, "mov rax, %s", cond_res->code);
add_text(ctx, "cmp rax, 0");
add_text(ctx, "je %s", l1);
add_text(ctx, "mov rax, %s", yes_val);
add_text(ctx, "mov rax, %s", yes_val->code);
add_text(ctx, "jmp %s", l2);
add_text(ctx, "%s:", l1);
add_text(ctx, "mov rax, %s", no_val);
add_text(ctx, "mov rax, %s", no_val->code);
add_text(ctx, "%s:", l2);
symbol *sym = new_temp_symbol(ctx, 8);
add_text(ctx, "mov [rsp+%u], rax", sym->offset);
return cc_asprintf("[rsp+%u]", sym->offset);
return new_result(cc_asprintf("[rsp+%u]", sym->offset), NULL);
}

void cast_debug(int depth, parser_node *node)
Expand All @@ -158,10 +158,10 @@ void cast_debug(int depth, parser_node *node)
cast->type->debug(depth + 2, cast->type);
}

char *cast_apply(parser_node *node, context *ctx)
apply_result *cast_apply(parser_node *node, context *ctx)
{
node_cast *cast = (node_cast *)node->data;
return cast->val->apply(cast->val, ctx);
return new_result(cast->val->apply(cast->val, ctx)->code, NULL);
}

parser_node *parse_paren(typed_token **tkns_ptr)
Expand Down
8 changes: 4 additions & 4 deletions parser/expr/func_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ void func_call_debug(int depth, parser_node *node)
}
}

char *func_call_apply(parser_node *node, context *ctx)
apply_result *func_call_apply(parser_node *node, context *ctx)
{
node_func_call *call = (node_func_call *)node->data;

char **argvals = (char **)malloc(sizeof(char *) * 6);
for (int i = 0; i < call->num_args; i++)
{
char *regval = call->args[i]->apply(call->args[i], ctx);
apply_result *regval = call->args[i]->apply(call->args[i], ctx);

symbol *tmp = new_temp_symbol(ctx, 8);

char *regname = cc_asprintf("[rsp + %u]", tmp->offset);

add_text(ctx, "mov rax, %s", regval);
add_text(ctx, "mov rax, %s", regval->code);
add_text(ctx, "mov %s, rax", regname);

argvals[i] = regname;
Expand Down Expand Up @@ -66,7 +66,7 @@ char *func_call_apply(parser_node *node, context *ctx)
symbol *tmp = new_temp_symbol(ctx, 8);
add_text(ctx, "mov [rsp + %u], rax", tmp->offset);

return cc_asprintf("[rsp + %u]", tmp->offset);
return new_result(cc_asprintf("[rsp + %u]", tmp->offset), NULL);
}

parser_node *parse_func_call(typed_token **tkns_ptr)
Expand Down
10 changes: 5 additions & 5 deletions parser/expr/literal.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ char *escape(char *inp)
return ret;
}

char *literal_apply(parser_node *node, context *ctx)
apply_result *literal_apply(parser_node *node, context *ctx)
{
node_literal *lit = (node_literal *)node->data;
if (lit->type == TKN_LIT_STR)
{
char *varname = cc_asprintf("__temp_str_%u", ctx->data.count);
add_data(ctx, "%s db `%s`, 0", varname, escape(lit->value));
return varname;
return new_result(varname, NULL);
}
if (lit->type == TKN_LIT_INT)
{
return cc_asprintf("%u", *((int *)lit->value));
return new_result(cc_asprintf("%u", *((int *)lit->value)), NULL);
}
if (lit->type == TKN_LIT_CHAR)
{
return cc_asprintf("%u", (int)(*((char *)lit->value)));
return new_result(cc_asprintf("%u", (int)(*((char *)lit->value))), NULL);
}
return lit->value;
return NULL;
}

void literal_debug(int depth, parser_node *node)
Expand Down
4 changes: 2 additions & 2 deletions parser/expr/ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void ref_debug(int depth, parser_node *node)
ref->var->debug(depth + 1, ref->var);
}

char *ref_apply(parser_node *node, context *ctx)
apply_result *ref_apply(parser_node *node, context *ctx)
{
node_ref *ref = (node_ref *)node->data;
node_var *v = (node_var *)ref->var->data;
Expand All @@ -26,7 +26,7 @@ char *ref_apply(parser_node *node, context *ctx)
add_text(ctx, "mov rax, rsp");
add_text(ctx, "add rax, %u", sym->offset);

return "rax";
return new_result("rax", NULL);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions parser/expr/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void var_debug(int depth, parser_node *node)
printf("Variable(%s)\n", var->var_name);
}

char *var_apply(parser_node *node, context *ctx)
apply_result *var_apply(parser_node *node, context *ctx)
{
node_var *var = (node_var *)node->data;
symbol *sym = find_symbol(ctx, var->var_name);
Expand All @@ -22,7 +22,7 @@ char *var_apply(parser_node *node, context *ctx)
}
else
{
return cc_asprintf("[rsp + %u]", sym->offset);
return new_result(cc_asprintf("[rsp + %u]", sym->offset), NULL);
}
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions parser/for.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "../codegen/codegen.h"
#include "../linked_list.h"

char *for_apply(parser_node *node, context *ctx)
apply_result *for_apply(parser_node *node, context *ctx)
{
node_for *forn = (node_for *)node->data;

Expand All @@ -22,8 +22,8 @@ char *for_apply(parser_node *node, context *ctx)
char *end_for = new_label(ctx);

add_text(ctx, "%s:", start_for);
char *condv = forn->cond->apply(forn->cond, ctx);
add_text(ctx, "mov rax, %s", condv);
apply_result *condv = forn->cond->apply(forn->cond, ctx);
add_text(ctx, "mov rax, %s", condv->code);
add_text(ctx, "cmp rax, 0");
add_text(ctx, "je %s", end_for);
forn->body->apply(forn->body, ctx);
Expand Down
4 changes: 2 additions & 2 deletions parser/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "../codegen/codegen.h"
#include "../linked_list.h"

char *func_decl_apply(parser_node *node, context *ctx)
apply_result *func_decl_apply(parser_node *node, context *ctx)
{
node_func_decl *func = (node_func_decl *)node->data;
char *ext = malloc(128);
Expand All @@ -20,7 +20,7 @@ char *func_decl_apply(parser_node *node, context *ctx)
return NULL;
}

char *func_def_apply(parser_node *node, context *ctx)
apply_result *func_def_apply(parser_node *node, context *ctx)
{
node_func_def *func = (node_func_def *)node->data;

Expand Down
6 changes: 3 additions & 3 deletions parser/if.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "../codegen/codegen.h"
#include "../linked_list.h"

char *if_apply(parser_node *node, context *ctx)
apply_result *if_apply(parser_node *node, context *ctx)
{
node_if *ifn = (node_if *)node->data;
char *ife = ifn->cond->apply(ifn->cond, ctx);
apply_result *ife = ifn->cond->apply(ifn->cond, ctx);

add_text(ctx, "mov rax, 0");

Expand All @@ -23,7 +23,7 @@ char *if_apply(parser_node *node, context *ctx)
end_of_else = new_label(ctx);
}

add_text(ctx, "cmp rax, %s", ife);
add_text(ctx, "cmp rax, %s", ife->code);
add_text(ctx, "je %s", end_of_if);
ifn->body->apply(ifn->body, ctx);
if (ifn->else_body)
Expand Down
9 changes: 9 additions & 0 deletions parser/parser.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdlib.h>

#include "parser.h"

void printtabs(int depth)
Expand All @@ -6,4 +8,11 @@ void printtabs(int depth)
{
printf(" ");
}
}

apply_result *new_result(char *code, parser_node *type) {
apply_result *ret = (apply_result*)malloc(sizeof(apply_result));
ret->code = code;
ret->type = type;
return ret;
}
11 changes: 10 additions & 1 deletion parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@

void printtabs(int depth);

struct apply_result_;

typedef struct parser_node_
{
int type;
void *data;
void (*debug)(int, struct parser_node_ *);
char *(*apply)(struct parser_node_ *, context *ctx);
struct apply_result_ *(*apply)(struct parser_node_ *, context *ctx);
} parser_node;

typedef struct apply_result_ {
char *code;
parser_node *type;
} apply_result;

apply_result *new_result(char *code, parser_node *type);

#endif
2 changes: 1 addition & 1 deletion parser/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void program_debug(int depth, parser_node *node)
printf(")\n");
}

char *program_apply(parser_node *node, context *ctx)
apply_result *program_apply(parser_node *node, context *ctx)
{
node_program *prog = (node_program *)node->data;
for (int i = 0; i < prog->num_functions; i++)
Expand Down
8 changes: 4 additions & 4 deletions parser/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "if.h"
#include "while.h"

char *compound_statement_apply(parser_node *node, context *ctx)
apply_result *compound_statement_apply(parser_node *node, context *ctx)
{
node_compound_statement *comp = (node_compound_statement *)node->data;
int num_syms = ctx->symbol_table.count;
Expand All @@ -35,14 +35,14 @@ void compound_statement_debug(int depth, parser_node *node)
}
}

char *return_apply(parser_node *node, context *ctx)
apply_result *return_apply(parser_node *node, context *ctx)
{
node_return *func = (node_return *)node->data;

if (func->exp)
{
char *val = func->exp->apply(func->exp, ctx);
add_text(ctx, "mov rax, %s", val);
apply_result *val = func->exp->apply(func->exp, ctx);
add_text(ctx, "mov rax, %s", val->code);
}

add_text(ctx, "mov rsp, rbp");
Expand Down
6 changes: 3 additions & 3 deletions parser/var_decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include "../codegen/codegen.h"
#include "../linked_list.h"

char *var_decl_apply(parser_node *node, context *ctx)
apply_result *var_decl_apply(parser_node *node, context *ctx)
{
node_var_decl *decl = (node_var_decl *)node->data;
symbol *sym = new_symbol(ctx, decl->identity, 8);
if (decl->value)
{
char *val = decl->value->apply(decl->value, ctx);
add_text(ctx, "mov rax, %s", val);
apply_result *val = decl->value->apply(decl->value, ctx);
add_text(ctx, "mov rax, %s", val->code);
add_text(ctx, "mov [rsp+%u], rax", sym->offset);
}
return NULL;
Expand Down
Loading

0 comments on commit c9e4ddd

Please sign in to comment.