diff options
| author | Tom Harley | 2020-01-08 14:14:39 +0000 |
|---|---|---|
| committer | Tom Harley | 2020-01-08 14:14:39 +0000 |
| commit | 4cdc57ce8041aecabd3175f2dff176b115f17724 (patch) | |
| tree | da9a25e2d18ce5f4415d4c61441962e9e06ee727 | |
| parent | checkpoint (diff) | |
| download | sinter-4cdc57ce8041aecabd3175f2dff176b115f17724.tar.gz sinter-4cdc57ce8041aecabd3175f2dff176b115f17724.zip | |
I should commit more
| -rw-r--r-- | common.c | 2 | ||||
| -rw-r--r-- | examples/function.sin | 76 | ||||
| -rw-r--r-- | examples/list.sin | 53 | ||||
| -rw-r--r-- | examples/nq/Makefile | 27 | ||||
| -rw-r--r-- | examples/nq/closures.sin | 47 | ||||
| -rw-r--r-- | examples/nq/either.sin | 37 | ||||
| -rw-r--r-- | examples/nq/glue.c | 44 | ||||
| -rw-r--r-- | examples/nq/list.sin | 94 | ||||
| -rw-r--r-- | examples/nq/nqueens.sin | 83 | ||||
| -rw-r--r-- | examples/nq/repeat.h | 70 | ||||
| -rw-r--r-- | examples/nq/run_func.c | 79 | ||||
| -rw-r--r-- | examples/nq/util.sin | 15 | ||||
| -rw-r--r-- | llvm_codegen.c | 888 | ||||
| -rw-r--r-- | scope.c | 6 | ||||
| -rw-r--r-- | scope.h | 13 |
15 files changed, 1303 insertions, 231 deletions
@@ -1,5 +1,6 @@ #include <stddef.h> #include <stdio.h> +#include <assert.h> #include <gc.h> @@ -42,5 +43,6 @@ int main() { int *ret = (int *) inter_main(); + assert(ret); return *ret; } diff --git a/examples/function.sin b/examples/function.sin deleted file mode 100644 index 461b9e2..0000000 --- a/examples/function.sin +++ /dev/null @@ -1,76 +0,0 @@ -[type hunk [func args_req arg_c args]] - -[dec run_func [f v as]] - -[dec list [x n]] -[dec nil []] -[dec is_nil [x]] -[dec append [xs x]] -[dec head [xs]] -[dec tail [xs]] - -[dec left [x]] -[dec right [x]] -[dec is_right [x]] -[dec right.val [x]] -[dec left.val [x]] - -[def = [a b] - [if [- a b] - 0 - 1]] - -[def add_arg [h a] - [hunk [hunk.func h] - [hunk.args_req h] - [+ [hunk.arg_c h] 1] - [append [hunk.args h] a]]] - -[def run_hunk [h] - [run_func [hunk.func h] [hunk.args_req h] [hunk.args h]]] - -[def maybe_run_hunk [h] - [if [= [hunk.args_req h] [hunk.arg_c h]] - [left [run_hunk h]] - [right h]]] - -[def add_arg_and_maybe_run [h a] - [maybe_run_hunk [add_arg h a]]] - -[def !1 [h a] - [add_arg_and_maybe_run h a]] - -[def swap_args [f a b] - [f b a]] - -[def make_hunk [f v] - [if v - [right [hunk f v 0 [nil]]] - [left [run_func f 0 [nil]]]]] - -[def !> [h a] - [if [is_right h] - [!1 [right.val h] a] - h]] - -[dec add_args_and_maybe_run_rev [as h]] - -[def add_args_and_maybe_run [h as] - [if [is_nil as] - h - [add_args_and_maybe_run [!> h [head as]] - [tail as]]]] - -[def ! [h as] - [add_args_and_maybe_run h as]] - -[def countdown [x] - [if x - [list x [countdown [- x 1]]] - [nil]]] - -[def mult [x y] - [* x y]] - -[def inter_main [] - [debug_int [left.val [! [make_hunk mult 2] [countdown 10]]]]] diff --git a/examples/list.sin b/examples/list.sin deleted file mode 100644 index 5952ae3..0000000 --- a/examples/list.sin +++ /dev/null @@ -1,53 +0,0 @@ -[type list_ [kind head tail]] -[type nil_ [kind]] - -[def list [head tail] - [list_ 1 head tail]] - -[def nil [] - [nil_ 0]] - -[def head [xs] - [list_.head xs]] - -[def tail [xs] - [list_.tail xs]] - -[def has_tail [xs] - [nil_.kind xs]] - -[def is_nil [x] - [if [nil_.kind x] - 0 - 1]] - -[def concat [as bs] - [list [head as] - [if [is_nil [tail as]] - bs - [concat [tail as] bs]]]] - -[def alt_concat [as bs] - [if [is_nil [tail as]] - [list [head as] bs] - [list [head as] [concat [tail as] bs]]]] - -[def prepend [x xs] - [list x xs]] - -[def append [xs x] - [if [is_nil xs] - [list [debug_int x] [nil]] - [list [head xs] [append [tail xs] x]]]] - -[def map [xs f] - [if [is_nil xs] - xs - [list [f [head xs]] - [map [tail xs] f]]]] - -[def debug_list_int [xs] - [if [is_nil xs] - xs - [list [debug_int [head xs]] - [debug_list_int [tail xs]]]]] diff --git a/examples/nq/Makefile b/examples/nq/Makefile new file mode 100644 index 0000000..a2ad100 --- /dev/null +++ b/examples/nq/Makefile @@ -0,0 +1,27 @@ +CC = clang +CXX = clang++ +SINC = ../../sinter + +DEBUG = -g -O3 + +CFLAGS = $(DEBUG) -Werror -Wpedantic -Wall -I$(HOME)/usr/include +LDFLAGS = $(DEBUG) -L$(HOME)/usr/lib +LDLIBS = -lgc + +LLCFLAGS = $(DEBUG) -Werror -Wpedantic -Wall +SINCFLAGS = -v + +TRGT = nqueens + +include ../../common.mak + +.PHONY: default all clean + +default: all + +all: $(TRGT) + +$(TRGT): nqueens.o util.o either.o list.o closures.o run_func.o glue.o + +clean: + $(RM) *.ll *.dot *.svg *.o $(TRGT) diff --git a/examples/nq/closures.sin b/examples/nq/closures.sin new file mode 100644 index 0000000..2e23daf --- /dev/null +++ b/examples/nq/closures.sin @@ -0,0 +1,47 @@ +[type closure_ [func arity arg_c args]] + +[dec = [x y]] + +[dec left [x]] +[dec right [x]] +[dec is_right [x]] +[dec right.val [x]] + +[dec run_func [f a as]] + +[dec cons [x xs]] +[dec append [xs x]] +[dec nil []] +[dec head [xs]] +[dec tail [xs]] +[dec is_nil [xs]] + +[def add_arg [c a] + [closure_ [closure_.func c] + [closure_.arity c] + [+ [closure_.arg_c c] 1] + [append [closure_.args c] a]]] + +[def run_closure [h] + [run_func [closure_.func h] [closure_.arity h] [closure_.args h]]] + +[def maybe_run_closure [h] + [if [= [closure_.arity h] [closure_.arg_c h]] + [left [run_func [closure_.func h] [closure_.arity h] [closure_.args h]]] + [right h]]] + +[def closure [f v] + [if v + [right [closure_ f v 0 [nil]]] + [left [run_func f 0 [nil]]]]] + +[def !> [c a] + [if [is_right c] + [maybe_run_closure [add_arg [right.val c] a]] + c]] + +[def !>> [c as] + [if [is_nil as] + c + [!>> [!> c [head as]] + [tail as]]]] diff --git a/examples/nq/either.sin b/examples/nq/either.sin new file mode 100644 index 0000000..75dc1a0 --- /dev/null +++ b/examples/nq/either.sin @@ -0,0 +1,37 @@ +[type left_ [kind val]] +[type right_ [kind val]] + +[dec inter_assert [a b]] + +[def left [val] + [left_ 0 val]] + +[def right [val] + [right_ 1 val]] + +[def _left.val [x] + [left_.val x]] + +[dec is_left [x]] + +[def left.val [x] + [inter_assert [is_left x] [left_.val x]]] + +[def right.val [x] + [right_.val x]] + +[def is_right [x] + [right_.kind x]] + +[def is_left [x] + [if [is_right x] + 0 + 1]] + +[def Either.>>= [a f] + [if [is_right a] + [f [right.val a]] + a]] + +[def Either.return [x] + [right x]] diff --git a/examples/nq/glue.c b/examples/nq/glue.c new file mode 100644 index 0000000..5d8c6f0 --- /dev/null +++ b/examples/nq/glue.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#include <gc.h> + +void *count_solns(void *n); + +void *gc_alloc(int size) { + void *p = GC_MALLOC(size); + + /* + * To get info about the GC discarding objects, use + * GC_REGISTER_FINALIZER(p, fn, arg, 0, 0); + * + * This runs fn(p, arg) when the object at p is finalised. + * + * More info in gc.h of Boehm garbage collector. + */ + + assert(p); + return p; +} + +int main(int argc, char **argv) { + + /* + * Initialising the GC is not necessary on all systems but may be required + * on some. Better safe than sorry! See gc.h for more info. + */ + GC_INIT(); + + if (argc != 2) { + printf("usage: %s n\n", argv[0]); + + } else { + int n = atoi(argv[1]); + int n_solns = *((int *) count_solns(&n)); + printf("%i solutions found\n", n_solns); + + } + + return 0; +} diff --git a/examples/nq/list.sin b/examples/nq/list.sin new file mode 100644 index 0000000..87c2509 --- /dev/null +++ b/examples/nq/list.sin @@ -0,0 +1,94 @@ +[type list_ [kind head tail]] +[type nil_ [kind]] + +[dec = [x y]] + +[dec !> [c a]] + +[def cons [head tail] + [list_ 0 head tail]] + +[def nil [] + [nil_ 1]] + +[def head [xs] + [list_.head xs]] + +[def tail [xs] + [list_.tail xs]] + +[def has_tail [xs] + [nil_.kind xs]] + +[def is_nil [xs] + [if [nil_.kind xs] + 1 + 0]] + +[def _length [xs n] + [if [is_nil xs] + n + [_length [tail xs] [+ n 1]]]] + +[def length [xs] + [_length xs 0]] + +[def concat [as bs] + [if [is_nil as] + bs + [cons [head as] [concat [tail as] bs]]]] + +[def alt_concat [as bs] + [cons [head as] + [if [is_nil [tail as]] + bs + [concat [tail as] bs]]]] + +[def foldr [f acc xs] + [if [is_nil xs] + acc + [f [head xs] [foldr f acc [tail xs]]]]] + +[def foldl [f acc xs] + [if [is_nil xs] + acc + [foldl f [f acc [head xs]] [tail xs]]]] + +[def . [g f x] + [g [f x]]] + +[def flatten [xs] + [if [is_nil xs] + [nil] + [concat [head xs] [flatten [tail xs]]]]] + +[def .. [x y] + [if [= x y] + [cons x [nil]] + [cons x [.. [+ x 1] y]]]] + +[def append [xs x] + [if [is_nil xs] + [cons x [nil]] + [cons [head xs] [append [tail xs] x]]]] + +[def map [f xs] + [if [is_nil xs] + [nil] + [cons [f [head xs]] + [map f [tail xs]]]]] + +[def pure [x] + [cons x [nil]]] + +[def empty [] + [nil]] + +[def return [x] + [cons x [nil]]] + +[def !map [c xs] + [if [is_nil xs] + xs + [cons [!> c [head xs]] + [!map c [tail xs]]]]] diff --git a/examples/nq/nqueens.sin b/examples/nq/nqueens.sin new file mode 100644 index 0000000..6839dc0 --- /dev/null +++ b/examples/nq/nqueens.sin @@ -0,0 +1,83 @@ +[dec != [x y]] +[dec and [x y]] + +[dec cons [x xs]] +[dec head [xs]] +[dec tail [xs]] +[dec nil []] +[dec is_nil [xs]] +[dec flatten [xs]] +[dec .. [x y]] +[dec length [xs]] +[dec map [f xs]] +[dec foldl [f acc xs]] +[dec concat [xs ys]] + +[dec left.val [x]] + +[dec pure [x]] +[dec empty []] +[dec return [x]] + +[dec !map [c as]] +[dec !> [c a]] +[dec closure [f a]] + +[def safe [x d qs] + [if [is_nil qs] + 1 + [and [and [!= x [head qs]] + [!= x [+ [head qs] d]]] + [and [!= x [- [head qs] d]] + [safe x [+ d 1] [tail qs]]]]]] + +[def guard [v] + [if v + [pure 0] + [empty]]] + +[def !. [g f x] + [!> g [f x]]] + +[def .! [g f x] + [g [left.val [!> f x]]]] + +[def !.! [g f x] + [!> g [!> f x]]] + +[def !foldl [c acc xs] + [if [is_nil xs] + acc + [!foldl c [left.val [!> [!> c acc] [head xs]]] [tail xs]]]] + +[def !concatMapClosure [c acc x] + [concat acc [left.val [!> c x]]]] + +[def !concatMap [c xs] + [!foldl [!> [closure !concatMapClosure 3] c] [nil] xs]] + +[def _!>>= [xs c] + [flatten [map left.val [!map c xs]]]] + +[def !>>= [xs c] + [!concatMap c xs]] + +[def gen_1_1_1 [q b _] + [return [cons q b]]] + +[def gen_1_1 [b q] + [!>>= [guard [safe q 1 b]] + [!> [!> [closure gen_1_1_1 3] q] b]]] + +[def gen_1 [nq b] + [!>>= [.. 1 nq] + [!> [closure gen_1_1 2] b]]] + +[def gen [n nq] + [if n + [!>>= [gen [- n 1] nq] + [!> [closure gen_1 2] nq]] + [cons [nil] [nil]]]] + +[def count_solns [n] + [length [gen n n]]] diff --git a/examples/nq/repeat.h b/examples/nq/repeat.h new file mode 100644 index 0000000..8564abf --- /dev/null +++ b/examples/nq/repeat.h @@ -0,0 +1,70 @@ +#ifndef REPEAT_H +#define REPEAT_H + +#define repeat_1(x) x +#define repeat_2(x) x, repeat_1(x) +#define repeat_3(x) x, repeat_2(x) +#define repeat_4(x) x, repeat_3(x) +#define repeat_5(x) x, repeat_4(x) +#define repeat_6(x) x, repeat_5(x) +#define repeat_7(x) x, repeat_6(x) +#define repeat_8(x) x, repeat_7(x) +#define repeat_9(x) x, repeat_8(x) +#define repeat_10(x) x, repeat_9(x) +#define repeat_11(x) x, repeat_10(x) +#define repeat_12(x) x, repeat_11(x) +#define repeat_13(x) x, repeat_12(x) +#define repeat_14(x) x, repeat_13(x) +#define repeat_15(x) x, repeat_14(x) +#define repeat_16(x) x, repeat_15(x) +#define repeat_17(x) x, repeat_16(x) +#define repeat_18(x) x, repeat_17(x) +#define repeat_19(x) x, repeat_18(x) +#define repeat_20(x) x, repeat_19(x) +#define repeat_21(x) x, repeat_20(x) +#define repeat_22(x) x, repeat_21(x) +#define repeat_23(x) x, repeat_22(x) +#define repeat_24(x) x, repeat_23(x) +#define repeat_25(x) x, repeat_24(x) +#define repeat_26(x) x, repeat_25(x) +#define repeat_27(x) x, repeat_26(x) +#define repeat_28(x) x, repeat_27(x) +#define repeat_29(x) x, repeat_28(x) +#define repeat_30(x) x, repeat_29(x) +#define repeat_31(x) x, repeat_30(x) +#define repeat_32(x) x, repeat_31(x) + +#define repeat_n_1(x) concat(x, 1) +#define repeat_n_2(x) repeat_n_1(x), concat(x, 2) +#define repeat_n_3(x) repeat_n_2(x), concat(x, 3) +#define repeat_n_4(x) repeat_n_3(x), concat(x, 4) +#define repeat_n_5(x) repeat_n_4(x), concat(x, 5) +#define repeat_n_6(x) repeat_n_5(x), concat(x, 6) +#define repeat_n_7(x) repeat_n_6(x), concat(x, 7) +#define repeat_n_8(x) repeat_n_7(x), concat(x, 8) +#define repeat_n_9(x) repeat_n_8(x), concat(x, 9) +#define repeat_n_10(x) repeat_n_9(x), concat(x, 10) +#define repeat_n_11(x) repeat_n_10(x), concat(x, 11) +#define repeat_n_12(x) repeat_n_11(x), concat(x, 12) +#define repeat_n_13(x) repeat_n_12(x), concat(x, 13) +#define repeat_n_14(x) repeat_n_13(x), concat(x, 14) +#define repeat_n_15(x) repeat_n_14(x), concat(x, 15) +#define repeat_n_16(x) repeat_n_15(x), concat(x, 16) +#define repeat_n_17(x) repeat_n_16(x), concat(x, 17) +#define repeat_n_18(x) repeat_n_17(x), concat(x, 18) +#define repeat_n_19(x) repeat_n_18(x), concat(x, 19) +#define repeat_n_20(x) repeat_n_19(x), concat(x, 20) +#define repeat_n_21(x) repeat_n_20(x), concat(x, 21) +#define repeat_n_22(x) repeat_n_21(x), concat(x, 22) +#define repeat_n_23(x) repeat_n_22(x), concat(x, 23) +#define repeat_n_24(x) repeat_n_23(x), concat(x, 24) +#define repeat_n_25(x) repeat_n_24(x), concat(x, 25) +#define repeat_n_26(x) repeat_n_25(x), concat(x, 26) +#define repeat_n_27(x) repeat_n_26(x), concat(x, 27) +#define repeat_n_28(x) repeat_n_27(x), concat(x, 28) +#define repeat_n_29(x) repeat_n_28(x), concat(x, 29) +#define repeat_n_30(x) repeat_n_29(x), concat(x, 30) +#define repeat_n_31(x) repeat_n_30(x), concat(x, 31) +#define repeat_n_32(x) repeat_n_31(x), concat(x, 32) + +#endif /* REPEAT_H */ diff --git a/examples/nq/run_func.c b/examples/nq/run_func.c new file mode 100644 index 0000000..7ee0652 --- /dev/null +++ b/examples/nq/run_func.c @@ -0,0 +1,79 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#include "repeat.h" + +typedef struct list { + void *kind; + void *contents; + struct list *next; +} list; + +/* FIXME: double fprintf is ugly */ +#define error(v, ...) do { \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + exit(v); \ +} while (0) + +#define _concat(a, b) a ## b +#define concat(a, b) _concat(a, b) + +#define assign_arg(n) \ + concat(arg_, n) = l->contents; \ + l = l->next + +#define cast_apply_f(n) return ((void *(*)(concat(repeat_, n)(void *))) f)\ +(concat(repeat_n_, n)(arg_)) + +#define new_case(n) \ + assign_arg(n); \ + if (arity == n) cast_apply_f(n) + +void *run_func(void *f, void *v, void *as) { + int arity = *((int *) v); + list *l = (list *) as; + + void repeat_n_32(*arg_); + + if (arity == 0) return ((void *(*)()) f)(); + new_case(1); + new_case(2); + new_case(3); + new_case(4); + new_case(5); + new_case(6); + new_case(7); + new_case(8); + new_case(9); + new_case(10); + new_case(11); + new_case(12); + new_case(13); + new_case(14); + new_case(15); + new_case(16); + new_case(17); + new_case(18); + new_case(19); + new_case(20); + new_case(21); + new_case(22); + new_case(23); + new_case(24); + new_case(25); + new_case(26); + new_case(27); + new_case(28); + new_case(29); + new_case(30); + new_case(31); + new_case(32); + error(1, "arity %i not handled", arity); +} + +void *inter_assert(void *a, void *b) { + assert(*((int *) a)); + return b; +} diff --git a/examples/nq/util.sin b/examples/nq/util.sin new file mode 100644 index 0000000..20cf3c4 --- /dev/null +++ b/examples/nq/util.sin @@ -0,0 +1,15 @@ +[def not [x] + [if x + 0 + 1]] + +[def and [x y] + [if x + y + 0]] + +[def != [x y] + [- x y]] + +[def = [x y] + [not [!= x y]]] diff --git a/llvm_codegen.c b/llvm_codegen.c index b27af9b..f64f9aa 100644 --- a/llvm_codegen.c +++ b/llvm_codegen.c @@ -35,12 +35,6 @@ static LLVMValueRef gc_alloc_p; static LLVMTypeRef print_int_t; static LLVMValueRef print_int_p; -static LLVMTypeRef debug_func_call_t; -static LLVMValueRef debug_func_call_p; - -static LLVMTypeRef print_str_t; -static LLVMValueRef print_str_p; - static LLVMTypeRef llvm_int_t; static LLVMTypeRef llvm_str_t; static LLVMTypeRef boxed_t; @@ -61,15 +55,21 @@ void put_builder_at_end(LLVMBuilderRef b, LLVMBasicBlockRef block) { } } +LLVMValueRef build_call(LLVMBuilderRef b, LLVMTypeRef t, LLVMValueRef p, + LLVMValueRef *args, uint argc, char *id) { + + LLVMValueRef call = LLVMBuildCall2(b, t, p, args, argc, id); + LLVMSetInstructionCallConv(call, LLVMFastCallConv); + return call; +} + LLVMValueRef box_val(LLVMBuilderRef b, LLVMValueRef val, LLVMTypeRef type) { - /* FIXME: memory leak */ - /* LLVMValueRef box = LLVMBuildMalloc(b, type, "box"); */ LLVMValueRef size = LLVMSizeOf(type); LLVMValueRef size_cast = LLVMConstIntCast(size, llvm_int_t, 0); LLVMTypeRef ptr_type = LLVMPointerType(type, 0); LLVMValueRef box = - LLVMBuildCall2(b, gc_alloc_t, gc_alloc_p, &size_cast, 1, "boxed"); + build_call(b, gc_alloc_t, gc_alloc_p, &size_cast, 1, "boxed"); LLVMValueRef box_cast = LLVMBuildPointerCast(b, box, ptr_type, "box_cast"); LLVMBuildStore(b, val, box_cast); @@ -86,7 +86,7 @@ LLVMValueRef unbox_val(LLVMBuilderRef b, LLVMValueRef box, LLVMTypeRef type) { LLVMValueRef codegen_print_int(LLVMValueRef ref) { LLVMValueRef args[] = { ref }; LLVMValueRef ret = - LLVMBuildCall2(builder, print_int_t, print_int_p, args, 1, ""); + build_call(builder, print_int_t, print_int_p, args, 1, ""); return ret; } @@ -114,6 +114,17 @@ LLVMValueRef codegen_id(char *id) { return val; } +LLVMTypeRef make_void_function_type(unsigned param_c, LLVMBool vararg) { + + /* FIXME: memory leak */ + LLVMTypeRef *boxed_params = malloc((sizeof (LLVMTypeRef)) * param_c); + for (uint i = 0; i < param_c; i++) { + boxed_params[i] = boxed_t; + } + + return LLVMFunctionType(LLVMVoidType(), boxed_params, param_c, vararg); +} + LLVMTypeRef make_function_type(unsigned param_c, LLVMBool vararg) { /* FIXME: memory leak */ @@ -125,6 +136,12 @@ LLVMTypeRef make_function_type(unsigned param_c, LLVMBool vararg) { return LLVMFunctionType(boxed_t, boxed_params, param_c, vararg); } +LLVMValueRef add_function(LLVMTypeRef func_t, char *func_id) { + LLVMValueRef func_p = LLVMAddFunction(module, func_id, func_t); + LLVMSetFunctionCallConv(func_p, LLVMFastCallConv); + return func_p; +} + LLVMTypeRef make_struct_type(char *id, uint param_c, LLVMBool packed) { /* FIXME: memory leak */ @@ -138,18 +155,28 @@ LLVMTypeRef make_struct_type(char *id, uint param_c, LLVMBool packed) { return struct_t; } -LLVMValueRef _codegen(sexpr *sexpr); +LLVMValueRef _codegen(sexpr *sexpr, int tail_position); uint get_args_from_list(sexpr *se, LLVMValueRef *args) { debug("getting args from list @%p\n", (void *) se); if (!se) return 0; assert(se->type == BRANCH); - *args = _codegen(se->contents.n.l); + *args = _codegen(se->contents.n.l, 0); return 1 + get_args_from_list(se->contents.n.r, args + 1); } +uint get_args_from_list_except_last(sexpr *se, LLVMValueRef *args) { + if (!se) return 0; + assert(se->type == BRANCH); + if (!se->contents.n.r) return 1; + + *args = _codegen(se->contents.n.l, 0); + + return 1 + get_args_from_list_except_last(se->contents.n.r, args + 1); +} + int get_type_param(sexpr *param, char **id, LLVMTypeRef *type) { assert(param); assert(param->type == BRANCH); @@ -206,6 +233,19 @@ uint make_func_params(sexpr *params, char **ids, lloc_t **llocs) { return 1 + make_func_params(r, ids + 1, llocs + 1); } +int make_filler_name(char *buf, int n, char *struct_id) { + return snprintf(buf, n, ".%s.filler", struct_id); +} + +int make_trmc_inner_name(char *buf, int n, char *outer_id, char *mod) { + return snprintf(buf, n, ".%s.inner.%s", outer_id, mod); +} + +LLVMValueRef build_gc_alloc(LLVMBuilderRef b, LLVMTypeRef t, char *id) { + LLVMValueRef size = LLVMConstIntCast(LLVMSizeOf(t), llvm_int_t, 0); + return LLVMBuildCall2(b, gc_alloc_t, gc_alloc_p, &size, 1, id); +} + LLVMValueRef codegen_type_definition(sexpr *se) { assert(se); assert(se->type == BRANCH); @@ -245,23 +285,63 @@ LLVMValueRef codegen_type_definition(sexpr *se) { LLVMBuilderRef util_b = LLVMCreateBuilder(); /* - * Make a function for constructing an instance of this struct type. Takes - * each of the struct's properties as an individual argument. + * Create a "filler" function: given a pointer as its last argument, fill in + * the space it points to with properties given as the other arguments. */ - LLVMTypeRef ctor_t = make_function_type(prop_c, 0); - LLVMValueRef ctor_p = LLVMAddFunction(module, struct_id, ctor_t); - LLVMBasicBlockRef ctor_b = LLVMAppendBasicBlock(ctor_p, "entry"); - put_builder_at_end(util_b, ctor_b); - /* FIXME: memory leak */ - LLVMValueRef src = LLVMBuildMalloc(util_b, struct_t, "src"); + /* FIXME: magic size */ + char filler_id[50]; + make_filler_name(filler_id, 50, struct_id); + + uint filler_param_c = prop_c + 1; + LLVMTypeRef filler_t = make_void_function_type(filler_param_c, 0); + LLVMValueRef filler_p = add_function(filler_t, filler_id); + LLVMBasicBlockRef filler_b = LLVMAppendBasicBlock(filler_p, "entry"); + put_builder_at_end(util_b, filler_b); + + LLVMValueRef space_cast = LLVMGetParam(filler_p, filler_param_c - 1); + LLVMValueRef space = LLVMBuildPointerCast(util_b, space_cast, + struct_ptr_t, "space_uncast"); for (uint i = 0; i < prop_c; i++) { - LLVMValueRef prop_pos = LLVMBuildStructGEP(util_b, src, i, "pos"); - LLVMValueRef param = LLVMGetParam(ctor_p, i); + LLVMValueRef prop_pos = LLVMBuildStructGEP(util_b, space, i, "pos"); + LLVMValueRef param = LLVMGetParam(filler_p, i); LLVMBuildStore(util_b, param, prop_pos); } - LLVMValueRef cast = LLVMBuildPointerCast(util_b, src, boxed_t, "cast"); - LLVMBuildRet(util_b, cast); - scope_add_entry(sc, struct_id, ctor_p, ctor_t); + LLVMBuildRetVoid(util_b); + + scope_entry *struct_e = malloc(sizeof(*struct_e)); + struct_e->type = struct_t; + struct_e->id = struct_id; + struct_e->value = filler_p; + + /* + * Create a wrapper around the filler function for use as a general-purpose + * constructor. + */ + char *ctor_id = struct_id; + uint ctor_param_c = prop_c; + LLVMTypeRef ctor_t = make_function_type(ctor_param_c, 0); + LLVMValueRef ctor_p = add_function(ctor_t, ctor_id); + LLVMBasicBlockRef ctor_b = LLVMAppendBasicBlock(ctor_p, "entry"); + put_builder_at_end(util_b, ctor_b); + + LLVMValueRef src = build_gc_alloc(util_b, struct_t, "src"); + assert(src); + + LLVMValueRef *args = malloc(filler_param_c * sizeof(*args)); + LLVMGetParams(ctor_p, args); + args[filler_param_c - 1] = src; + build_call(util_b, filler_t, filler_p, args, filler_param_c, ""); + free(args); + + LLVMBuildRet(util_b, src); + + scope_entry * ctor_e = scope_add_entry(sc, struct_id, ctor_p, ctor_t); + ctor_e->has_filler = 1; + /* FIXME: memory leak */ + ctor_e->filler.id = strdup(filler_id); + ctor_e->filler.value = filler_p; + ctor_e->filler.type = filler_t; + ctor_e->filler.constructs = struct_t; /* * Make functions to retreive each property of the struct. @@ -274,7 +354,7 @@ LLVMValueRef codegen_type_definition(sexpr *se) { LLVMTypeRef prop_func_t = make_function_type(1, 0); LLVMValueRef prop_func_p = - LLVMAddFunction(module, prop_func_id, prop_func_t); + add_function(prop_func_t, prop_func_id); LLVMBasicBlockRef prop_func_b = LLVMAppendBasicBlock(prop_func_p, "entry"); put_builder_at_end(util_b, prop_func_b); @@ -314,7 +394,7 @@ LLVMValueRef codegen_declaration(sexpr *se) { debug("building function %s\n", func_id); LLVMTypeRef func_t = make_function_type(param_c, 0); - LLVMValueRef func_p = LLVMAddFunction(module, func_id, func_t); + LLVMValueRef func_p = add_function(func_t, func_id); scope_add_entry(sc, func_id, func_p, func_t); @@ -376,6 +456,656 @@ LLVMMetadataRef debug_make_location(LLVMContextRef context, lloc_t *lloc, return LLVMDIBuilderCreateDebugLocation(context, lineno, colno, scope, 0); } +int looks_inside_final_argument(scope_entry *func_entry) { + /* + * TODO FIXME + * implementing this function will require keeping the AST of a function's + * definition in the entry; just look through the function to see if any + * access functions or operators are applied to the function's final + * argument (this would include checking function calls recursively) + */ + return 0 && func_entry; +} + +sexpr *last_list_item(sexpr *list) { + sexpr *last; + sexpr *current = list; + + while (current) { + last = current; + current = current->contents.n.r; + } + + return last; +} + +typedef enum { + NOT_TAIL_RECURSIVE = 0, + TAIL_RECURSIVE, + TAIL_RECURSIVE_MOD_CONS +} tail_recursive_t; + +int is_tail_recursive_branch(char *id, sexpr *branch) { + if (branch->type != BRANCH) return 0; + + char *outermost_id = branch->contents.n.l->contents.s; + scope_entry *outermost_e = scope_find(sc, outermost_id); + if (!outermost_e->has_filler) return 0; + + sexpr *last_arg = last_list_item(branch)->contents.n.l; + if (last_arg->type != BRANCH) return 0; + + char *last_arg_id = last_arg->contents.n.l->contents.s; + return !strcmp(last_arg_id, id); +} + +tail_recursive_t is_tail_recursive(char *id, sexpr *ast) { + switch (ast->type) { + case ID: + case INT: + /* + * just an ID here means that the function is returning a constant + * function pointer + * + * an int here means the function is returning a constant. + */ + debug("%s is not tail recursive because it returns a literal\n", + id); + return NOT_TAIL_RECURSIVE; + + case BRANCH: { + char *outermost_id = ast->contents.n.l->contents.s; + + if (!strcmp(id, outermost_id)) { + debug("%s is tail recursive\n", id); + return TAIL_RECURSIVE; + } + + if (strcmp(outermost_id, "if")) { + debug("%s is not tail recursive mod cons because it does not " + "immediately branch\n", id); + return NOT_TAIL_RECURSIVE; + } + + sexpr *cond_node = ast->contents.n.r; + sexpr *then_node = cond_node->contents.n.r; + if (is_tail_recursive_branch(id, then_node->contents.n.l)) { + debug("%s is recursive mod cons on its then branch\n", id); + return TAIL_RECURSIVE_MOD_CONS; + } + + sexpr *else_node = then_node->contents.n.r; + if (is_tail_recursive_branch(id, else_node->contents.n.l)) { + debug ("%s is recursive mod cons on its else branch\n", id); + return TAIL_RECURSIVE_MOD_CONS; + } + + debug("%s is not tail recursive\n", id); + return NOT_TAIL_RECURSIVE; + } + + case NIL: + default: + error(GENERAL_ERROR, "checking if %s is tail-recursive mod cons", + id); + + } +} + +sexpr *find_tail_recursive_call(char *id, sexpr *ast) { + if (ast->type != BRANCH) { + return 0; + } + + sexpr *last_arg = last_list_item(ast); + if (last_arg->type != BRANCH) { + return 0; + } + + if (strcmp(last_arg->contents.n.l->contents.n.l->contents.s, id)) { + return 0; + } + + return last_arg; +} + +void debug_set_builder_location(LLVMBuilderRef b, sexpr *se) { + + LLVMMetadataRef loc = + debug_make_location(global_context, &se->lloc, last_func_m); + + /* + * LLVM 9 onwards has LLVMSetCurrentDebugLocation2, and this call is + * deprecated + */ + LLVMSetCurrentDebugLocation(b, LLVMMetadataAsValue(global_context, + loc)); +} + +LLVMValueRef codegen_from_scope(char *func_id, LLVMValueRef *args, uint arg_c, + int tail_position) { + + scope_entry *entry = scope_find(sc, func_id); + if (!entry) return 0; + + LLVMValueRef func_p = entry->value; + LLVMTypeRef func_t = entry->type; + + /* + * in the case that the function being called is passed in as an argument, + * we cast it to the function kind that we're expecting. + */ + if (func_t == boxed_t) { + LLVMTypeRef exp_func_t = make_function_type(arg_c, 0); + LLVMTypeRef exp_func_ptr_t = LLVMPointerType(exp_func_t, 0); + LLVMValueRef casted = LLVMBuildPointerCast(builder, entry->value, + exp_func_ptr_t, "func_cast"); + func_t = exp_func_t; + func_p = casted; + } + + LLVMValueRef res = + build_call(builder, func_t, func_p, args, arg_c, ""); + + LLVMSetTailCall(res, tail_position); + + return res; +} + +void codegen_tmrc_inner(char *outer_id, char **outer_param_ids, LLVMValueRef + inner_p, LLVMValueRef then_p, LLVMTypeRef then_t, LLVMValueRef else_p, + LLVMTypeRef else_t, sexpr *body, sexpr* inner_ast) { + + sexpr *tail_recursive_call_ast = find_tail_recursive_call(outer_id, + inner_ast); + + LLVMBasicBlockRef entry_b = LLVMAppendBasicBlock(inner_p, "entry"); + LLVMBasicBlockRef then_b = LLVMAppendBasicBlock(inner_p, "trmc_then"); + LLVMBasicBlockRef else_b = LLVMAppendBasicBlock(inner_p, "trmc_else"); + put_builder_at_end(builder, entry_b); + + sexpr *cond_node = body->contents.n.r; + sexpr *cond_ast = cond_node->contents.n.l; + + /* FIXME: magic size */ + LLVMValueRef next_args[50]; + uint outer_arg_c = + get_args_from_list(tail_recursive_call_ast->contents.n.l->contents.n.r, + next_args); + uint inner_arg_c = outer_arg_c + 1; + + /* + * We need to lift the conditional of the next recursive call up into + * this one. To do so, we'll add a new layer to the scope that masks the + * function's variables with the values of those variables the next call + * down. + */ + scope_push_layer(&sc); + for (uint i = 0; i < (inner_arg_c - 1); ++i) { + scope_add_entry(sc, outer_param_ids[i], next_args[i], boxed_t); + } + + LLVMValueRef zero_v = LLVMConstInt(llvm_int_t, 0, 0); + LLVMValueRef cond_res = unbox_val(builder, _codegen(cond_ast, 0), + llvm_int_t); + LLVMValueRef cond_v = LLVMBuildICmp(builder, LLVMIntNE, + cond_res, zero_v, "neq_0"); + LLVMBuildCondBr(builder, cond_v, then_b, else_b); + + scope_pop_layer(&sc); + + put_builder_at_end(builder, then_b); + sexpr *then_node = cond_node->contents.n.r; + sexpr *then_ast = then_node->contents.n.l; + + if (is_tail_recursive_branch(outer_id, then_ast)) { + scope_entry *cons_e = scope_find(sc, + inner_ast->contents.n.l->contents.s); + LLVMValueRef next_space = build_gc_alloc(builder, + cons_e->filler.constructs, "then_space"); + + LLVMValueRef space = LLVMGetLastParam(inner_p); + /* FIXME: magic size */ + LLVMValueRef filler_args[50]; + uint cons_arg_c = + get_args_from_list_except_last(inner_ast->contents.n.r, + filler_args); + uint filler_arg_c = cons_arg_c + 1; + filler_args[cons_arg_c - 1] = next_space; + filler_args[filler_arg_c - 1] = space; + /* FIXME: magic size */ + char filler_id[50]; + make_filler_name(filler_id, 50, cons_e->id); + scope_entry *filler_e = scope_find(sc, filler_id); + build_call(builder, filler_e->type, filler_e->value, filler_args, + filler_arg_c, ""); + + next_args[inner_arg_c - 1] = next_space; + LLVMValueRef tail_call = build_call(builder, then_t, then_p, + next_args, inner_arg_c, ""); + LLVMSetTailCall(tail_call, 1); + + LLVMBuildRetVoid(builder); + + } else { + + /* + * Codegenning a simple branch requires masking the variables again + */ + scope_push_layer(&sc); + for (uint i = 0; i < (inner_arg_c - 1); ++i) { + scope_add_entry(sc, outer_param_ids[i], next_args[i], boxed_t); + } + + LLVMValueRef res = _codegen(then_ast, 0); + scope_pop_layer(&sc); + + LLVMValueRef space = LLVMGetLastParam(inner_p); + /* FIXME: magic size */ + LLVMValueRef filler_args[50]; + uint cons_arg_c = + get_args_from_list_except_last(inner_ast->contents.n.r, + filler_args); + uint filler_arg_c = cons_arg_c + 1; + filler_args[cons_arg_c - 1] = res; + filler_args[filler_arg_c - 1] = space; + char *cons_id = inner_ast->contents.n.l->contents.s; + scope_entry *cons_e = scope_find(sc, cons_id); + build_call(builder, cons_e->filler.type, cons_e->filler.value, + filler_args, filler_arg_c, ""); + + LLVMBuildRetVoid(builder); + + } + + put_builder_at_end(builder, else_b); + sexpr *else_node = then_node->contents.n.r; + sexpr *else_ast = else_node->contents.n.l; + + if (is_tail_recursive_branch(outer_id, else_ast)) { + scope_entry *cons_e = scope_find(sc, + inner_ast->contents.n.l->contents.s); + LLVMValueRef next_space = build_gc_alloc(builder, + cons_e->filler.constructs, "else_space"); + + LLVMValueRef space = LLVMGetLastParam(inner_p); + /* FIXME: magic size */ + LLVMValueRef filler_args[50]; + uint cons_arg_c = + get_args_from_list_except_last(inner_ast->contents.n.r, + filler_args); + uint filler_arg_c = cons_arg_c + 1; + filler_args[cons_arg_c - 1] = next_space; + filler_args[filler_arg_c - 1] = space; + /* FIXME: magic size */ + char filler_id[50]; + make_filler_name(filler_id, 50, cons_e->filler.id); + build_call(builder, cons_e->filler.type, cons_e->filler.value, + filler_args, filler_arg_c, ""); + + next_args[inner_arg_c - 1] = next_space; + LLVMValueRef tail_call = build_call(builder, else_t, else_p, + next_args, inner_arg_c, ""); + LLVMSetTailCall(tail_call, 1); + + LLVMBuildRetVoid(builder); + + } else { + + /* + * Codegenning a simple branch requires masking the variables again + */ + scope_push_layer(&sc); + for (uint i = 0; i < (inner_arg_c - 1); ++i) { + scope_add_entry(sc, outer_param_ids[i], next_args[i], boxed_t); + } + + LLVMValueRef res = _codegen(else_ast, 0); + scope_pop_layer(&sc); + + LLVMValueRef space = LLVMGetLastParam(inner_p); + /* FIXME: magic size */ + LLVMValueRef filler_args[50]; + uint cons_arg_c = + get_args_from_list_except_last(inner_ast->contents.n.r, + filler_args); + uint filler_arg_c = cons_arg_c + 1; + filler_args[cons_arg_c - 1] = res; + filler_args[filler_arg_c - 1] = space; + char *cons_id = inner_ast->contents.n.l->contents.s; + scope_entry *cons_e = scope_find(sc, cons_id); + /* FIXME: magic size */ + char filler_id[50]; + make_filler_name(filler_id, 50, cons_e->filler.id); + scope_entry *filler_e = scope_find(sc, filler_id); + build_call(builder, filler_e->type, filler_e->value, filler_args, + filler_arg_c, ""); + + LLVMBuildRetVoid(builder); + + } +} + +void codegen_trmc(scope_entry *outer_e, uint outer_param_c, sexpr *body, + char **param_names) { + + /* + * making a TRMC function TR requires creating an inner function that is + * called by the outer function, and that calls the cons function with a + * null final argument so that the final function called is the inner + * function itself. + * + * The inner function is given a pointer to space for it to fill alongside + * its usual arguments. It should fill this space, then (as its final + * action) tail-call itself recursively with a new space. + * + * The outer function has to call the cons for the first inner function call + * (of which there will always be at least one), but it needs to pass a + * dummy value in place of the return value of the inner function since it + * hasn't been called yet. + * + * This can be acheived by building two blocks for the outer function: + * 1. Regular code generation, except when the recursive tail-call is + * detected it is replaced by a pointer to empty space. + * 2. Call the previously-omitted tail-call, giving it its regular arguments + * plus a pointer to the space it should fill (rather than allocating + * space itself. + * + * 1 jumps straight into 2; having separate blocks means that 2 can be + * generated as soon as the recursive tail-call is found. + * + * The difficulty is that 1 needs a pointer to space already allocated for + * the right size for the result of 2. If the inner function contains a + * conditional (which it almost certainly does for managing a base case), + * then the size required could be variable. Pushing the conditional up to + * the caller function means it can allocate the correct amount of space. + * + * As an example: + * + * [def map [f xs] + * [if [is_nil xs] + * [nil] + * [cons [f [head xs]] + * [map f [tail xs]]]]] + * + * This definition of map is TRMC, and should become the following + * pseudocode: + * + * map(f, xs): + * if (is_nil(xs)): + * p = allocate_space_for(nil) + * else: + * p = allocate_space_for(cons) + * map_inner(f, xs, p) + * return p + * + * map_inner(f, xs, p): + * if (is_nil(xs)): + * *p = nil() + * else: + * next_xs = tail(xs) + * if (is_nil(next_xs)): + * q = allocate_space(nil) + * else: + * q = allocate_space(cons) + * *p = cons(f(head(xs)), q) + * map_inner(f, next_xs, q) + * + * This version duplicates the conditional expression, which might slow + * things down and also duplicate side effects. How about: + * + * map(f, xs): + * if (is_nil(xs)): + * p = allocate_space_for(nil) + * map_then(f, xs, p) + * else: + * p = allocate_space_for(cons) + * map_else(f, xs, p) + * return p + * + * map_then(f, xs, p): + * *p = nil() + * + * map_else(f, xs, p): + * next_f = f + * next_xs = tail(xs) + * if (is_nil(next_xs)): + * q = allocate_space_for(nil) + * *p = cons(f(head(xs)), q) + * map_then(next_f, next_xs, q) + * else: + * q = allocate_space_for(cons) + * *p = cons(f(head(xs)), q) + * map_else(next_f, next_xs, q) + * + * [def concat [as bs] + * [if [is_nil as] + * bs + * [cons [head as] [concat [tail as] bs]]]] + * + * becomes + * + * concat(as, bs): + * if (is_nil(as)): + * p = bs + * else: + * p = allocate_space_for(cons) + * concat_else(as, bs, p) + * return p + * + * concat_else(as, bs, p): + * next_as = tail(as) + * next_bs = bs + * if (is_nil(next_as)): + * q = next_bs + * *p = cons(head(as), q) + * else: + * q = allocate_space_for(cons) + * *p = cons(head(as), q) + * concat_else(next_as, next_bs, q) + * + * This doesn't duplicate side effects, but it would reorder them. + * + * So in general the pattern is: + * + * F(args): + * if (cond(args)): + * A(a_args(args)) + * else: + * B(b_args(args), F(f_args(args))) + * + * becomes: + * + * F(args): + * if (cond(args)): + * p = allocate_space_for(A) + * F_then(args, p) + * else: + * p = allocate_space_for(B) + * F_else(args, p) + * return p + * + * F_then(args, p): + * *p = A(a_args(args)) + * + * F_else(args, p): + * next_args = f_args(args) + * if (cond(next_args)): + * q = allocate_space_for(A) + * *p = B(b_args(args), q) + * F_then(next_args, p) + * else: + * q = allocate_space_for(B) + * *p = B(b_args(args), q) + * F_else(next_args, p) + * + * The outer function can be inlined, since it doesn't do too much. + */ + + /* + * Create the functions, by defining their types. The inner functions + * shouldn't be visible to the rest of the program, and the outer function + * is already defined, so there's no need to ad them to the scope. + */ + LLVMValueRef outer_p = outer_e->value; + LLVMTypeRef inner_t = make_void_function_type(outer_param_c + 1, 0); + + char *outer_id = outer_e->id; + + /* FIXME: magic size */ + char inner_id[50]; + + LLVMValueRef then_p = 0; + LLVMValueRef else_p = 0; + + /* + * Build the outer function. It's already been defined in + * codegen_definition, so some of its information is available in its scope + * entry. + * + * The topmost part of the AST should be the conditional. + */ + LLVMBasicBlockRef then_b = LLVMAppendBasicBlock(function, "trmc_then"); + LLVMBasicBlockRef else_b = LLVMAppendBasicBlock(function, "trmc_else"); + + sexpr *cond_node = body->contents.n.r; + sexpr *cond_ast = cond_node->contents.n.l; + LLVMValueRef llvm_zero_v = LLVMConstInt(llvm_int_t, 0, 0); + LLVMValueRef cond_res = _codegen(cond_ast, 0); + LLVMValueRef unboxed_cond_res = unbox_val(builder, cond_res, llvm_int_t); + LLVMValueRef cond_v = LLVMBuildICmp(builder, LLVMIntNE, unboxed_cond_res, + llvm_zero_v, "neq_0"); + LLVMBuildCondBr(builder, cond_v, then_b, else_b); + + uint inner_param_c = outer_param_c + 1; + LLVMValueRef *params = malloc(inner_param_c * sizeof(*params)); + LLVMGetParams(outer_p, params); + + put_builder_at_end(builder, then_b); + sexpr *then_node = cond_node->contents.n.r; + sexpr *then_ast = then_node->contents.n.l; + + if (is_tail_recursive_branch(outer_id, then_ast)) { + make_trmc_inner_name(inner_id, 50, outer_id, "then"); + then_p = add_function(inner_t, inner_id); + + scope_entry *then_cons = scope_find(sc, + then_ast->contents.n.l->contents.s); + LLVMValueRef then_space = build_gc_alloc(builder, then_cons->type, + "then_space"); + + params[inner_param_c - 1] = then_space; + build_call(builder, inner_t, then_p, params, inner_param_c, ""); + LLVMBuildRet(builder, then_space); + + } else { + LLVMValueRef res = _codegen(then_ast, 0); + LLVMBuildRet(builder, res); + + } + + put_builder_at_end(builder, else_b); + sexpr *else_node = then_node->contents.n.r; + sexpr *else_ast = else_node->contents.n.l; + + if (is_tail_recursive_branch(outer_id, else_ast)) { + make_trmc_inner_name(inner_id, 50, outer_id, "else"); + else_p = add_function(inner_t, inner_id); + + scope_entry *else_cons = scope_find(sc, + else_ast->contents.n.l->contents.s); + LLVMValueRef else_space = build_gc_alloc(builder, + else_cons->filler.constructs, "else_space"); + + params[inner_param_c - 1] = else_space; + build_call(builder, inner_t, else_p, params, inner_param_c, ""); + LLVMBuildRet(builder, else_space); + + } else { + LLVMValueRef res = _codegen(else_ast, 0); + LLVMBuildRet(builder, res); + + } + + if (then_p) codegen_tmrc_inner(outer_id, param_names, then_p, then_p, + inner_t, else_p, inner_t, body, then_ast); + + if (else_p) codegen_tmrc_inner(outer_id, param_names, else_p, then_p, + inner_t, else_p, inner_t, body, else_ast); + + /* + * It would be nice if we could consider these TRMC functions similarly to + * filler functions, e.g. flatten would be TRMC with concat being its cons. + * + * An idea is to make an allocator function for all TRMC-optimised + * functions, to complement the inner functions (since they're effectively + * fillers). Both the outer and inner functions can use this allocator + * function (maybe), but the readon for making these allocator functions is + * so that external functions that are tail-recursive mod this function can + * use it to allocate the correct space. + */ +} + +scope_entry *find_constructor_base(sexpr *ast) { + if (ast->type != BRANCH) return 0; + + char *outermost_id = ast->contents.n.l->contents.s; + scope_entry *outermost_e = scope_find(sc, outermost_id); + if (!outermost_e) return 0; + if (outermost_e->has_filler) return outermost_e; + + return 0; +} + +void codegen_constructor(scope_entry *outer_e, scope_entry *base_e, uint + outer_param_c, sexpr *body) { + + LLVMValueRef outer_p = outer_e->value; + char *outer_id = outer_e->id; + + /* FIXME: magic size */ + char filler_id[50]; + make_filler_name(filler_id, 50, outer_id); + + uint filler_param_c = outer_param_c + 1; + LLVMTypeRef filler_t = make_void_function_type(filler_param_c, 0); + LLVMValueRef filler_p = add_function(filler_t, filler_id); + + LLVMValueRef space = build_gc_alloc(builder, base_e->filler.constructs, + "space"); + /* FIXME: magic size */ + LLVMValueRef filler_params[50]; + LLVMGetParams(outer_p, filler_params); + filler_params[filler_param_c - 1] = space; + build_call(builder, filler_t, filler_p, filler_params, filler_param_c, + ""); + + LLVMBuildRet(builder, space); + + LLVMBasicBlockRef filler_entry_b = LLVMAppendBasicBlock(filler_p, + "entry"); + put_builder_at_end(builder, filler_entry_b); + function = filler_p; + + LLVMValueRef next_filler_params[50]; + uint next_filler_param_c = get_args_from_list(body->contents.n.r, + next_filler_params) + 1; + next_filler_params[next_filler_param_c - 1] = LLVMGetLastParam(filler_p); + LLVMValueRef next_filler_p = base_e->filler.value; + LLVMTypeRef next_filler_t = base_e->filler.type; + build_call(builder, next_filler_t, next_filler_p, next_filler_params, + next_filler_param_c, ""); + + LLVMBuildRetVoid(builder); + + outer_e->has_filler = 1; + /* FIXME: memory leak */ + outer_e->filler.id = strdup(filler_id); + outer_e->filler.value = filler_p; + outer_e->filler.type = filler_t; + outer_e->filler.constructs = base_e->filler.constructs; +} + LLVMValueRef codegen_definition(sexpr *se) { assert(se); assert(se->type == BRANCH); @@ -404,7 +1134,7 @@ LLVMValueRef codegen_definition(sexpr *se) { assert(param_c == LLVMCountParamTypes(entry->type)); - LLVMBasicBlockRef func_b = LLVMAppendBasicBlock(func_p, "entry"); + LLVMBasicBlockRef func_b = LLVMAppendBasicBlock(func_p, "debug"); /* create debug info for function */ LLVMMetadataRef func_m = debug_make_subprogram(di_builder, di_file, func_id, @@ -437,13 +1167,6 @@ LLVMValueRef codegen_definition(sexpr *se) { LLVMSetCurrentDebugLocation(builder, LLVMMetadataAsValue(global_context, loc)); -/* LLVMValueRef func_name = */ -/* LLVMBuildGlobalString(builder, func_id, "func_name"); */ -/* LLVMValueRef f_n_cast = */ -/* LLVMBuildPointerCast(builder, func_name, boxed_t, "func_name_cast"); */ -/* LLVMBuildCall2(builder, debug_func_call_t, debug_func_call_p, &f_n_cast, 1, */ -/* ""); */ - function = func_p; scope_push_layer(&sc); @@ -452,40 +1175,29 @@ LLVMValueRef codegen_definition(sexpr *se) { } assert(!info->contents.n.r->contents.n.r); - LLVMValueRef ret = _codegen(info->contents.n.r->contents.n.l); - LLVMBuildRet(builder, ret); - - scope_pop_layer(&sc); + sexpr *body = info->contents.n.r->contents.n.l; - debug("built function %s\n", func_id); - - return func_p; -} - -LLVMValueRef codegen_from_scope(char *func_id, LLVMValueRef *args, uint arg_c) { - scope_entry *entry = scope_find(sc, func_id); - if (!entry) return 0; - - LLVMValueRef func_p = entry->value; - LLVMTypeRef func_t = entry->type; + scope_entry *constructor_base = find_constructor_base(body); /* - * in the case that the function being called is passed in as an argument, - * we cast it to the function kind that we're expecting. + * check if this function is tail-recursive mod cons */ - if (func_t == boxed_t) { - LLVMTypeRef exp_func_t = make_function_type(arg_c, 0); - LLVMTypeRef exp_func_ptr_t = LLVMPointerType(exp_func_t, 0); - LLVMValueRef casted = LLVMBuildPointerCast(builder, entry->value, - exp_func_ptr_t, "func_cast"); - func_t = exp_func_t; - func_p = casted; + if (is_tail_recursive(func_id, body) == TAIL_RECURSIVE_MOD_CONS) { + codegen_trmc(entry, param_c, body, ids); + + } else if (constructor_base) { + codegen_constructor(entry, constructor_base, param_c, body); + + } else { + LLVMValueRef ret = _codegen(body, 1); + LLVMBuildRet(builder, ret); } - LLVMValueRef res = - LLVMBuildCall2(builder, func_t, func_p, args, arg_c, ""); + scope_pop_layer(&sc); - return res; + debug("built function %s\n", func_id); + + return func_p; } /* TODO: maybe make a function? */ @@ -527,7 +1239,7 @@ LLVMValueRef codegen_inbuilt_functions(sexpr *se, uint arg_c, LLVMValueRef return res; } -LLVMValueRef codegen_invocation(sexpr *se) { +LLVMValueRef codegen_invocation(sexpr *se, int tail_position) { assert(se); sexpr *l = se->contents.n.l; @@ -542,17 +1254,9 @@ LLVMValueRef codegen_invocation(sexpr *se) { LLVMValueRef args[50]; uint arg_c = get_args_from_list(r, args); - LLVMMetadataRef loc = - debug_make_location(global_context, &l->lloc, last_func_m); + debug_set_builder_location(builder, l); - /* - * LLVM 9 onwards has LLVMSetCurrentDebugLocation2, and this call is - * deprecated - */ - LLVMSetCurrentDebugLocation(builder, LLVMMetadataAsValue(global_context, - loc)); - - LLVMValueRef res = codegen_from_scope(func_id, args, arg_c); + LLVMValueRef res = codegen_from_scope(func_id, args, arg_c, tail_position); if (!res) res = codegen_inbuilt_functions(se, arg_c, args); if (!res) error(UNKNOWN_ID, "unrecognised ID %s", func_id); @@ -561,7 +1265,7 @@ LLVMValueRef codegen_invocation(sexpr *se) { return res; } -LLVMValueRef codegen_conditional(sexpr *se) { +LLVMValueRef codegen_conditional(sexpr *se, int tail_position) { assert(se); assert(se->type == BRANCH); @@ -589,19 +1293,19 @@ LLVMValueRef codegen_conditional(sexpr *se) { LLVMBasicBlockRef done_b = LLVMAppendBasicBlock(function, "done"); LLVMValueRef llvm_zero_v = LLVMConstInt(llvm_int_t, 0, 0); - LLVMValueRef cond_res = _codegen(cond_ast); + LLVMValueRef cond_res = _codegen(cond_ast, 0); LLVMValueRef unboxed_cond_res = unbox_val(builder, cond_res, llvm_int_t); LLVMValueRef cond_v = LLVMBuildICmp(builder, LLVMIntNE, unboxed_cond_res, llvm_zero_v, "neq_0"); LLVMBuildCondBr(builder, cond_v, then_b, else_b); put_builder_at_end(builder, then_b); - LLVMValueRef then_v = _codegen(then_ast); + LLVMValueRef then_v = _codegen(then_ast, tail_position); LLVMBasicBlockRef then_exit_b = builder_block; LLVMBuildBr(builder, done_b); put_builder_at_end(builder, else_b); - LLVMValueRef else_v = _codegen(else_ast); + LLVMValueRef else_v = _codegen(else_ast, tail_position); LLVMBasicBlockRef else_exit_b = builder_block; LLVMBuildBr(builder, done_b); @@ -614,7 +1318,7 @@ LLVMValueRef codegen_conditional(sexpr *se) { return phi_p; } -LLVMValueRef codegen_branch(sexpr *se) { +LLVMValueRef codegen_branch(sexpr *se, int tail_position) { assert(se); @@ -631,9 +1335,9 @@ LLVMValueRef codegen_branch(sexpr *se) { } else if (!strcmp(l->contents.s, "type")) { res = codegen_type_definition(r); } else if (!strcmp(l->contents.s, "if")) { - res = codegen_conditional(r); + res = codegen_conditional(r, tail_position); } else { - res = codegen_invocation(se); + res = codegen_invocation(se, tail_position); } } else { @@ -643,7 +1347,7 @@ LLVMValueRef codegen_branch(sexpr *se) { return res; } -LLVMValueRef _codegen(sexpr *sexpr) { +LLVMValueRef _codegen(sexpr *sexpr, int tail_position) { switch (sexpr->type) { case NIL: case INT: @@ -651,7 +1355,7 @@ LLVMValueRef _codegen(sexpr *sexpr) { case ID: return codegen_id(sexpr->contents.s); case BRANCH: - return codegen_branch(sexpr); + return codegen_branch(sexpr, tail_position); default: printf("erm\n"); return 0; @@ -737,26 +1441,10 @@ void llvm_codegen_prologue(char *filename) { LLVMDisposeMessage(target_triple); - /* add print_int */ - print_int_t = make_function_type(1, 0); - print_int_p = LLVMAddFunction(module, "debug_int", print_int_t); - scope_add_entry(sc, "debug_int", print_int_p, print_int_t); - - /* add print_str */ - LLVMTypeRef print_str_params[] = { llvm_str_t }; - print_str_t = LLVMFunctionType(LLVMInt32Type(), print_str_params, 1, 0); - print_str_p = LLVMAddFunction(module, "debug_str", print_str_t); - scope_add_entry(sc, "debug_str", print_str_p, print_str_t); - - /* add debug_func_call */ - debug_func_call_t = make_function_type(1, 0); - debug_func_call_p = - LLVMAddFunction(module, "debug_func_call", debug_func_call_t); - /* add gc_alloc */ LLVMTypeRef gc_alloc_params[] = { llvm_int_t }; gc_alloc_t = LLVMFunctionType(boxed_t, gc_alloc_params, 1, 0); - gc_alloc_p = LLVMAddFunction(module, "gc_alloc", gc_alloc_t); + gc_alloc_p = add_function(gc_alloc_t, "gc_alloc"); scope_add_entry(sc, "gc_alloc", gc_alloc_p, gc_alloc_t); } @@ -838,5 +1526,5 @@ int llvm_codegen_epilogue(char *output_filename, format_t format) { } void llvm_codegen(sexpr *se) { - _codegen(se); + _codegen(se, 0); } @@ -41,7 +41,9 @@ void scope_pop_layer(scope *sc) { *sc = new_sc; } -void scope_add_entry(scope sc, char *id, LLVMValueRef val, LLVMTypeRef type) { +scope_entry *scope_add_entry(scope sc, char *id, LLVMValueRef val, + LLVMTypeRef type) { + scope_entry *new_entry = malloc(sizeof (*new_entry)); if (!new_entry) error(GENERAL_ERROR, "cannot malloc?"); @@ -54,6 +56,8 @@ void scope_add_entry(scope sc, char *id, LLVMValueRef val, LLVMTypeRef type) { scope_layer *layer = sc; new_entry->prev = layer->head; layer->head = new_entry; + + return new_entry; } scope_entry *scope_find(scope sc, char *id) { @@ -8,6 +8,15 @@ typedef struct scope_entry { LLVMValueRef value; LLVMTypeRef type; LLVMMetadataRef metadata; + + int has_filler; + struct { + char *id; + LLVMValueRef value; + LLVMTypeRef type; + LLVMTypeRef constructs; + } filler; + struct scope_entry *prev; } scope_entry; @@ -21,7 +30,9 @@ typedef scope_layer *scope; void scope_push_layer(scope *sc); void scope_pop_layer(scope *sc); -void scope_add_entry(scope sc, char *id, LLVMValueRef val, LLVMTypeRef type); +scope_entry *scope_add_entry(scope sc, char *id, LLVMValueRef val, + LLVMTypeRef type); + scope_entry *scope_find(scope sc, char *id); #endif /* SCOPE_H */ |
