added basics of timing the different stages of compilation
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 25 Sep 2020 03:51:55 +0000 (22:51 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 25 Sep 2020 03:51:55 +0000 (22:51 -0500)
Makefile
include/bh.h
misc/onyx.vim
onyx
src/onyx.c

index 302e4efacfafe4c04903efe4c488a7423fce42e1..b073230eeda89f834f9f85fed50c0385e357bb00 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
 RELEASE=1
+TIME=0
 
 OBJ_FILES=\
        build/onyxlex.o \
@@ -25,6 +26,10 @@ else
 endif
 endif
 
+ifeq ($(TIME), 1)
+       TIMEFLAG=-DREPORT_TIMES=1
+endif
+
 INCLUDES=-I./include
 LIBS=
 TARGET=./onyx
@@ -40,10 +45,10 @@ else
 endif
 
 build/%.o: src/%.c include/bh.h
-       $(CC) $(FLAGS) -c $< -o $@ $(INCLUDES)
+       $(CC) $(TIMEFLAG) $(FLAGS) -c $< -o $@ $(INCLUDES)
 
 $(TARGET): $(OBJ_FILES)
-       $(CC) $(FLAGS) $(OBJ_FILES) -o $@ $(LIBS)
+       $(CC) $(TIMEFLAG) $(FLAGS) $(OBJ_FILES) -o $@ $(LIBS)
 
 install: $(TARGET) core/*
        cp $(TARGET) /usr/bin/
index ffb5379f2d6fbc95ddacfa3a174b95b077e7fc66..c179531b4dd5d57bf5e96647f7d92943b0a86178 100644 (file)
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <malloc.h>
+#include <time.h>
 
 #include <stdlib.h>
 #include <stdarg.h>
@@ -769,6 +770,12 @@ BH_ALLOCATOR_PROC(bh_managed_heap_allocator_proc);
 
 
 
+//------------------------------------------------------------------------------
+// TIME / DURATION
+//------------------------------------------------------------------------------
+u64 bh_time_curr();
+u64 bh_time_duration(u64 old);
+
 
 
 
@@ -2263,6 +2270,31 @@ void bh_imap_clear(bh_imap* imap) {
 
 #endif // ifndef BH_NO_IMAP
 
+
+
+
+
+
+
+u64 bh_time_curr() {
+    struct timespec spec;
+    clock_gettime(CLOCK_MONOTONIC, &spec);
+
+    time_t sec = spec.tv_sec;
+    u64 ms  = spec.tv_nsec / 1000000;
+    if (ms > 999) {
+        sec++;
+        ms = 0;
+    }
+
+    return sec * 1000 + ms;
+}
+
+u64 bh_time_duration(u64 old) {
+    u64 curr = bh_time_curr();
+    return curr - old;
+}
+
 #endif // ifdef BH_DEFINE
 
 #endif // ifndef BH_H
index 2afb167e7ae15338941c397e10a9db91e6b44dff..e06ab001bd60d0a90e94220582e636b4bf683ff0 100644 (file)
@@ -37,7 +37,7 @@ syn keyword onyxCommentStart    contained TODO NOTE BUG HACK
 syn region onyxComment          start="//" end="$" keepend contains=onyxCommentStart
 syn region onyxComment          start="/\*" end="\*/" contains=onyxCommentStart
 
-syn match onyxDefinitionGroup   "\<[a-zA-Z_][a-zA-Z0-9_]*\> *::" contains=onyxDefinition
+syn match onyxDefinitionGroup   "\<[a-zA-Z_][a-zA-Z0-9_]*\> *:" contains=onyxDefinition
 syn match onyxDefinition        "\<[a-zA-Z_][a-zA-Z0-9_]*\>" contained
 
 syn match onyxDirective         "\#[a-zA-Z_]\+"
diff --git a/onyx b/onyx
index 70b0a6920e7fb5882647712aa03d017298cdd3bb..2579a91eb8674956fb5d1e13956a91bd4dfd259b 100755 (executable)
Binary files a/onyx and b/onyx differ
index 5036c03c8d56c1e8d2000daee94afbd755cbdf1e..a5a6d7896b19edd38d7cf251faf22b8ae27220b3 100644 (file)
 #endif
 
 
+
+
+#ifdef REPORT_TIMES
+// CLEANUP: Move this to another file
+typedef struct TimerStackElem {
+    char* label;
+    u64   time;
+} TimerStackElem;
+
+static bh_arr(TimerStackElem) global_timer_stack;
+
+void timer_stack_init() {
+    global_timer_stack = NULL;
+    bh_arr_new(global_heap_allocator, global_timer_stack, 4);
+}
+
+void timer_stack_push(char* label) {
+    TimerStackElem elem;
+    elem.label = label;
+    elem.time  = bh_time_curr();
+    bh_arr_push(global_timer_stack, elem);
+}
+
+void timer_stack_pop() {
+    TimerStackElem elem = bh_arr_pop(global_timer_stack);
+    u64 duration = bh_time_duration(elem.time);
+    bh_printf("[Time] %s took %lms.\n", elem.label, duration);
+}
+#endif
+
+
+
 static const char* docstring = "Onyx compiler version " VERSION "\n"
     "\n"
     "The compiler for the Onyx programming language.\n"
@@ -329,6 +361,10 @@ static void merge_parse_results(CompilerState* compiler_state, ParseResults* res
 static CompilerProgress process_source_file(CompilerState* compiler_state, char* filename) {
     if (bh_table_has(bh_file_contents, compiler_state->loaded_files, filename)) return ONYX_COMPILER_PROGRESS_SUCCESS;
 
+#ifdef REPORT_TIMES
+    timer_stack_push(bh_aprintf(global_heap_allocator, "Parsing '%s'", filename));
+#endif
+
     bh_file file;
 
     bh_file_error err = bh_file_open(&file, filename);
@@ -360,6 +396,10 @@ static CompilerProgress process_source_file(CompilerState* compiler_state, char*
     ParseResults results = parse_source_file(compiler_state, &fc);
     merge_parse_results(compiler_state, &results);
 
+#ifdef REPORT_TIMES
+    timer_stack_pop();
+#endif
+
     if (onyx_has_errors()) {
         return ONYX_COMPILER_PROGRESS_FAILED_PARSE;
     } else {
@@ -370,6 +410,10 @@ static CompilerProgress process_source_file(CompilerState* compiler_state, char*
 
 static i32 onyx_compile(CompilerState* compiler_state) {
 
+#ifdef REPORT_TIMES
+    timer_stack_push("Parsing");
+#endif
+
     // NOTE: While the queue is not empty, process the next file
     while (!bh_arr_is_empty(compiler_state->queued_files)) {
         CompilerProgress result = process_source_file(compiler_state, (char *) compiler_state->queued_files[0]);
@@ -380,6 +424,13 @@ static i32 onyx_compile(CompilerState* compiler_state) {
         bh_arr_fastdelete(compiler_state->queued_files, 0);
     }
 
+#ifdef REPORT_TIMES
+    timer_stack_pop();
+    
+    timer_stack_push("Checking semantics");
+    timer_stack_push("Initializing builtins");
+#endif
+
     initialize_builtins(compiler_state->ast_alloc, &compiler_state->prog_info);
     if (onyx_has_errors()) {
         return ONYX_COMPILER_PROGRESS_FAILED_SEMPASS;
@@ -400,6 +451,10 @@ static i32 onyx_compile(CompilerState* compiler_state) {
             sizeof(Entity),
             sort_entities);
 
+#ifdef REPORT_TIMES
+    timer_stack_pop();
+#endif
+
     // NOTE: Check types and semantic rules
     if (compiler_state->options->verbose_output)
         bh_printf("[Checking semantics]\n");
@@ -407,6 +462,10 @@ static i32 onyx_compile(CompilerState* compiler_state) {
     onyx_sempass_init(compiler_state->sp_alloc, compiler_state->ast_alloc);
     onyx_sempass(&compiler_state->prog_info);
 
+#ifdef REPORT_TIMES
+    timer_stack_pop();
+#endif
+
     if (onyx_has_errors()) {
         return ONYX_COMPILER_PROGRESS_FAILED_SEMPASS;
     }
@@ -423,6 +482,11 @@ static i32 onyx_compile(CompilerState* compiler_state) {
     if (compiler_state->options->verbose_output)
         bh_printf("[Generating WASM]\n");
 
+#ifdef REPORT_TIMES
+    timer_stack_push("Code generation");
+    timer_stack_push("Generating WASM");
+#endif
+
     compiler_state->wasm_mod = onyx_wasm_module_create(compiler_state->options->allocator);
     onyx_wasm_module_compile(&compiler_state->wasm_mod, &compiler_state->prog_info);
 
@@ -430,6 +494,10 @@ static i32 onyx_compile(CompilerState* compiler_state) {
         return ONYX_COMPILER_PROGRESS_FAILED_BINARY_GEN;
     }
 
+#ifdef REPORT_TIMES
+    timer_stack_pop();
+    timer_stack_push("Write to file");
+#endif
 
     // NOTE: Output to file
     bh_file output_file;
@@ -442,6 +510,11 @@ static i32 onyx_compile(CompilerState* compiler_state) {
 
     onyx_wasm_module_write_to_file(&compiler_state->wasm_mod, output_file);
 
+#ifdef REPORT_TIMES
+    timer_stack_pop();
+    timer_stack_pop();
+#endif
+
     return ONYX_COMPILER_PROGRESS_SUCCESS;
 }