-
Notifications
You must be signed in to change notification settings - Fork 0
/
mank.sh
executable file
·188 lines (159 loc) · 4.69 KB
/
mank.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
set -e
# Helper script to compile .mank files with a mank_main() to a binary
# Usage: ./mank.sh my_prog.mank
# Creates ./my_prog binary
if [[ -z "${MANK_HOME}" ]]; then
MANK_HOME=$(pwd)
else
MANK_HOME="${MANK_HOME}"
fi
build_dir=$(mktemp --directory)
output_dir=$(pwd)
source_dir_base=$output_dir
if [[ "${ABS_SOURCE_PATH}" == "1" ]]; then
source_dir_base=""
fi
cd $build_dir
cat << EOF > ./main.c
#include <stdio.h>
#include <gc/gc.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
extern void __mank__main(void*);
int stderr_putchar(char c) {
if (write(STDERR_FILENO, &c, 1) == -1) {
return -1;
}
return c;
}
void* __mank_alloc__any(size_t bytes) {
return GC_MALLOC(bytes);
}
void* __mank_alloc__atomic(size_t bytes) {
return GC_MALLOC_ATOMIC(bytes);
}
char* __mank_builtin__str_concat(size_t l1, char* s1, size_t l2, char* s2, size_t* l_out) {
*l_out = l1 + l2;
char* new_str = __mank_alloc__atomic(*l_out);
memcpy(new_str, s1, l1);
memcpy(new_str + l1, s2, l2);
return new_str;
}
char* __mank_builtin__str_cast(char c) {
char* new_str = __mank_alloc__atomic(1);
*new_str = c;
return new_str;
}
// Pretty much 24 bytes
struct __mank_vec_header {
size_t
type_size,
capacity,
length;
};
struct __mank_vec {
void* data; //-3 <> -2<> -1<> 0 1 2
};
struct __mank_str {
size_t length;
char* data;
};
#define MANK_VEC_INIT_CAPACITY 10
inline static struct __mank_vec_header* get_vec_header(struct __mank_vec vec, bool offset) {
if (offset) {
vec.data -= sizeof(struct __mank_vec_header);
}
struct __mank_vec_header* header = (struct __mank_vec_header*)(vec.data);
}
void __mank_builtin__init_vec(struct __mank_vec* vec, size_t type_size) {
vec->data = __mank_alloc__any(sizeof(struct __mank_vec_header) + type_size * MANK_VEC_INIT_CAPACITY);
struct __mank_vec_header* header = get_vec_header(*vec, false);
header->type_size = type_size;
header->capacity = MANK_VEC_INIT_CAPACITY;
header->length = 0;
vec->data += sizeof(struct __mank_vec_header);
}
static inline struct __mank_vec_header* resize_vector(struct __mank_vec* vec) {
struct __mank_vec_header* header = get_vec_header(*vec, true);
void* data = vec->data - sizeof(struct __mank_vec_header);
data = GC_REALLOC(data, sizeof(struct __mank_vec_header) + header->capacity * header->type_size);
vec->data = data + sizeof(struct __mank_vec_header);
return get_vec_header(*vec, true);
}
void __mank_builtin__push_back(struct __mank_vec* vec, void* new_element) {
struct __mank_vec_header* header = get_vec_header(*vec, true);
header->length += 1;
if (header->length > header->capacity) {
header->capacity *= 2;
header = resize_vector(vec);
}
memcpy(vec->data + (header->length - 1) * header->type_size, new_element, header->type_size);
}
void __mank_builtin__pop_back(struct __mank_vec* vec) {
struct __mank_vec_header* header = get_vec_header(*vec, true);
if (header->length <= 0) {
return;
}
header->length -= 1; // good enough
}
void __mank_builtin__vector_fill(struct __mank_vec* vec, void* fill, size_t count) {
struct __mank_vec_header* header = get_vec_header(*vec, true);
if (count > header->capacity) {
header->capacity = count;
header = resize_vector(vec);
}
header->length = count;
void* el = vec->data;
for (size_t i = 0; i < count; i++) {
memcpy(el, fill, header->type_size);
el += header->type_size;
}
}
void __mank_builtin__bounds_error(
size_t length, int64_t index, char* filename, int32_t line, int32_t col
) {
fprintf(stderr, "Index out of bounds: %s:%d:%d: length is %ld, but index was %ld\n",
filename, line + 1, col + 1, length, index);
abort();
}
int main(int argc, char* argv[]) {
struct __mank_vec mank_args;
__mank_builtin__init_vec(&mank_args, sizeof(struct __mank_str));
for (size_t i = 0; i < argc; i++) {
char* raw_str = argv[i];
struct __mank_str str = { .length = strlen(raw_str), .data = raw_str };
__mank_builtin__push_back(&mank_args, &str);
}
// LLVM codegens passing struct by value as passing as a bunch of params
__mank__main(mank_args.data);
}
EOF
bin_name=$(basename $1 .mank)
if [[ "$2" == "--tests" ]]; then
LLC_ARGS=""
MANK_ARGS="--tests"
bin_name+="_tests"
else
LLC_ARGS="$2"
MANK_ARGS=""
fi
compile_ir () {
for llvm_ir in ./*.ll
do
llc $LLC_ARGS -relocation-model=pic "$llvm_ir"
done
}
{
gcc ./main.c -c -o main.o \
&& $MANK_HOME/mankc $source_dir_base/$1 --codegen $MANK_ARGS > mank_dump.ll.packed \
&& csplit --quiet -b "%d.ll" ./mank_dump.ll.packed "/;--fin/" \
&& compile_ir \
&& gcc ./main.o ./*.s -o ./$bin_name -lgc -lm \
&& mv ./$bin_name $output_dir/$bin_name
}
cd ..
rm -rf $build_dir