Skip to content

Commit 4271638

Browse files
committed
Merge branch 'memcontainer'
2 parents 1ee22f1 + e81530e commit 4271638

File tree

5 files changed

+219
-129
lines changed

5 files changed

+219
-129
lines changed

src/wsa/lib/memcontainer.wsa

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
include memory
2+
3+
jump memcontainer_lib_end
4+
5+
; args capacity
6+
; returns &M
7+
label memcontainer_new
8+
push 1
9+
call malloc
10+
; [cap,&M]
11+
swap
12+
call malloc
13+
; [&M,&D]
14+
copy 1
15+
swap
16+
store
17+
ret
18+
19+
; args &M
20+
label memcontainer_destroy
21+
dup
22+
retrieve
23+
; [&M,&D]
24+
call mfree
25+
pop
26+
call mfree
27+
pop
28+
ret
29+
30+
; args &M
31+
; returns cap
32+
label memcontainer_get_capacity
33+
retrieve
34+
; assuming internal stuff from memory but... who cares
35+
sub 1
36+
retrieve
37+
ret
38+
39+
; args &M cap
40+
label memcontainer_ensure_capacity
41+
swap
42+
dup
43+
retrieve
44+
copy 1
45+
call memcontainer_get_capacity
46+
; [minCap, &M, &D, cap]
47+
copy 3
48+
sub
49+
jumpn __memcontainer_ensure_capacity_move
50+
slide 2
51+
pop
52+
ret
53+
label __memcontainer_ensure_capacity_move
54+
; [minCap, &M, &D]
55+
copy 2
56+
dup
57+
div 2
58+
add
59+
call realloc
60+
; [minCap, &M, &D]
61+
store
62+
pop
63+
ret
64+
65+
label memcontainer_lib_end

src/wsa/lib/vector.wsa

Lines changed: 42 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,75 @@
11
include memory
2+
include memcontainer
23

34
jump lib_vector_end
45

56
; args capacity
67
; returns &V
78
label vector_new
8-
; Vec = [&data,cap,len]
9-
push 3
10-
call malloc
11-
swap
12-
; [&V, cap]
13-
dup
14-
call malloc
15-
; [&V, cap, &data]
16-
copy 2
17-
swap
18-
store
19-
; [&V, cap]
20-
copy 1
219
add 1
22-
swap
23-
store
24-
10+
call memcontainer_new
2511
dup
26-
add 2
12+
13+
; set length to 0
14+
retrieve
2715
push 0
2816
store
2917
ret
3018

3119
; args &V
3220
label vector_destroy
33-
dup
34-
call vector_get_addr
35-
call mfree
36-
pop
37-
call mfree
38-
pop
21+
call memcontainer_destroy
3922
ret
4023

4124
; args &V length value
4225
label vector_fill
4326
copy 2
4427
copy 2
45-
call __vector_ensure_capacity
28+
add 1
29+
call memcontainer_ensure_capacity
4630

4731
copy 2
48-
call vector_get_addr
49-
; [&V,length,value,&dest]
32+
retrieve
33+
add 1
34+
; [&V,length,value,&D]
5035
swap
5136
copy 2
52-
; [&V,length,&dest,value,size]
37+
; [&V,length,&D,value,size]
5338
call memset
5439

5540
; [&V,length]
56-
swap
57-
add 2
58-
swap
59-
store
41+
call vector_truncate ; aka set_length
6042
ret
6143

6244
; args &V
6345
; returns &data
6446
label vector_get_addr
6547
retrieve
48+
add 1
6649
ret
6750

6851
; args &V
6952
; returns capacity
7053
label vector_get_capacity
71-
add 1
72-
retrieve
54+
call memcontainer_get_capacity
7355
ret
7456

7557
; args &V
7658
; returns length
7759
label vector_get_length
78-
add 2
60+
retrieve
7961
retrieve
8062
ret
8163

64+
; args &V length
65+
; aka set_length
66+
label vector_truncate
67+
swap
68+
retrieve
69+
swap
70+
store
71+
ret
72+
8273
; args &V value
8374
label vector_push
8475
swap
@@ -87,15 +78,16 @@ label vector_push
8778
add 1
8879
copy 1
8980
copy 1
90-
call __vector_ensure_capacity
81+
add 1
82+
call memcontainer_ensure_capacity
9183

9284
; [value, &V, len+1]
93-
dup
94-
copy 2
95-
add 2
96-
swap
85+
copy 1
86+
retrieve
87+
copy 1
9788
store
9889

90+
; [value, &V, len+1]
9991
sub 1
10092
swap
10193
call vector_get_addr
@@ -120,22 +112,15 @@ label vector_pop
120112
; in case of out-of-bounds we don't have exceptions :( undefined behaviour, but keep length to 0 to avoid other complications
121113
jumpn __vector_pop_exit
122114
copy 2
123-
add 2
115+
retrieve
124116
copy 2
125117
store
126118
label __vector_pop_exit
127119
slide 2
128120
ret
129121

130-
; args &V length
131-
label vector_truncate
132-
swap
133-
add 2
134-
swap
135-
store
136-
ret
137122

138-
; args &V offset length
123+
; args: &V offset length
139124
; returns &Vslice
140125
label vector_slice
141126
dup
@@ -155,14 +140,13 @@ label vector_slice
155140
call memcpy
156141

157142
dup
158-
add 2
159143
copy 2
160-
store
144+
call vector_truncate ; aka set length
161145
slide 3
162146
ret
163147

164148
; args &Vtarget &Vsrc
165-
label vector_concat
149+
label vector_push_all
166150
copy 1
167151
call vector_get_length
168152
copy 1
@@ -171,7 +155,7 @@ label vector_concat
171155
dup
172156
copy 3
173157
swap
174-
call __vector_ensure_capacity
158+
call memcontainer_ensure_capacity
175159

176160
; [&V,&Vsrc,len]
177161
; memcpy args: source, dest, size
@@ -190,11 +174,9 @@ label vector_concat
190174
swap
191175
call memcpy
192176

177+
; [&V,&Vsrc,len]
193178
slide 1
194-
swap
195-
add 2
196-
swap
197-
store
179+
call vector_truncate ; aka set length
198180
ret
199181

200182
; args &V offset length
@@ -234,52 +216,14 @@ label vector_splice
234216
pop
235217
; [&V,offset,len,&Vspliced]
236218

237-
; set the length of &Vspliced
238-
copy 3
219+
dup
220+
copy 4
239221
call vector_get_length
240-
copy 2
222+
copy 3
241223
sub
242-
copy 1
243-
add 2
244-
swap
245-
store
224+
call vector_truncate ; aka set length
246225

247226
slide 3
248227
ret
249228

250-
; args &V capacity
251-
label __vector_ensure_capacity
252-
swap
253-
dup
254-
retrieve
255-
copy 1
256-
add 1
257-
retrieve
258-
; [minCap, &V, &addr, cap]
259-
copy 3
260-
sub
261-
jumpn __vector_ensure_capacity_move
262-
slide 2
263-
pop
264-
ret
265-
label __vector_ensure_capacity_move
266-
; [minCap, &V, &addr]
267-
copy 2
268-
dup
269-
div 2
270-
add
271-
swap
272-
copy 1
273-
call realloc
274-
; [minCap, &V, cap, &addr]
275-
copy 2
276-
swap
277-
store
278-
swap
279-
add 1
280-
swap
281-
store
282-
pop
283-
ret
284-
285229
label lib_vector_end

src/wsa/wsa.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import lib_bitwise_extensions from "./lib/bitwise.extensions.wsa?raw" with { type: "text" };
21
import lib_bitwise from "./lib/bitwise.wsa?raw" with { type: "text" };
2+
import lib_bitwise_extensions from "./lib/bitwise.extensions.wsa?raw" with { type: "text" };
33
import lib_io from "./lib/io.wsa?raw" with { type: "text" };
44
import lib_math from "./lib/math.wsa?raw" with { type: "text" };
5+
import lib_memcontainer from "./lib/memcontainer.wsa?raw" with { type: "text" };
56
import lib_memory from "./lib/memory.wsa?raw" with { type: "text" };
67
import lib_memory_stack from "./lib/memory_stack.wsa?raw" with { type: "text" };
78
import lib_vector from "./lib/vector.wsa?raw" with { type: "text" };
89

10+
const libraries: Record<string, string> = { lib_bitwise_extensions, lib_bitwise, lib_io, lib_math, lib_memory, lib_memory_stack, lib_vector, lib_memcontainer };
11+
912
type Opcode =
1013
| { params: "none"; constr: () => string }
1114
| { params: "integer"; constr: (x: bigint) => string }
@@ -238,29 +241,13 @@ async function include(
238241
if (includedFiles.has(filename)) return "";
239242
includedFiles.add(filename);
240243

241-
if (filename === "io") {
242-
return compile(stringToLineStream(lib_io), getIncludedStream);
243-
}
244-
if (filename === "memory") {
245-
return compile(stringToLineStream(lib_memory), getIncludedStream);
246-
}
247-
if (filename === "memory_stack") {
248-
return compile(stringToLineStream(lib_memory_stack), getIncludedStream);
249-
}
250-
if (filename === "bitwise") {
251-
return compile(
252-
stringToLineStream(
253-
extensions ? lib_bitwise_extensions : lib_bitwise
254-
),
255-
getIncludedStream
256-
);
257-
}
258-
if (filename === "math") {
259-
return compile(stringToLineStream(lib_math), getIncludedStream);
260-
}
261-
if (filename === "vector") {
262-
return compile(stringToLineStream(lib_vector), getIncludedStream);
244+
const libName = `lib_${filename}`;
245+
if (libName in libraries) {
246+
const content = extensions && (`${libName}_extensions` in libraries) ? libraries[`${filename}_extensions`] : libraries[libName]
247+
return compile(stringToLineStream(content), getIncludedStream);
263248
}
249+
console.log(libName, libraries);
250+
264251
return compile(getIncludedStream(filename), getIncludedStream);
265252
}
266253
function ret() {

wsa-tests/heap.wsa

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,5 @@ push 5
1515
retrieve
1616
outn
1717

18-
push 100
19-
store 0
20-
push 123
21-
store 101
22-
retrieve +1
23-
outn
24-
2518
;EXPECTED
26-
;553123
19+
;553

0 commit comments

Comments
 (0)