Skip to content

Commit 3e6af99

Browse files
committed
c
2 parents 5740855 + 954e198 commit 3e6af99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+30761
-27968
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v3
19-
- name: Build
20-
run: cargo build --verbose
2119
- name: Run tests
22-
run: cargo test --verbose
20+
run: shopt -s expand_aliases && alias cc='clang' && cargo test --verbose
2321
- name: Run deploy
24-
run: make deploy-lite
22+
run: make deploy-lite CC=clang

BOOTSTRAP/cli.c

Lines changed: 30208 additions & 27799 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
CC = cc
2+
CFLAGS = -O3 -march=native -mtune=native
23

34
dev: install-production
4-
lm SRC/index-index.lm
5+
lm tests/regress/seq-macro.lm
56
cc tmp.c
7+
./a.out
68

79
build: compile-production
810
time ./production --c -o deploy1.c SRC/index-index.lm
9-
$(CC) -O3 deploy1.c -o deploy1
11+
$(CC) $(CFLAGS) deploy1.c -o deploy1
1012
time ./deploy1 --c -o deploy2.c SRC/index-index.lm
1113
diff deploy1.c deploy2.c
1214
mv deploy1.c BOOTSTRAP/cli.c
@@ -16,8 +18,14 @@ build: compile-production
1618
deploy: build smoke-test
1719
deploy-lite: build smoke-test-lite
1820

21+
valgrind: install-bootstrap
22+
valgrind --tool=callgrind lm SRC/index-index.lm
23+
24+
valgrind-view:
25+
callgrind_annotate callgrind.out.6386
26+
1927
gprof:
20-
$(CC) -O3 -pg -o bootstrap.exe BOOTSTRAP/cli.c
28+
$(CC) $(CFLAGS) -pg -o bootstrap.exe BOOTSTRAP/cli.c
2129
./bootstrap.exe SRC/index-index.lm
2230

2331
gprof-view-count:
@@ -32,12 +40,12 @@ profile: install-bootstrap
3240

3341
compile-bootstrap:
3442
rm -f bootstrap.exe
35-
$(CC) -O3 -o bootstrap.exe BOOTSTRAP/cli.c
43+
$(CC) $(CFLAGS) -o bootstrap.exe BOOTSTRAP/cli.c
3644

3745
compile-production: compile-bootstrap
3846
rm -f production
3947
./bootstrap.exe --c -o production.c SRC/index-index.lm
40-
$(CC) -O3 -o production production.c
48+
$(CC) $(CFLAGS) -o production production.c
4149
rm -f production.c
4250

4351
install-production: compile-production
@@ -72,7 +80,7 @@ smoke-test: smoke-test-clang smoke-test-gcc smoke-test-musl
7280
smoke-test-lite: smoke-test-clang smoke-test-gcc
7381

7482
install:
75-
$(CC) -O3 -o lm BOOTSTRAP/cli.c
83+
$(CC) $(CFLAGS) -o lm BOOTSTRAP/cli.c
7684
ifeq ($(shell test -w /usr/local/bin; echo $$?), 0)
7785
mv lm /usr/local/bin/lm
7886
else

PLATFORM/C/LIB/array.lsts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,42 @@ declare-unop( $"&", raw-type(t), raw-type(t[]), (l"(&"; x; l")";) );
2727
declare-unop( raw, raw-type(t), raw-type(t), x );
2828
declare-unop( raw, raw-type(base-type[]), raw-type(base-type[]+Raw), x );
2929

30+
let mark-memory-as-safe(ptr: t[], len: U64): Nil = (
31+
# BEFORE CHANGING THIS: talk to alex
32+
33+
while len > 0_u64 {
34+
let ignored = ptr[0_u64];
35+
len = len - 1_u64;
36+
ptr = ((ptr as U8[]) + sizeof(t)) as t[];
37+
};
38+
);
39+
40+
## this will fail if len is 0
3041
let safe-alloc(len: U64, ty: Type<t>): t[] = (
42+
# BEFORE CHANGING THIS: talk to alex
43+
3144
let nb = len * sizeof(t);
32-
let ptr = malloc(nb);
45+
let ptr = malloc(nb) as t[];
3346
if ptr as U64 == 0_u64 {
3447
fail("malloc(\{nb}) fail");
3548
};
36-
ptr as t[]
49+
50+
mark-memory-as-safe(ptr, len);
51+
ptr
3752
);
3853

54+
## this will fail if len is 0
3955
let safe-realloc(ptr: t[], len: U64, ty: Type<t>): t[] = (
56+
# BEFORE CHANGING THIS: talk to alex
57+
4058
let nb = len * sizeof(t);
41-
let new_ptr = realloc(ptr as ?[], nb);
59+
let new_ptr = realloc(ptr as ?[], nb) as t[];
4260
if new_ptr as U64 == 0_u64 {
4361
fail("realloc(\{nb}) fail");
4462
};
45-
new_ptr as t[]
63+
64+
mark-memory-as-safe(new_ptr, len);
65+
new_ptr
4666
);
4767

4868
let close(x: p): p[] = (

PLATFORM/C/LIB/common-macros.lm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ macro ( ('for-each (item 'in iter) loop) ) (
4949
)))
5050
);
5151

52+
macro ( ('for-each-v (item 'in iter) loop) ) (
53+
(let (uuid iter-term) iter)
54+
(let (uuid iter-i) 0_u64)
55+
(let (uuid iter-length) (.length (uuid iter-term)))
56+
(while (<( (uuid iter-i) (uuid iter-length))) (match ([]( (uuid iter-term) (uuid iter-i) )) (
57+
()
58+
(item( loop (set (uuid iter-i) (+( (uuid iter-i) 1_u64 )) ) ))
59+
)))
60+
);
61+
5262
for-arg-i := λ: Blob(: vi x...). (: () x);
5363
macro ( ('for-arg (item 'in iter) loop) ) (
5464
(scope('for-arg_r (

PLATFORM/C/LIB/vector.lsts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11

22
## faster than [type(List<t>)]. should be used when you append or remove data a lot
3-
type Vector<t> = Vector { data: t[], length: U64, capacity: U64 };
3+
type Vector<t> = Vector { data: t[], _length: U32, capacity: U32 };
44
type Vector<t> implements Collection<t>;
55

6-
let mk-vector(ty: Type<t>): Vector<t> = (
7-
mk-vector(ty, 0_u64)
6+
let .length(v: Vector<t>): U64 = (
7+
v._length
88
);
99

1010
let mk-vector(type: Type<t>, capacity: U64): Vector<t> = (
11-
Vector { safe-alloc(capacity, type(t)), 0_u64, capacity }
11+
let ptr = if (capacity == 0_u64) then (0_u64 as t[]) else (safe-alloc(capacity, type(t)));
12+
Vector { ptr, 0_u32, capacity as U32 }
13+
);
14+
15+
let mk-vector(ty: Type<t>): Vector<t> = (
16+
mk-vector(ty, 0_u64)
1217
);
1318

1419
## generate a [type(Vector<t>)], in which each element is [value]
@@ -23,14 +28,14 @@ let fill-vector(value: t, len: U64): Vector<t> = (
2328

2429
## does not change the length length, or destroy old elements!
2530
let .realloc(v: Vector<t>, target-capacity: U64): Vector<t> = (
26-
let newp = safe-realloc(v.data, target-capacity, type(t));
27-
Vector { newp, v.length, target-capacity }
31+
let newp = if (target-capacity == 0_u64) then (0_u64 as t[]) else (safe-realloc(v.data, target-capacity, type(t)));
32+
Vector { newp, v.length as U32, target-capacity as U32 }
2833
);
2934

3035
## reserve additional num elements to the current length
3136
let .reserve-additional(v: Vector<t>, additional: U64): Vector<t> = (
32-
if (v.capacity - v.length) < additional {
33-
v.realloc(v.length + additional)
37+
if (v.capacity - v.length) < additional as U32 {
38+
v.realloc((v.length as U64) + additional)
3439
} else {
3540
v
3641
}
@@ -42,12 +47,12 @@ let .push(v: Vector<t>, i: t): Vector<t> = (
4247
let new-cap = if v.capacity == 0 {
4348
4_u64
4449
} else {
45-
(v.length >> 1_u64) + v.length # this is mul 1.5, not 3
50+
(v.length >> 1_u32) + v.length # this is mul 1.5, not 3
4651
};
47-
v = v.realloc(new-cap);
52+
v = v.realloc(new-cap as U64);
4853
};
49-
v.data[v.length] = i;
50-
v.length = v.length + 1_u64;
54+
v.data[v.length as U64] = i;
55+
v._length = v._length + 1_u32;
5156
v
5257
);
5358

@@ -80,7 +85,7 @@ let .remove-front(v: Vector<t>, num: U64): Vector<t> = (
8085
v[i-num] = v[i];
8186
i = i + 1;
8287
};
83-
v.length = v.length - num;
88+
v._length = v._length - (num as U32);
8489

8590
v.shrink();
8691
);
@@ -91,7 +96,7 @@ let .remove-back(v: Vector<t>, num: U64): Vector<t> = (
9196
fail("tried to remove \{num} elements from vector, but only have \{v.length()}");
9297
};
9398

94-
v.length = v.length - num;
99+
v._length = v._length - (num as U32);
95100

96101
v.shrink();
97102
);
@@ -115,14 +120,14 @@ let .pop(v: Vector<t>): Tuple<Vector<t>, t> = (
115120
};
116121

117122
let lasti = v.length - 1;
118-
let last = v[lasti];
123+
let last = v[lasti as U64];
119124
v = v.remove-back(1);
120125

121126
Tuple { v, last }
122127
);
123128

124129
let $"[]"( v: Vector<t>, i: U64 ): t = (
125-
if i >= v.length {
130+
if i >= (v.length as U64) {
126131
fail("Vector Index Out of Bounds");
127132
};
128133
v.data[i]
@@ -131,9 +136,9 @@ let $"[]"( v: Vector<t>, i: U64 ): t = (
131136
let $"+"( vl: Vector<t>, vr: Vector<t> ): t = (
132137
let v = mk-vector(type(t), vl.length + vr.length);
133138
let vi = 0_u64;
134-
while vi < vl.length { v = v.push(vl[vi]); };
139+
while vi < (vl.length as U64) { v = v.push(vl[vi]); vi = vi + 1; };
135140
vi = 0_u64;
136-
while vi < vr.length { v = v.push(vr[vi]); };
141+
while vi < (vr.length as U64) { v = v.push(vr[vi]); vi = vi + 1; };
137142
v
138143
);
139144

PLUGINS/BACKEND/C/compile-c-typedef.lm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ is-incomplete-typedef := λ(: tt Type). (: (
5959
( (TGround( 'Cons_s (LCons( p1 (LCons( p2 LEOF )) )) )) (
6060
(set r (||( (is-incomplete-typedef p1) (is-incomplete-typedef p2) )))
6161
))
62-
( (TAnd( lt rt )) (
63-
(set r (||( (is-incomplete-typedef lt) (is-incomplete-typedef rt) )))
62+
( (TAnd( conjugate )) (
63+
(for-each-v (c in conjugate) (set r (||( r (is-incomplete-typedef c) ))))
6464
))
6565
( TAny () )
6666
( (TVar _) () )

PLUGINS/BACKEND/C/compile-expr-direct.lm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ compile-expr-direct := λ(: ctx FContext)(: term AST)(: stack-offset I64)(: used
4141
(+( (mangle-c-type tt) (SAtom '\]\]_s) ))
4242
))
4343
)))
44-
(let et (TAnd(
45-
(close(t1 'Literal_s))
46-
(close(TAnd(
47-
(close(t1 'Constant_s))
48-
(close(t1 'U64_s))
49-
)))
44+
(let et (&&(
45+
(t1 'Literal_s)
46+
(&&(
47+
(t1 'Constant_s)
48+
(t1 'U64_s)
49+
))
5050
)))
5151
(set.type( e et ))
5252
(set.context( e (close ctx) ))

PLUGINS/BACKEND/C/is-const-array.lm

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ is-const-array := λ(: tt Type). (: (
33
(let r 0_u64)
44
(match tt (
55
()
6-
( (TAnd( t1 t2 )) (set r (||( (is-const-array( t1 )) (is-const-array( t2 )) ))) )
6+
( (TAnd( conjugate )) (
7+
(for-each-v (c in conjugate) (
8+
(set r (||( r (is-const-array( c )) )))
9+
))
10+
))
711
( (TGround( 'Array_s (LCons( (TGround( 'CONST_s _ )) (LCons( array-base _ )) )) )) (set r 1_u64) )
812
( _ () )
913
))

PLUGINS/BACKEND/C/mangle-c-type.lm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ mangle-c-type-internal := λ(: tt Type). (: (
1010
(let r SNil)
1111
(match tt (
1212
()
13-
( (TAnd( lt rt )) (
14-
(set r (mangle-c-type lt))
15-
(if (non-zero r) () (set r (mangle-c-type rt)))
13+
( (TAnd( conjugate )) (
14+
(for-each-v (c in conjugate) (
15+
(if (non-zero r) () (set r (mangle-c-type c)))
16+
))
1617
))
1718
( TAny (set r (SAtom 'void_s)) )
1819
( (TVar _) () )

0 commit comments

Comments
 (0)