Skip to content

Commit a5bdd4a

Browse files
Merge pull request #211 from andrew-johnson-4/inline-constructors-1
Inline constructors 1
2 parents 8462597 + 6bb3f09 commit a5bdd4a

File tree

9 files changed

+12247
-12915
lines changed

9 files changed

+12247
-12915
lines changed

BOOTSTRAP/cli.s

+12,132-12,839
Large diffs are not rendered by default.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lambda_mountain"
3-
version = "1.11.73"
3+
version = "1.11.74"
44
authors = ["Andrew <andrew@subarctic.org>"]
55
license = "MIT"
66
description = "Lambda Mountain"

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
nostd: prod
3-
./production --nostd -o tmp.s tests/strict/cc2.lm
3+
./production --nostd -o tmp.s tests/nostd/write_function2.lm
4+
# ./production --nostd -o tmp.s tests/strict/cc2.lm
45
as -o tmp.o tmp.s
56
ld -o tmp tmp.o
67
./tmp && echo $?

PRODUCTION/codegen-strict.lm

+2-2
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ strict-bind-accessors := λ ctx base-type src-type field-offset field-number . (
479479
(App( (App( (Literal ':) (Variable 'src) )) (Literal LocalVariable) ))
480480
(App(
481481
(App( (Variable '.program) (
482-
\t 'movq \s ('+( ('.expression 'src) field-offset )) \s \[ '%rbp \] ,
483-
\s '%r15 \n
482+
\t 'mov \s ('+( ('.expression 'src) field-offset )) \s \[ '%rbp \] ,
483+
\s '% register-name \n
484484
)))
485485
(App( (Variable '.expression) (
486486
register-name

PRODUCTION/fragment.lm

+20
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ fragment-substitute-context := λ ctx fragment-rhs . (match fragment-rhs (
8989
))
9090
result
9191
)))
92+
( (App(
93+
(App(
94+
(App(
95+
(Variable 'if-eq)
96+
l
97+
))
98+
r
99+
))
100+
body
101+
)) (tail(
102+
(set l (fragment-substitute-context( ctx l )))
103+
(set r (fragment-substitute-context( ctx r )))
104+
(local result)
105+
(if (eq( l r )) (
106+
(set result (
107+
(fragment-substitute-context( ctx body ))
108+
))
109+
) ())
110+
result
111+
)))
92112
( ('.expression v ) (tail(
93113
(if (is-atom v) (
94114
(while ctx (

PRODUCTION/stack.lm

+32-60
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,34 @@ stack-call := λ ctx function-name function-args offset . (tail(
1717
(local src-offset)
1818
(local copy-offset)
1919
(set src-offset (s2i 8)) # stack pointer is 64 bits
20-
(match (expr::get-expr e-args) (
21-
()
22-
( 1 (tail(
23-
(set copy-offset (add( (s2i '-1) copy-offset )))
24-
(set copy-bytes ( copy-bytes
25-
\t 'mov \s (i2s( src-offset )) \[ '%rbp \] , \s '%al \n
26-
\t 'mov \s '%al , \s (i2s( copy-offset )) \[ '%rbp \] \n
27-
))
28-
(set src-offset (add( src-offset (s2i 1) )))
29-
)))
30-
( 2 (tail(
31-
(set copy-offset (add( (s2i '-2) copy-offset )))
32-
(set copy-bytes ( copy-bytes
33-
\t 'mov \s (i2s( src-offset )) \[ '%rbp \] , \s '%ax \n
34-
\t 'mov \s '%ax , \s (i2s( copy-offset )) \[ '%rbp \] \n
35-
))
36-
(set src-offset (add( src-offset (s2i 2) )))
37-
)))
38-
( 4 (tail(
39-
(set copy-offset (add( (s2i '-4) copy-offset )))
40-
(set copy-bytes ( copy-bytes
41-
\t 'mov \s (i2s( src-offset )) \[ '%rbp \] , \s '%eax \n
42-
\t 'mov \s '%eax , \s (i2s( copy-offset )) \[ '%rbp \] \n
43-
))
44-
(set src-offset (add( src-offset (s2i 4) )))
45-
)))
46-
( copy-size (tail(
47-
(set copy-size (s2i copy-size))
48-
(set copy-offset (inv( copy-size )))
49-
(while copy-size (
50-
(set copy-bytes ( copy-bytes
51-
\t 'mov \s (i2s( src-offset )) \[ '%rbp \] , \s '%rax \n
52-
\t 'mov \s '%rax , \s (i2s( copy-offset )) \[ '%rbp \] \n
53-
))
54-
(set copy-offset (add( (s2i '8) copy-offset )))
55-
(set src-offset (add( (s2i '8) src-offset )))
56-
(set copy-size (add( (s2i '-8) copy-size )))
57-
))
58-
)))
20+
(local copy-size)
21+
(set copy-size (s2i (expr::get-expr e-args)))
22+
(set copy-offset (inv( copy-size )))
23+
(while copy-size (
24+
(set copy-bytes ( copy-bytes
25+
\t 'mov \s (i2s( src-offset )) \[ '%rbp \] , \s '%rax \n
26+
\t 'mov \s '%rax , \s (i2s( copy-offset )) \[ '%rbp \] \n
27+
))
28+
(set copy-offset (add( (s2i '8) copy-offset )))
29+
(set src-offset (add( (s2i '8) src-offset )))
30+
(set copy-size (add( (s2i '-8) copy-size )))
31+
))
32+
(local args-size)
33+
(set args-size (expr::get-expr e-args))
34+
(if (eq( args-size 0 )) () (
35+
(set args-size (max( '8 args-size )))
5936
))
6037
(set e-call (expr::set-prog( e-call (
6138
(expr::get-frame e-args)
6239
(expr::get-prog e-args)
6340
(enter-function())
64-
(if (eq( (expr::get-expr e-args) 0 )) () (
65-
( \t 'sub \s '$ (expr::get-expr e-args) , \s '%rsp \n )
41+
(if (eq( args-size 0 )) () (
42+
( \t 'sub \s '$ args-size , \s '%rsp \n )
6643
))
6744
copy-bytes
6845
(\t 'call \s (mangle-identifier( function-name (typecheck-lookup body) )) \n)
69-
(if (eq( (expr::get-expr e-args) 0 )) () (
70-
( \t 'add \s '$ (expr::get-expr e-args) , \s '%rsp \n )
46+
(if (eq( args-size 0 )) () (
47+
( \t 'add \s '$ args-size , \s '%rsp \n )
7148
))
7249
(\t 'mov \s '%rbp, \s '%rsp \n
7350
\t 'pop \s '%rbp \n)
@@ -136,39 +113,32 @@ stack-call-push-arg := λ ctx e-arg offset args-size . (tail(
136113
(assert-typeof( 'stack-call-push-arg::args-size args-size Atom ))
137114
(local e1)
138115
(local e2)
139-
(local e3)
140116
(local et)
141117
(set et (typecheck-lookup e-arg))
142118
(local e-sz)
143119
(set e-sz (typecheck-sizeof et))
120+
(set e-sz (max( 8 e-sz )))
144121
(set args-size (i2s(add( (s2i args-size) (s2i e-sz) ))) )
145122
(local new-offset)
146123
(set new-offset( (i2s(add( (s2i offset) (inv(s2i( e-sz ))) ))) ))
147-
148-
(local frame)
124+
149125
(local unframe)
150126
(if (eq( e-sz '0 )) () (
151-
(set frame ( \t 'sub \s '$ e-sz , \s '%rsp \n ))
152-
(set unframe ( \t 'add \s '$ e-sz , \s '%rsp \n ))
127+
(set unframe ( \t 'add \s '$ (max( '8 e-sz )) , \s '%rsp \n ))
153128
))
154129

155130
(set e1 (compile-expr-strict( ctx e-arg new-offset Used )))
156131
(set e1 (expr::set-type( e1 (typecheck-lookup e-arg))))
157-
(set e2 (expr::new()))
158-
(set e2 (expr::set-expr( e2 new-offset )))
159-
(set e2 (expr::set-context( e2 ctx )))
160-
(set e2 (expr::set-offset( e2 new-offset )))
161-
(set e2 (expr::set-type( e2 (And( LocalVariable (Sized e-sz) )) )))
162132

163-
(set e3 (fragment-apply(
164-
ctx 'mov (Cons( (typecheck-lookup e-arg) (And( LocalVariable (Sized e-sz) )) ))
165-
((() e1) e2) e2
133+
(set e2 (fragment-apply(
134+
ctx 'push (typecheck-lookup e-arg)
135+
(() e1) e1
166136
)))
167137

168-
(set e3 (expr::set-frame( e3 ((expr::get-frame e3) frame) )))
169-
(set e3 (expr::set-unframe( e3 ((expr::get-unframe e3) unframe) )))
170-
(set e3 (expr::set-expr( e3 args-size )))
171-
e3
138+
(set e2 (expr::set-unframe( e2 ((expr::get-unframe e2) unframe) )))
139+
(set e2 (expr::set-expr( e2 args-size )))
140+
(set e2 (expr::set-offset( e2 new-offset )))
141+
e2
172142
));
173143

174144
stack-destructure-args := λ ctx function-args offset arg-mode . (tail(
@@ -227,6 +197,7 @@ stack-define-destructure := λ ctx lhs offset . (match lhs (
227197
(set ltype (typecheck-infer-type-compound ltype) )
228198
(local size)
229199
(set size (typecheck-sizeof ltype))
200+
(set size (max( '8 size )))
230201
(local new_offset)
231202
(set new_offset (add( offset (inv(s2i size)) )) )
232203
(local return)
@@ -242,6 +213,7 @@ stack-define-destructure := λ ctx lhs offset . (match lhs (
242213
(set ltype (typecheck-infer-type-compound ltype) )
243214
(local size)
244215
(set size (typecheck-sizeof ltype))
216+
(set size (max( '8 size )))
245217
(local new_offset)
246218
(set new_offset (add( offset (inv(s2i size)) )) )
247219
(local more-e)

PRODUCTION/typecheck.lm

+28-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ typecheck-infer-expr := λctx e used . (tail(
736736
(local ctype)
737737
(set ctype (typecheck-infer-constructor-type lit))
738738
(if ctype (
739-
(typecheck-ascript( e (And( Constant (And( lit ctype )) )) ))
739+
(typecheck-ascript( e (And( (And( lit ctype )) Constant )) ))
740740
) (
741741
(typecheck-ascript( e Atom ))
742742
))
@@ -873,6 +873,33 @@ typecheck-sizeof-impl := λtt . (match tt (
873873
)))
874874
));
875875

876+
typecheck-assign-representation := λtt . (tail(
877+
(print-s (AssignRepr tt))(print-s \n)
878+
(if (not(typecheck-has-representation tt)) (
879+
(set tt (And( LocalVariable tt )))
880+
(print-s (Repr tt))(print-s \n)
881+
) ())
882+
tt
883+
));
884+
885+
typecheck-has-representation := λtt . (match tt (
886+
()
887+
( (lt rt) (
888+
if (typecheck-has-representation lt) True
889+
(typecheck-has-representation rt)
890+
))
891+
( () () )
892+
( Constant True )
893+
( Reg8 True )
894+
( Reg16 True )
895+
( Reg32 True )
896+
( Reg64 True )
897+
( LocalVariable True )
898+
( StackVariable True )
899+
( GlobalVariable True )
900+
( _ () )
901+
));
902+
876903
typecheck-not-representation := λtt . (match tt (
877904
()
878905
( (And( Constant rt )) (typecheck-not-representation rt) )

STDLIB/default-instruction-set.lm

+24-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ fragment type Reg64 RAX | RBX | RCX | RDX | RSI | RDI | RBP | RSP | R8 | R9 |
66

77
fragment syscall := λ. (: (.program( \t 'syscall \n )) Nil);
88

9-
fragment push := λ(: l Constant+U64). (: ( (.program( \t 'pushq \s '$ (.expression l) \n)) ) Nil);
10-
fragment push := λ(: l Constant+I64). (: ( (.program( \t 'pushq \s '$ (.expression l) \n)) ) Nil);
11-
129
fragment cdecl::return := λ(: l Nil). (: () Nil);
1310
fragment cdecl::return := λ(: l Reg8). (: ( (.program( \t 'mov \t '% (.expression l) , '%al \n)) (.expression 'al) ) Nil);
1411
fragment cdecl::return := λ(: l Reg16). (: ( (.program( \t 'mov \t '% (.expression l) , '%ax \n)) (.expression 'ax) ) Nil);
@@ -42,11 +39,29 @@ fragment push := λ(: src GlobalVariable+Sized<size>). (: (
4239
\t 'pushq \s '0 \[ '%r15 \] \n
4340
\t 'add \s '$8 , \s '%r15 \n
4441
))
42+
(if-eq size 4 (
43+
\t 'mov \s '0 \[ '%r15 \] , \s '%r15d \n
44+
\t 'push \s '%r15 \n
45+
))
46+
(if-eq size 2 (
47+
\t 'mov \s '0 \[ '%r15 \] , \s '%r15w \n
48+
\t 'push \s '%r15 \n
49+
))
50+
(if-eq size 2 (
51+
\t 'mov \s '0 \[ '%r15 \] , \s '%r15b \n
52+
\t 'push \s '%r15 \n
53+
))
4554
))
4655
) Nil);
4756
fragment push := λ(: src LocalVariable+Sized<size>). (: (
4857
(.program ( for word-offset in (range( 0 (/( size 8 )) )) (
49-
\t 'pushq \s (+( (.expression src) (*( word-offset 8 )) )) \[ '%rbp \] \n
58+
\t 'pushq \s (+(
59+
(.expression src)
60+
(+(
61+
(+( size '-8 ))
62+
(inv(*( word-offset 8 )))
63+
))
64+
)) \[ '%rbp \] \n
5065
)))
5166
) Nil);
5267
fragment pop := λ(: src LocalVariable+Sized<size>). (: (
@@ -150,6 +165,11 @@ fragment mov := λ(: src LocalVariable)(: dst Reg16). (: (.program( \t 'mov \s (
150165
fragment mov := λ(: src LocalVariable)(: dst Reg32). (: (.program( \t 'mov \s (.expression src) \[ '%rbp \] , \s '% (.expression dst) \n )) Nil);
151166
fragment mov := λ(: src LocalVariable)(: dst Reg64). (: (.program( \t 'mov \s (.expression src) \[ '%rbp \] , \s '% (.expression dst) \n )) Nil);
152167

168+
fragment push := λ(: dst Reg8). (: (.program( \t 'mov \s '% (.expression dst) , '%r15b \n \t 'push \s '%r15 \n )) Nil);
169+
fragment push := λ(: dst Reg16). (: (.program( \t 'mov \s '% (.expression dst) , '%r15w \n \t 'push \s '%r15 \n )) Nil);
170+
fragment push := λ(: dst Reg32). (: (.program( \t 'mov \s '% (.expression dst) , '%r15d \n \t 'push \s '%r15 \n )) Nil);
171+
fragment push := λ(: dst Reg64). (: (.program( \t 'push \s '% (.expression dst) \n )) Nil);
172+
153173
fragment mov := λ(: offset Constant)(: src Reg64)(: dst Reg8). (: (.program( \t 'mov \s (.expression offset) \[ '% (.expression src) \] , \s '% (.expression dst) \n )) Nil);
154174

155175
fragment cmp := λ(: imm Constant+Literal+Sized<8>)(: r Constant+Reg64). (: (.program( \t 'cmp \s '$ (.expression imm) , '% (.expression r) \n )) Nil);

tests/strict/cc2.lm

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ echo := λ(: x U64). (: (
88
x
99
) U64);
1010

11-
main := (tail(
11+
main := (
1212
(let l1 (SAtom( 'an-atom_s )))
13-
(let l2 (close l1))
13+
(let l2 (close(l1)))
1414
(let r1 (SNil))
15-
(let r2 (close r1))
15+
(let r2 (close(r1)))
1616
(let x (SCons( l2 r2 )))
1717
(print x)
18-
()
1918
# (print (SCons(
20-
# (SAtom( 'an-atom_s ))
21-
# (SNil)
19+
# (close(SAtom( 'an-atom_s )))
20+
# (close(SNil))
2221
# )))
23-
));
22+
);

0 commit comments

Comments
 (0)