Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLVM-based Compiler #578

Merged
merged 31 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c749387
add plumbing for llvm-based compiler
ctiedt Jun 30, 2023
489b277
compile llvm ir with clang
ctiedt Jun 30, 2023
f956026
Merge remote-tracking branch 'origin/main' into inkwell-compiler
ctiedt Jul 13, 2023
1694762
start work on builtins, function calls
ctiedt Jul 13, 2023
9427241
free globals at end of program
ctiedt Jul 13, 2023
7a4e8a2
support ifElse and printing main output
ctiedt Jul 14, 2023
94f85cb
add support for closures
ctiedt Jul 20, 2023
8e7eb44
deref value when using reference instruction
ctiedt Jul 21, 2023
0f82e2f
reorganize candy_rt, add structs
ctiedt Jul 21, 2023
cedd1f5
implement list
ctiedt Jul 22, 2023
fc6dd1c
add support for indirect calls; make fibonacci run
ctiedt Aug 3, 2023
f8808d9
Merge remote-tracking branch 'origin/main' into inkwell-compiler
ctiedt Aug 3, 2023
3d9897e
remove unnecessary allocations in runtime
ctiedt Aug 3, 2023
f11eabb
Apply suggestions from code review
ctiedt Aug 10, 2023
f56f1a8
clean up runtime and codegen
ctiedt Aug 10, 2023
91f26bf
Merge branch 'inkwell-compiler' of https://github.com/candy-lang/cand…
ctiedt Aug 10, 2023
cdb1cb9
remove int.candy example
ctiedt Aug 10, 2023
874c299
fix packages after renaming backend_inkwell
ctiedt Aug 10, 2023
832e868
replace candy_type with candy_value
ctiedt Aug 10, 2023
4c28b54
deduplicate more of string allocation process
ctiedt Aug 10, 2023
05bf0d9
ignore responsibilities
ctiedt Aug 10, 2023
65a905c
Update compiler/backend_inkwell/src/lib.rs
ctiedt Aug 10, 2023
daee6f4
Update compiler/backend_inkwell/src/lib.rs
ctiedt Aug 10, 2023
8fb87a1
extract global creation into function
ctiedt Aug 10, 2023
8a43121
add inkwell feature to cli
ctiedt Aug 10, 2023
aa628c6
rename candy runtime
ctiedt Aug 10, 2023
008ab8e
address requested changes
ctiedt Aug 12, 2023
c97ccc4
download llvm in ci
ctiedt Aug 12, 2023
7681ee8
Merge remote-tracking branch 'origin/main' into inkwell-compiler
ctiedt Aug 12, 2023
98b9da4
undo workspace formatting
ctiedt Aug 12, 2023
b8467e6
specify exact llvm action version in ci
ctiedt Aug 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/labels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- compiler/frontend/**/*
'P: Compiler: Fuzzer':
- compiler/fuzzer/**/*
'P: Compiler: Inkwell Backend':
- compiler/backend_inkwell/**/*
'P: Compiler: Language Server':
- compiler/language_server/**/*
'P: Compiler: VM':
Expand Down
3 changes: 2 additions & 1 deletion compiler/backend_inkwell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ edition = "2021"
[dependencies]
candy_frontend = { version = "0.1.0", path = "../frontend" }
inkwell = { version = "0.2.0", features = ["llvm15-0"] }
llvm-sys = { version = "150", features = ["prefer-dynamic"] }
itertools = "0.11.0"
llvm-sys = { version = "150", features = ["prefer-dynamic"] }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC=clang

candy_rt.a: candy_rt.o candy_builtin.o
candy_runtime.a: candy_runtime.o candy_builtin.o
ar rcs $@ $^

%.o: %.c %.h
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "candy_rt.h"
#include "candy_runtime.h"

const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right)
{
Expand All @@ -21,7 +22,7 @@ const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *ri
}
}

const candy_value_t *candy_builtin_ifelse(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise)
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise)
{
candy_value_t *body = candy_tag_to_bool(condition) ? then : otherwise;
candy_function function = (body->value).function.function;
Expand All @@ -41,7 +42,14 @@ candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *ri

candy_value_t *candy_builtin_int_bit_length(candy_value_t *value)
{
return make_candy_int(64);
long long int_value = value->value.integer;
ctiedt marked this conversation as resolved.
Show resolved Hide resolved
int shifts = 0;
while (int_value)
{
int_value = int_value >> shifts;
shifts++;
}
ctiedt marked this conversation as resolved.
Show resolved Hide resolved
return make_candy_int(shifts);
}

candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right)
Expand All @@ -59,10 +67,10 @@ candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t
return make_candy_int(left->value.integer ^ right->value.integer);
}

const candy_value_t *candy_builtin_int_compareto(candy_value_t *left, candy_value_t *right)
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right)
{
int128_t left_value = left->value.integer;
int128_t right_value = right->value.integer;
int64_t left_value = left->value.integer;
int64_t right_value = right->value.integer;
if (left_value < right_value)
{
return &__internal_less;
Expand Down Expand Up @@ -122,7 +130,7 @@ const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, cand
return &__internal_false;
}

const candy_value_t *candy_builtin_typeof(candy_value_t *value)
const candy_value_t *candy_builtin_type_of(candy_value_t *value)
{
switch (value->type)
{
Expand All @@ -141,4 +149,4 @@ const candy_value_t *candy_builtin_typeof(candy_value_t *value)
default:
candy_panic(&__internal_unknown);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "candy_rt.h"
#include "candy_runtime.h"

candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_ifelse(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise);
const candy_value_t *candy_builtin_equals(candy_value_t *left, candy_value_t *right);
const candy_value_t *candy_builtin_if_else(candy_value_t *condition, candy_value_t *then, candy_value_t *otherwise);
candy_value_t *candy_builtin_int_add(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_subtract(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bit_length(candy_value_t *value);
candy_value_t *candy_builtin_int_bitwise_and(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bitwise_or(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_bitwise_xor(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_int_compareto(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_list_length(candy_value_t *list);
candy_value_t *candy_builtin_print(candy_value_t *value);
const candy_value_t *candy_builtin_int_compare_to(candy_value_t *left, candy_value_t *right);
candy_value_t *candy_builtin_list_length(const candy_value_t *list);
const candy_value_t *candy_builtin_print(candy_value_t *value);
candy_value_t *candy_builtin_struct_get(candy_value_t *structure, candy_value_t *key);
candy_value_t *candy_builtin_struct_get_keys(candy_value_t *structure);
candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key);
candy_value_t *candy_builtin_typeof(candy_value_t *value);
const candy_value_t *candy_builtin_struct_has_key(candy_value_t *structure, candy_value_t *key);
const candy_value_t *candy_builtin_type_of(candy_value_t *value);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "candy_rt.h"
#include "candy_runtime.h"
#include "candy_builtin.h"

const candy_value_t __internal_true = {
Expand Down Expand Up @@ -49,7 +49,7 @@ void print_candy_value(const candy_value_t *value)
switch (value->type)
{
case CANDY_TYPE_INT:
printf("%lld", value->value.integer);
printf("%ld", value->value.integer);
break;
case CANDY_TYPE_TEXT:
printf("%s", value->value.text);
Expand Down Expand Up @@ -116,7 +116,7 @@ int candy_tag_to_bool(const candy_value_t *value)
}
}

candy_value_t *make_candy_int(int128_t value)
candy_value_t *make_candy_int(int64_t value)
{
candy_value_t *candy_value = malloc(sizeof(candy_value_t));
candy_value->value.integer = value;
Expand Down Expand Up @@ -168,7 +168,7 @@ candy_value_t *make_candy_struct(candy_value_t **keys, candy_value_t **values)
return candy_value;
}

candy_value_t *call_candy_function_with(candy_value_t *function, candy_value_t *arg)
candy_value_t *run_candy_main(candy_value_t *function, candy_value_t *arg)
{
return function->value.function.function(arg);
}
Expand All @@ -193,7 +193,7 @@ void candy_panic(const candy_value_t *reason)

void free_candy_value(candy_value_t *value)
{
if (value == candy_environment && value == NULL)
if (value == candy_environment || value == NULL)
{
return;
}
Expand All @@ -206,4 +206,4 @@ void free_candy_value(candy_value_t *value)
// the list/struct, because they will be freed on their own
// at the end of the main function.
free(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __CANDY_RT_H
#define __CANDY_RT_H

#define int128_t long long int
#include <stdint.h>

typedef enum
{
Expand All @@ -16,7 +16,7 @@ typedef enum
typedef struct
{
void *environment;
struct candy_value *(*function)(struct candy_value *);
struct candy_value *(*function)(struct candy_value *, ...);
} candy_function_t;

typedef struct
Expand All @@ -29,7 +29,7 @@ typedef struct candy_value
{
union
{
int128_t integer;
int64_t integer;
char *text;
struct candy_value **list;
candy_function_t function;
Expand All @@ -38,7 +38,7 @@ typedef struct candy_value
candy_type_t type;
} candy_value_t;

typedef candy_value_t *(*candy_function)(candy_value_t *);
typedef candy_value_t *(*candy_function)(candy_value_t *, ...);

const extern candy_value_t __internal_true;
const extern candy_value_t __internal_false;
Expand All @@ -53,20 +53,21 @@ const extern candy_value_t __internal_list;
const extern candy_value_t __internal_struct;
const extern candy_value_t __internal_function;
const extern candy_value_t __internal_unknown;
const extern candy_value_t __internal_platform;
extern candy_value_t _candy_environment;
extern candy_value_t *candy_environment;

void print_candy_value(const candy_value_t *value);
const candy_value_t *to_candy_bool(int value);
int candy_tag_to_bool(const candy_value_t *value);
candy_value_t *make_candy_int(int128_t value);
candy_value_t *make_candy_int(int64_t value);
candy_value_t *make_candy_text(char *text);
candy_value_t *make_candy_tag(char *tag);
candy_value_t *make_candy_list(candy_value_t **values);
candy_value_t *make_candy_function(candy_function function, void *environment, int env_size);
candy_value_t *call_candy_function_with(candy_value_t *function, candy_value_t *arg);
candy_value_t *run_candy_main(candy_value_t *function, candy_value_t *arg);
candy_function get_candy_function_pointer(candy_value_t *function);
void *get_candy_function_environment(candy_value_t *function);
void candy_panic(const candy_value_t *reason);
void free_candy_value(candy_value_t *value);
#endif
#endif
Loading