Skip to content

Commit

Permalink
Add debugging flags
Browse files Browse the repository at this point in the history
  • Loading branch information
arlaneenalra committed May 24, 2019
1 parent 1b20734 commit d4fb524
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
60 changes: 44 additions & 16 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ char *target_preamble = " .section __TEXT,__text\n"
" .long 0 # meta_obj.type_def\n"
"str:\n";

char *target_size = "\n"
"str_size :\n"
" .quad ";

char *target_postamble = "\n";

char *target_global_template = ".global _%s\n"
"_%s:\n";

#elif __linux__
char *target_preamble = " .text\n"
" .globl main\n"
Expand All @@ -84,13 +83,17 @@ char *target_preamble = " .text\n"
" .long 0 # meta_obj.type_def\n"
"str:\n";

char *target_size = "\n"
"str_size :\n"
" .quad ";

char *target_postamble = "\n";

char *target_global = ".globl %s\n"
"%s:\n";

#endif

char *target_size = "\n"
"str_size:\n"
" .quad ";

typedef struct options options_type;

struct options {
Expand Down Expand Up @@ -151,15 +154,37 @@ void parse_options(int argc, char **argv, options_type *opts) {
}
}

/* Write an indexed label to a buffer */
void writeLabel(buffer_type *buf, char *prefix, int idx) {
char c[22];
int written = 0;
/* Write a global to the symbol */
void writeGlobalSymbol(FILE *out, char *name) {
(void)fprintf(out, target_global_template, name, name);
}

/* Write debugging information into that output stream */
void writeDebugInfo(FILE *out, debug_info_type *debug) {
char *prefix = "L.debug_files.";

buffer_write(buf, (uint8_t *)prefix, strlen(prefix));
hashtable_type *files = debug->files;

hash_iterator_type *it = 0;
hash_entry_type *entry = 0;
int count = 0;

for(;(entry = hash_next(files, &it)); count++) {
(void)fprintf(out, "%s%i: .asciz \"%s\"\n", prefix, count, entry->key);
}

written = snprintf(c, 22, "%i", idx);
buffer_write(buf, (uint8_t *)c, written);
/* We wind up one over the actual number */
count--;

writeGlobalSymbol(out, "debug_files");
for (int i = count; i >=0 ; i--) {
(void)fprintf(out, " .quad %s%i\n", prefix, i);
}

(void)fputs("\n", out);

writeGlobalSymbol(out, "debug_files_count");
(void)fprintf(out, " .quad %i\n", count);
}

/* Write the assembly output to a file */
Expand Down Expand Up @@ -232,7 +257,10 @@ size_t buildAttachment(gc_type *gc, char *asm_str, char **target) {
(void)fputs(target_size, out_buffer);

/* Output the size */
(void)fprintf(out_buffer, "%zu", length - 1);
(void)fprintf(out_buffer, "%zu\n\n", length - 1);

/* Output debugging information */
writeDebugInfo(out_buffer, debug);

(void)fputs(target_postamble, out_buffer);
(void)fclose(out_buffer);
Expand Down
3 changes: 3 additions & 0 deletions src/libinsomniac_runtime/runtime_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
extern uint64_t scheme_code_size();
extern uint8_t *scheme_code();

extern char *debug_files[];
extern int debug_files_count;

int run_scheme(int argc, char **argv) {
size_t code_size = (size_t)scheme_code_size();
uint8_t *code = scheme_code();
Expand Down

0 comments on commit d4fb524

Please sign in to comment.