Skip to content

Commit 299fbc8

Browse files
Putting the entire runtime into the C file is inelegant, so instead we put it into a temporary header file. This means no hassle with environment variables for the compiler. Also compiling the runtime seperately would prevent a load of optimization
1 parent 2793c6d commit 299fbc8

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PREFIX=`echo ~`/.local
44
BINDIR=$(PREFIX)/bin
55
TESTS=$(basename $(wildcard tests/*.cog))
66

7-
cognac: src/cognac.h src/cognac.c src/parser.c src/parser.h src/lexer.c src/runtime.h src/prelude.h src/builtins.c
7+
cognac: src/cognac.h src/cognac.c src/parser.c src/parser.h src/lexer.c src/runtime_bytes.h src/prelude.h src/builtins.c
88
$(CC) $(CFLAGS) src/lexer.c src/parser.c src/cognac.c -o cognac -DCC=$(CC)
99

1010
install: cognac
@@ -14,8 +14,8 @@ install: cognac
1414
uninstall:
1515
rm -rf $(BINDIR)/cognac
1616

17-
src/runtime.h: src/runtime.c
18-
xxd -i src/runtime.c > src/runtime.h
17+
src/runtime_bytes.h: src/runtime.h
18+
xxd -i src/runtime.h > src/runtime_bytes.h
1919

2020
src/prelude.h: src/prelude.cog
2121
xxd -i src/prelude.cog src/prelude.h
@@ -27,7 +27,7 @@ src/parser.c src/parser.h: src/parser.y
2727
bison src/parser.y --defines=src/parser.h -o src/parser.c
2828

2929
clean:
30-
rm src/lexer.c src/parser.c src/parser.h cognac src/runtime.h
30+
rm src/lexer.c src/parser.c src/parser.h cognac src/runtime_bytes.h
3131

3232
test: $(TESTS)
3333

src/cognac.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "cognac.h"
22
#include "parser.h"
3-
#include "runtime.h"
3+
#include "runtime_bytes.h"
44
#include "prelude.h"
55
#include <limits.h>
66
#include <assert.h>
@@ -26,6 +26,8 @@ module_t prelude1 = { .prefix = "prelude" }; // written in C
2626
module_t prelude2 = { .prefix = "prelude" }; // written in Cognate
2727
module_list_t preludes = { .mod=&prelude2, .next = &(module_list_t){.mod=&prelude1, .next=NULL} };
2828

29+
char runtime_filename[] = "/tmp/cognac-runtime-XXXXXX.h";
30+
2931
int usleep (unsigned int);
3032
char* strdup (const char*);
3133

@@ -770,12 +772,14 @@ void to_exe(module_t* mod)
770772
char* c_source_path = strdup(mod->path);
771773
c_source_path[strlen(c_source_path) - 2] = '\0';
772774

775+
printf("source path %s exe_path %s\n", c_source_path, exe_path);
776+
773777
char* args[] =
774778
{
775779
STR(CC), c_source_path, "-o", exe_path,
776780
"-Ofast", "-flto", "-s", "-w",
777781
//"-O0", "-ggdb3", "-g", "-rdynamic",
778-
"-lm", "-Wall", "-Wpedantic", NULL
782+
"-lm", "-Wall", "-Wpedantic", "-Wno-unused", NULL
779783
};
780784
pid_t p = fork();
781785
if (!p) execvp(args[0], args);
@@ -793,6 +797,7 @@ void to_exe(module_t* mod)
793797
fflush(stdout);
794798
}
795799
fputc('\n', stdout);
800+
remove(runtime_filename);
796801
if (status != EXIT_SUCCESS) exit(status);
797802
}
798803

@@ -818,17 +823,21 @@ void c_emit_funcall(func_t* fn, FILE* c_source, reg_dequeue_t* registers)
818823

819824
void to_c(module_t* mod)
820825
{
826+
mkstemps(runtime_filename, 2);
827+
FILE* runtime_file = fopen(runtime_filename, "w");
828+
fprintf(runtime_file, "%.*s", src_runtime_h_len, (char*)src_runtime_h);
829+
fclose(runtime_file);
830+
821831
char* c_source_path = strdup(mod->path);
822832
c_source_path[strlen(c_source_path) - 2] = '\0';
823833
FILE* c_source = fopen(c_source_path, "w");
824-
fprintf(c_source, "%.*s", src_runtime_c_len, (char*)src_runtime_c);
825-
fputc('\n', c_source);
834+
fprintf(c_source, "#include \"%s\"\n\n", runtime_filename);
826835
for (symbol_list_t* syms = mod->symbols ; syms ; syms = syms->next)
827836
fprintf(c_source, "SYMBOL SYM%s = \"%s\";\n", syms->text, syms->text);
828837
if (mod->symbols) fputc('\n', c_source);
829838
for (func_list_t* func = mod->funcs ; func ; func = func->next)
830839
{
831-
fprintf(c_source, "%s %s(",
840+
fprintf(c_source, "static %s %s(",
832841
func->func->returns ? c_val_type(func->func->rettype) : "void",
833842
func->func->name);
834843
if (!func->func->generic) for (word_list_t* w = func->func->captures ; w ; w = w->next)
@@ -858,7 +867,7 @@ void to_c(module_t* mod)
858867
for (func_list_t* func = mod->funcs ; func ; func = func->next)
859868
{
860869
//size_t num_words = 0;
861-
fprintf(c_source, "%s %s(",
870+
fprintf(c_source, "static %s %s(",
862871
func->func->returns ? c_val_type(func->func->rettype) : "void",
863872
func->func->name);
864873
if (!func->func->generic)

src/runtime.c renamed to src/runtime.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ static const char* restrict function_stack_start;
182182

183183
// Variables and needed by functions.c defined in runtime.c
184184
static void init_stack(void);
185-
static void expand_stack(void);
186185
static STRING show_object(const ANY object, const _Bool);
187186
static void _Noreturn __attribute__((format(printf, 1, 2))) throw_error_fmt(const char* restrict const, ...);
188187
static void _Noreturn throw_error(const char* restrict const);
@@ -223,7 +222,6 @@ static ANY box_DICT(DICT);
223222
static NUMBER radians_to_degrees(NUMBER);
224223
static NUMBER degrees_to_radians(NUMBER);
225224

226-
static void init(int, char **);
227225
static void cleanup(void);
228226
static void push(ANY);
229227
static ANY pop(void);
@@ -233,7 +231,6 @@ static int stack_length(void);
233231

234232
// Builtin functions needed by compiled source file defined in functions.c
235233
static DICT ___insert(STRING, ANY, DICT);
236-
static DICT ___emptyDdict(void);
237234
static LIST ___empty(void);
238235
static ANY ___if(BOOLEAN, ANY, ANY);
239236
static void ___put(ANY);
@@ -281,7 +278,6 @@ static NUMBER ___stringDlength(STRING);
281278
static STRING ___substring(NUMBER, NUMBER, STRING);
282279
static STRING ___input(void);
283280
static IO ___open(STRING, STRING);
284-
static void ___with(STRING, STRING, BLOCK);
285281
static void ___close(IO);
286282
static NUMBER ___number(STRING);
287283
static STRING ___path(void);
@@ -296,8 +292,6 @@ static NUMBER ___floor(NUMBER);
296292
static NUMBER ___round(NUMBER);
297293
static NUMBER ___ceiling(NUMBER);
298294
static void ___error(STRING);
299-
static LIST ___filter(BLOCK, LIST);
300-
static ANY ___index(NUMBER, LIST);
301295
//static BLOCK ___precompute(BLOCK);
302296
static void ___wait(NUMBER);
303297
static LIST ___split(STRING, STRING);
@@ -343,7 +337,7 @@ static size_t debug_lineno = 0;
343337
static int _argc;
344338
static char** _argv;
345339

346-
void fn0();
340+
static void fn0();
347341

348342
int main(int argc, char** argv)
349343
{
@@ -622,6 +616,7 @@ static STRING show_object (const ANY object, const _Bool raw_strings)
622616
{
623617
case NIL: throw_error("This shouldn't happen");
624618
break;
619+
case dict: // TODO
625620
case number: sprintf(buffer, "%.14g", object.number);
626621
buffer += strlen(buffer);
627622
break;
@@ -1174,7 +1169,6 @@ static NUMBER ___modulo(NUMBER a, NUMBER b) { return b - a * floor(b / a); }
11741169
static NUMBER ___sqrt(NUMBER a) { return sqrt(a); }
11751170
static NUMBER ___random(NUMBER low, NUMBER high)
11761171
{
1177-
NUMBER step = 1;
11781172
if unlikely((high - low) < 0) goto invalid_range;
11791173
else if (high - low < 1) return low;
11801174
// This is not cryptographically secure btw.
@@ -1818,7 +1812,6 @@ static void invalid_jump(void* env)
18181812

18191813
static void oh_no(void* env)
18201814
{
1821-
char a;
18221815
longjmp(*(jmp_buf*)env, 1);
18231816
}
18241817

@@ -1918,7 +1911,7 @@ static ANY ___get(STRING key, DICT d)
19181911

19191912
if (diff == 0) return d->value;
19201913
else if (diff > 0) return ___get(key, d->child1);
1921-
else if (diff < 0) return ___get(key, d->child2);
1914+
else return ___get(key, d->child2);
19221915
}
19231916

19241917
// ---------- ACTUAL PROGRAM ----------

0 commit comments

Comments
 (0)