Skip to content
This repository was archived by the owner on Jan 8, 2023. It is now read-only.

Commit 48c4e88

Browse files
author
ApsarasX
committed
feat: add optimization
1 parent 99b07cd commit 48c4e88

35 files changed

+374
-147
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ jobs:
3434
path: ${{ github.workspace }}/thirdparty
3535
key: ${{ matrix.os }}-${{ matrix.compiler.cc }}-antlr
3636
- name: Install Prerequirements
37-
run: sudo apt-get -y install uuid-dev pkg-config doxygen graphviz llvm-10
37+
run: |
38+
sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
39+
sudo apt-get -y install uuid-dev pkg-config doxygen graphviz
3840
- name: Install Antlr4 and Antlr4 Runtime
3941
if: steps.cache-antlr.outputs.cache-hit != 'true'
4042
env:
@@ -66,7 +68,7 @@ jobs:
6668
cd cmake-build-debug
6769
sudo cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="-w" -G "Unix Makefiles" ..
6870
sudo cmake --build . --target staticscript document -- -j $thread_count
69-
for file in ../examples/*ss
71+
for file in ../examples/*.ss
7072
do
7173
sudo ./lib/staticscript $file --emit-llvm -o ss-ir.ll
7274
sudo ./lib/staticscript $file -o ss-obj.o
@@ -104,15 +106,15 @@ jobs:
104106
cd cmake-build-debug
105107
sudo cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS="-w" -G "Unix Makefiles" ..
106108
sudo cmake --build . --target staticscript document -- -j $thread_count
107-
for file in ../examples/*ss
109+
for file in ../examples/*.ss
108110
do
109111
sudo ./lib/staticscript $file --emit-llvm -o ss-ir.ll
110112
sudo ./lib/staticscript $file -o ss-obj.o
111113
done
112114
cd ../cmake-build-release
113115
sudo cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-w" -G "Unix Makefiles" ..
114116
sudo cmake --build . --target staticscript document -- -j $thread_count
115-
for file in ../examples/*ss
117+
for file in ../examples/*.ss
116118
do
117119
sudo ./lib/staticscript $file --emit-llvm -o ss-ir.ll
118120
sudo ./lib/staticscript $file -o ss-obj.o

builtin/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ set(BUILTIN_BITCODES ss_string.bc ss_io.bc)
33

44
add_custom_command(
55
OUTPUT ${BUILTIN_BITCODES}
6-
COMMAND clang -c -emit-llvm ${PROJECT_SOURCE_DIR}/builtin/ss_string.c -std=c99 -Os -o ss_string.bc
7-
COMMAND clang -c -emit-llvm ${PROJECT_SOURCE_DIR}/builtin/ss_io.c -std=c99 -Os -o ss_io.bc
6+
COMMAND clang -c -emit-llvm ${PROJECT_SOURCE_DIR}/builtin/ss_string.c -std=c99 -O2 -o ss_string.bc
7+
COMMAND clang -c -emit-llvm ${PROJECT_SOURCE_DIR}/builtin/ss_io.c -std=c99 -O2 -o ss_io.bc
88
DEPENDS ${BUILTIN_SOURCES}
99
)
1010

builtin/ss_io.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
#include "ss_io.h"
22

3-
ss_string *KEEPALIVE ss_integer2string(long number) {
3+
ss_string *ss_integer2string(long number) {
44
size_t capacity = ss_string_get_capacity_with_size(20);
55
ss_string *str = ss_string_create_with_capacity(capacity);
66
snprintf(str->buffer, 20, "%ld", number);
77
str->size = strlen(str->buffer);
88
return str;
99
}
1010

11-
long KEEPALIVE ss_string2integer(ss_string *str) {
11+
long ss_string2integer(ss_string *str) {
1212
return strtol(str->buffer, NULL, 10);
1313
}
1414

15-
void KEEPALIVE ss_print_integer(long number) {
15+
void ss_print_integer(long number) {
1616
printf("%ld", number);
1717
}
1818

19-
void KEEPALIVE ss_println_integer(long number) {
19+
void ss_println_integer(long number) {
2020
printf("%ld\n", number);
2121
}
2222

23-
void KEEPALIVE ss_print_string(ss_string *str) {
23+
void ss_print_string(ss_string *str) {
2424
printf("%s", str->buffer);
2525
}
2626

27-
void KEEPALIVE ss_println_string(ss_string *str) {
27+
void ss_println_string(ss_string *str) {
2828
printf("%s\n", str->buffer);
2929
}

builtin/ss_io.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
#include "ss_base.h"
66
#include "ss_string.h"
77

8-
ss_string *KEEPALIVE ss_integer2string(long number);
8+
ss_string *ss_integer2string(long number);
99

10-
long KEEPALIVE ss_string2integer(ss_string *str);
10+
long ss_string2integer(ss_string *str);
1111

12-
void KEEPALIVE ss_print_integer(long number);
12+
void ss_print_integer(long number);
1313

14-
void KEEPALIVE ss_println_integer(long number);
14+
void ss_println_integer(long number);
1515

16-
void KEEPALIVE ss_print_string(ss_string *str);
16+
void ss_print_string(ss_string *str);
1717

18-
void KEEPALIVE ss_println_string(ss_string *str);
18+
void ss_println_string(ss_string *str);

builtin/ss_string.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ss_string.h"
22

3-
size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size) {
3+
size_t ss_string_get_capacity_with_size(size_t size) {
44
if (size == 0) {
55
return 16;
66
} else if (size < 16) {
@@ -12,7 +12,7 @@ size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size) {
1212
}
1313
}
1414

15-
ss_string *KEEPALIVE ss_string_create_with_capacity(size_t capacity) {
15+
ss_string *ss_string_create_with_capacity(size_t capacity) {
1616
ss_string *str = (ss_string *) malloc(sizeof(ss_string));
1717
if (!str) {
1818
return NULL;
@@ -24,7 +24,7 @@ ss_string *KEEPALIVE ss_string_create_with_capacity(size_t capacity) {
2424
return str;
2525
}
2626

27-
ss_string *KEEPALIVE ss_string_create(const char *literal) {
27+
ss_string *ss_string_create(const char *literal) {
2828
size_t size = strlen(literal);
2929
size_t capacity = ss_string_get_capacity_with_size(size);
3030
ss_string *str = ss_string_create_with_capacity(capacity);
@@ -36,16 +36,16 @@ ss_string *KEEPALIVE ss_string_create(const char *literal) {
3636
return str;
3737
}
3838

39-
void KEEPALIVE ss_string_delete(ss_string *str) {
39+
void ss_string_delete(ss_string *str) {
4040
free(str->buffer);
4141
free(str);
4242
}
4343

44-
size_t KEEPALIVE ss_string_get_size(ss_string *str) {
44+
size_t ss_string_get_size(ss_string *str) {
4545
return str->size;
4646
}
4747

48-
long KEEPALIVE ss_string_grow_with_capacity(ss_string *str, size_t new_capacity) {
48+
long ss_string_grow_with_capacity(ss_string *str, size_t new_capacity) {
4949
char *new_buffer = (char *) calloc(new_capacity, 1);
5050
if (!new_buffer) {
5151
return -1;
@@ -58,7 +58,7 @@ long KEEPALIVE ss_string_grow_with_capacity(ss_string *str, size_t new_capacity)
5858
return 0;
5959
}
6060

61-
long KEEPALIVE ss_string_grow(ss_string *str) {
61+
long ss_string_grow(ss_string *str) {
6262
if (str->capacity < 32) {
6363
str->capacity += 16;
6464
} else {
@@ -67,7 +67,7 @@ long KEEPALIVE ss_string_grow(ss_string *str) {
6767
return ss_string_grow_with_capacity(str, str->capacity);
6868
}
6969

70-
long KEEPALIVE ss_string_append(ss_string *dest, ss_string *src) {
70+
long ss_string_append(ss_string *dest, ss_string *src) {
7171
size_t src_size = strlen(src->buffer);
7272
size_t needed_size = dest->size + src_size;
7373
size_t needed_capacity = ss_string_get_capacity_with_size(needed_size);
@@ -81,7 +81,7 @@ long KEEPALIVE ss_string_append(ss_string *dest, ss_string *src) {
8181
return 0;
8282
}
8383

84-
long KEEPALIVE ss_string_prepend(ss_string *dest, ss_string *src) {
84+
long ss_string_prepend(ss_string *dest, ss_string *src) {
8585
size_t src_size = strlen(src->buffer);
8686
size_t needed_size = dest->size + src_size;
8787
size_t needed_capacity = ss_string_get_capacity_with_size(needed_size);
@@ -106,7 +106,7 @@ ss_string *ss_string_concat(ss_string *str1, ss_string *str2) {
106106
return str;
107107
}
108108

109-
ss_string *KEEPALIVE ss_string_slice(ss_string *str, ssize_t from, ssize_t to) {
109+
ss_string *ss_string_slice(ss_string *str, ssize_t from, ssize_t to) {
110110
if (from < 0) {
111111
from = str->size + from;
112112
}
@@ -124,24 +124,24 @@ ss_string *KEEPALIVE ss_string_slice(ss_string *str, ssize_t from, ssize_t to) {
124124
return new_str;
125125
}
126126

127-
long KEEPALIVE ss_string_equals(ss_string *str1, ss_string *str2) {
127+
long ss_string_equals(ss_string *str1, ss_string *str2) {
128128
size_t max_size = str1->size > str2->size ? str1->size : str2->size;
129129
return strncmp(str1->buffer, str2->buffer, max_size);
130130
}
131131

132-
ssize_t KEEPALIVE ss_string_index_of_with_literal(ss_string *str, const char *literal) {
132+
ssize_t ss_string_index_of_with_literal(ss_string *str, const char *literal) {
133133
char *sub = strstr(str->buffer, literal);
134134
if (!sub) {
135135
return -1;
136136
}
137137
return sub - str->buffer;
138138
}
139139

140-
ssize_t KEEPALIVE ss_string_index_of(ss_string *str, ss_string *substr) {
140+
ssize_t ss_string_index_of(ss_string *str, ss_string *substr) {
141141
return ss_string_index_of_with_literal(str, substr->buffer);
142142
}
143143

144-
long KEEPALIVE ss_string_trim_left(ss_string *str) {
144+
long ss_string_trim_left(ss_string *str) {
145145
size_t i = 0;
146146
while (isspace(str->buffer[i])) {
147147
i += 1;
@@ -158,7 +158,7 @@ long KEEPALIVE ss_string_trim_left(ss_string *str) {
158158
return 0;
159159
}
160160

161-
void KEEPALIVE ss_string_trim_right(ss_string *str) {
161+
void ss_string_trim_right(ss_string *str) {
162162
ssize_t i = str->size - 1;
163163
while (i >= 0 && isspace(str->buffer[i])) {
164164
str->buffer[i] = 0;
@@ -167,7 +167,7 @@ void KEEPALIVE ss_string_trim_right(ss_string *str) {
167167
str->size = i + 1;
168168
}
169169

170-
long KEEPALIVE ss_string_trim(ss_string *str) {
170+
long ss_string_trim(ss_string *str) {
171171
ss_string_trim_right(str);
172172
return ss_string_trim_left(str);
173173
}

builtin/ss_string.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,36 @@ typedef struct {
1212
size_t capacity;
1313
} ss_string;
1414

15-
size_t KEEPALIVE ss_string_get_capacity_with_size(size_t size);
15+
size_t ss_string_get_capacity_with_size(size_t size);
1616

17-
ss_string *KEEPALIVE ss_string_create_with_capacity(size_t capacity);
17+
ss_string *ss_string_create_with_capacity(size_t capacity);
1818

19-
ss_string *KEEPALIVE ss_string_create(const char *literal);
19+
ss_string *ss_string_create(const char *literal);
2020

21-
void KEEPALIVE ss_string_delete(ss_string *str);
21+
void ss_string_delete(ss_string *str);
2222

23-
size_t KEEPALIVE ss_string_get_size(ss_string *str);
23+
size_t ss_string_get_size(ss_string *str);
2424

25-
long KEEPALIVE ss_string_grow_with_capacity(ss_string *str, size_t new_capacity);
25+
long ss_string_grow_with_capacity(ss_string *str, size_t new_capacity);
2626

27-
long KEEPALIVE ss_string_grow(ss_string *str);
27+
long ss_string_grow(ss_string *str);
2828

29-
long KEEPALIVE ss_string_append(ss_string *dest, ss_string *src);
29+
long ss_string_append(ss_string *dest, ss_string *src);
3030

31-
long KEEPALIVE ss_string_prepend(ss_string *dest, ss_string *src);
31+
long ss_string_prepend(ss_string *dest, ss_string *src);
3232

3333
ss_string *ss_string_concat(ss_string *str1, ss_string *str2);
3434

35-
ss_string *KEEPALIVE ss_string_slice(ss_string *str, ssize_t from, ssize_t to);
35+
ss_string *ss_string_slice(ss_string *str, ssize_t from, ssize_t to);
3636

37-
long KEEPALIVE ss_string_equals(ss_string *str1, ss_string *str2);
37+
long ss_string_equals(ss_string *str1, ss_string *str2);
3838

39-
ssize_t KEEPALIVE ss_string_index_of_with_literal(ss_string *str, const char *literal);
39+
ssize_t ss_string_index_of_with_literal(ss_string *str, const char *literal);
4040

41-
ssize_t KEEPALIVE ss_string_index_of(ss_string *str, ss_string *substr);
41+
ssize_t ss_string_index_of(ss_string *str, ss_string *substr);
4242

43-
long KEEPALIVE ss_string_trim_left(ss_string *str);
43+
long ss_string_trim_left(ss_string *str);
4444

45-
void KEEPALIVE ss_string_trim_right(ss_string *str);
45+
void ss_string_trim_right(ss_string *str);
4646

47-
long KEEPALIVE ss_string_trim(ss_string *str);
47+
long ss_string_trim(ss_string *str);

cmake/AddLLVM.cmake

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
if (CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
2-
find_package(LLVM 10 REQUIRED CONFIG)
3-
elseif (CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
1+
if (CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
42
list(APPEND CMAKE_PREFIX_PATH /usr/local/opt/llvm/lib/cmake/llvm)
5-
find_package(LLVM 11 REQUIRED CONFIG)
63
endif ()
74

8-
find_package(LLVM REQUIRED CONFIG)
5+
find_package(LLVM 11 REQUIRED CONFIG)
96

107
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
118

129
include_directories(${LLVM_INCLUDE_DIRS})
1310

1411
add_definitions(${LLVM_DEFINITIONS})
1512

16-
llvm_map_components_to_libnames(llvm_libs support core irreader linker nativecodegen)
13+
llvm_map_components_to_libnames(llvm_libs support core irreader linker native codegen nativecodegen Passes ObjCARCOpts)

examples/all.ss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ function func(a: number, b: number): number {
2525
return x + y;
2626
}
2727

28-
func(1, 2);
28+
ss_println_integer(func(1, 2));
2929
test();

include/AST/ASTBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "AST/TypeNode.h"
88
#include "AST/ExprNode.h"
99
#include "AST/ModuleNode.h"
10-
#include "Util/Alias.h"
10+
#include "Support/Alias.h"
1111

1212
/// AST构建器
1313
class ASTBuilder final : public StaticScriptParserVisitor {

include/AST/Node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <memory>
4-
#include "Util/Alias.h"
4+
#include "Support/Alias.h"
55

66
class ASTVisitor;
77

0 commit comments

Comments
 (0)