Skip to content

Commit

Permalink
impl label system
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Aug 25, 2024
1 parent d6bf45d commit f11360f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
93 changes: 93 additions & 0 deletions codegen.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "compiler.h"
#include "helpers/vector.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>

Expand Down Expand Up @@ -35,6 +36,94 @@ void asm_push(const char *ins, ...) {
va_end(args);
}

struct code_generator *codegenerator_new(struct compile_process *process) {
struct code_generator *generator = calloc(1, sizeof(struct code_generator));
generator->entry_points = vector_create(sizeof(struct codegen_entry_point *));
generator->exit_points = vector_create(sizeof(struct codegen_exit_point *));
return generator;
}

void codegen_register_exit_point(int exit_point_id) {
struct code_generator *generator = current_process->generator;
struct codegen_exit_point *exit_point =
calloc(1, sizeof(struct codegen_exit_point));
exit_point->id = exit_point_id;
vector_push(generator->exit_points, &exit_point);
}

struct codegen_exit_point *codegen_current_exit_point() {
struct code_generator *generator = current_process->generator;
return vector_back_ptr_or_null(generator->exit_points);
}

int codegen_label_count() {
static int label_count = 0;
return label_count++;
}

void codegen_begin_exit_point() {
int exit_point_id = codegen_label_count();
codegen_register_exit_point(exit_point_id);
}

void codegen_end_exit_point() {
struct code_generator *generator = current_process->generator;
struct codegen_exit_point *exit_point = codegen_current_exit_point();
assert(exit_point);
asm_push(".exit_point_%d:", exit_point->id);
free(exit_point);
vector_pop(generator->exit_points);
}

void codegen_goto_exit_point() {
struct code_generator *generator = current_process->generator;
struct codegen_exit_point *exit_point = codegen_current_exit_point();
asm_push("jmp .exit_point_%d", exit_point->id);
}

void codegen_register_entry_point(int entry_point_id) {
struct code_generator *generator = current_process->generator;
struct codegen_entry_point *entry_point =
calloc(1, sizeof(struct codegen_entry_point));
entry_point->id = entry_point_id;
vector_push(generator->entry_points, &entry_point);
}

struct codegen_entry_point *codegen_current_entry_point() {
struct code_generator *generator = current_process->generator;
return vector_back_ptr_or_null(generator->entry_points);
}

void codegen_begin_entry_point() {
int entry_point_id = codegen_label_count();
codegen_register_entry_point(entry_point_id);
asm_push(".entry_point_%d:", entry_point_id);
}

void codegen_end_entry_point() {
struct code_generator *generator = current_process->generator;
struct codegen_entry_point *entry_point = codegen_current_entry_point();
assert(entry_point);
free(entry_point);
vector_pop(generator->entry_points);
}

void codegen_goto_entry_point() {
struct code_generator *generator = current_process->generator;
struct codegen_entry_point *entry_point = codegen_current_entry_point();
asm_push("jmp .entry_point_%d", entry_point->id);
}

void codegen_begin_entry_exit_point() {
codegen_begin_entry_point();
codegen_begin_exit_point();
}

void codegen_end_entry_exit_point() {
codegen_end_entry_point();
codegen_end_exit_point();
}

static const char *asm_keyword_for_size(size_t size, char *tmp_buf) {
const char *keyword = NULL;
switch (size) {
Expand Down Expand Up @@ -145,5 +234,9 @@ int codegen(struct compile_process *process) {

// generate read only data section
codegen_generate_rod();

codegen_begin_entry_exit_point();
codegen_goto_entry_point();
codegen_end_entry_exit_point();
return 0;
}
22 changes: 22 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ struct scope {
struct scope *parent;
};

struct codegen_entry_point {
// ID of the entry point
int id;
};

struct codegen_exit_point {
// ID of the exit point
int id;
};

struct code_generator {
// vector of struct codegen_entry_point*
struct vector *entry_points;

// vector of struct codegen_exit_point*
struct vector *exit_points;
};

enum {
SYMBOL_TYPE_NODE,
SYMBOL_TYPE_NATIVE_FUNCTION,
Expand Down Expand Up @@ -183,6 +201,9 @@ struct compile_process {
// all symbol tables
struct vector *tables;
} symbols;

// pointer to code generator
struct code_generator *generator;
};

enum { PARSE_ALL_OK, PARSE_GENERAL_ERROR };
Expand Down Expand Up @@ -717,5 +738,6 @@ void *fixup_private(struct fixup *fixup);
bool fixups_resolve(struct fixup_system *system);

int codegen(struct compile_process *process);
struct code_generator *codegenerator_new(struct compile_process *process);

#endif
1 change: 1 addition & 0 deletions cprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct compile_process *compile_process_create(const char *filename,
process->flags = flags;
process->cfile.fp = file;
process->ofile = out_file;
process->generator = codegenerator_new(process);

symresolver_init(process);
symresolver_new_table(process);
Expand Down

0 comments on commit f11360f

Please sign in to comment.