abc-interpreter-util.wat 42.3 KB
Newer Older
1 2
(module
	(import "clean" "memory" (memory 1))
Camil Staps's avatar
Camil Staps committed
3
	;;(func $debug (import "clean" "debug") (param i32 i32 i32 i32))
4

5
	;; to get host references during garbage collection
6 7 8
	;; has-host-reference should return a heap pointer when the requested index
	;;   is found, 0 if it is not found and no higher index is known, or -1 if
	;;   it is not found but higher indexes may be known
9 10 11
	(func $has-host-reference (import "clean" "has_host_reference") (param i32) (result i32))
	(func $update-host-reference (import "clean" "update_host_reference") (param i32 i32))

12 13 14 15 16 17 18 19
	(func $gc-start (import "clean" "gc_start"))
	(func $gc-end (import "clean" "gc_end"))
	;; When a node with the js-ref-constructor is found, the callback is called
	;; with its argument. This is used in iTasks to clean up the references from
	;; wasm to JS.
	(global $js-ref-constructor (mut i32) (i32.const 0))
	(func $js-ref-found (import "clean" "js_ref_found") (param i32))

20 21 22 23
	(func $get-asp (import "clean" "get_asp") (result i32))
	(func $set-hp (import "clean" "set_hp") (param i32))
	(func $set-hp-free (import "clean" "set_hp_free") (param i32))

24 25 26
	(global $start-heap (mut i32) (i32.const 0))
	(global $half-heap (mut i32) (i32.const 0))
	(global $end-heap (mut i32) (i32.const 0))
27
	(global $stack (mut i32) (i32.const 0))
28
	(global $caf-list (mut i32) (i32.const 0))
29 30
	(global $in-first-semispace (mut i32) (i32.const 1))

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
	(func (export "strncmp") (param $s1 i32) (param $s2 i32) (param $n i32) (result i32)
		(local $n4 i32)
		(local $c1 i32)
		(local $c2 i32)

		(local.set $n4 (i32.shr_u (local.get $n) (i32.const 2)))

		(block $end-check-words
			(br_if $end-check-words (i32.eqz (local.get $n4)))
			(loop $check-words
				(br_if $end-check-words (i32.eqz (local.get $n4)))

				(local.set $c1 (i32.load8_s (local.get $s1)))
				(local.set $c2 (i32.load8_s (local.get $s2)))
				(if
					(i32.or (i32.eqz (local.get $c1)) (i32.ne (local.get $c1) (local.get $c2)))
					(then (return (i32.sub (local.get $c1) (local.get $c2))))
				)

				(local.set $c1 (i32.load8_s offset=1 (local.get $s1)))
				(local.set $c2 (i32.load8_s offset=1 (local.get $s2)))
				(if
					(i32.or (i32.eqz (local.get $c1)) (i32.ne (local.get $c1) (local.get $c2)))
					(then (return (i32.sub (local.get $c1) (local.get $c2))))
				)

				(local.set $c1 (i32.load8_s offset=2 (local.get $s1)))
				(local.set $c2 (i32.load8_s offset=2 (local.get $s2)))
				(if
					(i32.or (i32.eqz (local.get $c1)) (i32.ne (local.get $c1) (local.get $c2)))
					(then (return (i32.sub (local.get $c1) (local.get $c2))))
				)

				(local.set $c1 (i32.load8_s offset=3 (local.get $s1)))
				(local.set $c2 (i32.load8_s offset=3 (local.get $s2)))
				(if
					(i32.or (i32.eqz (local.get $c1)) (i32.ne (local.get $c1) (local.get $c2)))
					(then (return (i32.sub (local.get $c1) (local.get $c2))))
				)

				(local.set $s1 (i32.add (local.get $s1) (i32.const 4)))
				(local.set $s2 (i32.add (local.get $s2) (i32.const 4)))
				(local.set $n4 (i32.sub (local.get $n4) (i32.const 1)))

				(br $check-words)
			)
		)

		(local.set $n (i32.and (local.get $n) (i32.const 3)))
		(block $end-check-bytes
			(loop $check-bytes
				(br_if $end-check-bytes (i32.eqz (local.get $n)))

				(local.set $c1 (i32.load8_u (local.get $s1)))
				(local.set $c2 (i32.load8_u (local.get $s2)))
				(if
					(i32.or (i32.eqz (local.get $c1)) (i32.ne (local.get $c1) (local.get $c2)))
					(then (return (i32.sub (local.get $c1) (local.get $c2))))
				)

				(local.set $s1 (i32.add (local.get $s1) (i32.const 1)))
				(local.set $s2 (i32.add (local.get $s2) (i32.const 1)))
				(local.set $n (i32.sub (local.get $n) (i32.const 1)))

				(br $check-bytes)
			)
		)

		(i32.sub (local.get $c1) (local.get $c2))
	)

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
	(func (export "memcpy") (param $dest i32) (param $src i32) (param $nbytes i32)
		(local $i i32)
		(local.set $i (i32.shr_u (local.get $nbytes) (i32.const 3)))
		(local.set $nbytes (i32.and (local.get $nbytes) (i32.const 0x7)))

		(block $end-copy-words
			(loop $copy-words
				(br_if $end-copy-words (i32.eqz (local.get $i)))
				(i64.store (local.get $dest) (i64.load (local.get $src)))
				(local.set $dest (i32.add (local.get $dest) (i32.const 8)))
				(local.set $src (i32.add (local.get $src) (i32.const 8)))
				(local.set $i (i32.sub (local.get $i) (i32.const 1)))
				(br $copy-words)
			)
		)

		(block $end
			(br_if $end (i32.eqz (local.get $nbytes)))
			(i64.store8 (local.get $dest) (i64.load8_u (local.get $src)))
			(br_if $end (i32.eq (local.get $nbytes) (i32.const 1)))
			(i64.store8 offset=1 (local.get $dest) (i64.load8_u offset=1 (local.get $src)))
			(br_if $end (i32.eq (local.get $nbytes) (i32.const 2)))
			(i64.store8 offset=2 (local.get $dest) (i64.load8_u offset=2 (local.get $src)))
			(br_if $end (i32.eq (local.get $nbytes) (i32.const 3)))
			(i64.store8 offset=3 (local.get $dest) (i64.load8_u offset=3 (local.get $src)))
			(br_if $end (i32.eq (local.get $nbytes) (i32.const 4)))
			(i64.store8 offset=4 (local.get $dest) (i64.load8_u offset=4 (local.get $src)))
			(br_if $end (i32.eq (local.get $nbytes) (i32.const 5)))
			(i64.store8 offset=5 (local.get $dest) (i64.load8_u offset=5 (local.get $src)))
			(br_if $end (i32.eq (local.get $nbytes) (i32.const 6)))
			(i64.store8 offset=6 (local.get $dest) (i64.load8_u offset=6 (local.get $src)))
		)
	)

136
	(func (export "setup_gc") (param i32 i32 i32 i32)
137 138 139
		(global.set $start-heap (local.get 0))
		(global.set $half-heap (i32.add (local.get 0) (local.get 1)))
		(global.set $end-heap (i32.add (local.get 0) (i32.add (local.get 1) (local.get 1))))
140
		(global.set $stack (i32.sub (local.get 2) (i32.const 8)))
141
		(global.set $caf-list (local.get 3))
142
	)
143 144 145
	(func (export "set_js_ref_constructor") (param i32)
		(global.set $js-ref-constructor (local.get 0))
	)
146

Camil Staps's avatar
Camil Staps committed
147 148 149 150 151 152 153 154
	(func (export "get_unused_semispace") (result i32)
		(select
			(global.get $half-heap)
			(global.get $start-heap)
			(global.get $in-first-semispace)
		)
	)

155 156
	(func (export "gc")
		(local $asp i32)
157 158 159 160 161
		(local $old i32)
		(local $new i32)
		(local $n i32)
		(local $d i32)
		(local $a-arity i32)
162
		(local $arity i32)
163
		(local $size i32)
164

165 166
		(call $gc-start)

167 168
		(local.set $asp (call $get-asp))

169 170 171 172 173 174 175 176 177 178 179 180 181
		(if
			(global.get $in-first-semispace)
			(then
				(local.set $old (global.get $start-heap))
				(local.set $new (global.get $half-heap)))
			(else
				(local.set $old (global.get $half-heap))
				(local.set $new (global.get $start-heap)))
		)

		;; copy A stack pointers
		(block $end-copy-asp
			(loop $copy-asp
182 183
				(br_if $end-copy-asp (i32.eq (local.get $asp) (global.get $stack)))
				(local.set $new (call $update-ref (local.get $asp) (local.get $new)))
184 185 186 187 188
				(local.set $asp (i32.sub (local.get $asp) (i32.const 8)))
				(br $copy-asp)
			)
		)

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
		;; copy CAF pointers
		(local.set $n (i32.add (global.get $caf-list) (i32.const 8)))
		(block $end-copy-cafs
			(loop $copy-cafs
				(br_if $end-copy-cafs (i32.eqz (i32.load (i32.sub (local.get $n) (i32.const 8)))))
				(local.set $n (i32.load (i32.sub (local.get $n) (i32.const 8))))
				(local.set $a-arity (i32.load16_s (local.get $n)))
				(block $end-copy-caf-pointers
					(loop $copy-caf-pointers
						(br_if $end-copy-caf-pointers (i32.eqz (local.get $a-arity)))
						(local.set $new (call $update-ref (i32.add (local.get $n) (i32.shl (local.get $a-arity) (i32.const 3))) (local.get $new)))
						(local.set $a-arity (i32.sub (local.get $a-arity) (i32.const 1)))
						(br $copy-caf-pointers)
					)
				)
				(br $copy-cafs)
			)
		)

208 209 210 211 212 213
		;; copy host references
		(local.set $arity (i32.load (local.get $asp))) ;; to restore
		(local.set $d (i32.const 0))
		(block $end-copy-host-references
			(loop $copy-host-references
				(local.set $n (call $has-host-reference (local.get $d)))
214

215
				(br_if $end-copy-host-references (i32.eqz (local.get $n)))
216 217 218 219 220 221 222 223 224 225

				(if
					(i32.gt_s (local.get $n) (i32.const 0))
					(then
						(i32.store (local.get $asp) (local.get $n))
						(local.set $new (call $update-ref (local.get $asp) (local.get $new)))
						(call $update-host-reference (local.get $d) (i32.load (local.get $asp)))
					)
				)

226 227 228 229 230 231
				(local.set $d (i32.add (local.get $d) (i32.const 1)))
				(br $copy-host-references)
			)
		)
		(i32.store (local.get $asp) (local.get $arity))

232 233 234 235 236 237 238 239 240 241
		;; update references
		(local.set $n (select (global.get $half-heap) (global.get $start-heap) (global.get $in-first-semispace)))
		(block $end-update-refs
			(loop $update-refs
				(br_if $end-update-refs (i32.ge_u (local.get $n) (local.get $new)))

				(local.set $d (i32.load (local.get $n)))

				(if
					(i32.and (local.get $d) (i32.const 2))
242
					;; hnf
243 244 245 246
					(then
						(if
							(i32.or
								(i32.eq (local.get $d) (i32.const 210)) ;; INT+2
247 248
								(i32.eq (local.get $d) (i32.const 170)) ;; REAL+2
								;; BOOL and CHAR are always static so cannot occur here
249 250 251 252 253 254
							)
							(then
								(local.set $n (i32.add (local.get $n) (i32.const 16)))
								(br $update-refs)
							)
						)
255 256 257 258
						(if
							;; _STRING_+2
							(i32.eq (local.get $d) (i32.const 50))
							(then
259 260 261
								(local.set $size (i32.load offset=8 (local.get $n))) ;; size
								(local.set $size (i32.shr_u (i32.add (local.get $size) (i32.const 7)) (i32.const 3)))
								(local.set $n (i32.add (local.get $n) (i32.add (i32.shl (local.get $size) (i32.const 3)) (i32.const 16))))
262 263 264
								(br $update-refs)
							)
						)
265 266 267 268
						(if
							;; _ARRAY_+2
							(i32.eq (local.get $d) (i32.const 10))
							(then
269
								(local.set $size (i32.load offset=8 (local.get $n))) ;; size
270 271
								(local.set $d (i32.load offset=16 (local.get $n))) ;; child descriptor
								(if
272
									(i32.or ;; unboxed INT/REAL
273
										(i32.eq (local.get $d) (i32.const 170))
274
										(i32.eq (local.get $d) (i32.const 210))
275 276
									)
									(then
277 278
										(local.set $n (i32.add (local.get $n) (i32.add (i32.const 24)
											(i32.shl (local.get $size) (i32.const 3)))))
279 280 281
										(br $update-refs)
									)
								)
282 283 284
								(if ;; unboxed BOOL
									(i32.eq (local.get $d) (i32.const 90))
									(then
285 286
										(local.set $n (i32.add (local.get $n) (i32.add (i32.const 24)
											(i32.shl (local.get $size) (i32.const 6))))) ;; 3+3
287 288 289 290
										(br $update-refs)
									)
								)

291
								(if
292 293
									(i32.eqz (local.get $d))
									(then
294
										(local.set $arity (i32.const 1))
295 296 297
										(local.set $a-arity (i32.const 1))
									)
									(else ;; unboxed record
298
										(local.set $arity (i32.sub (i32.load16_s (i32.sub (local.get $d) (i32.const 2))) (i32.const 256)))
299 300 301 302 303 304 305 306
										(local.set $a-arity (i32.load16_s (local.get $d)))
									)
								)

								(if
									(i32.eqz (local.get $a-arity))
									(then
										(local.set $n (i32.add (local.get $n) (i32.add (i32.const 24)
307
											(i32.shl (i32.mul (local.get $size) (local.get $arity)) (i32.const 3)))))
308
										(br $update-refs)
309
									)
310
								)
311

312
								(local.set $n (i32.add (local.get $n) (i32.const 24)))
313
								(local.set $arity (i32.shl (local.get $arity) (i32.const 3)))
314
								(local.set $a-arity (i32.shl (local.get $a-arity) (i32.const 3)))
315
								(loop $update-array
316
									(br_if $update-refs (i32.eqz (local.get $size)))
317

318
									(local.set $d (i32.const 0))
319 320
									(block $end-update-array-pointers
										(loop $update-array-pointers
321 322 323
											(br_if $end-update-array-pointers (i32.eq (local.get $d) (local.get $a-arity)))
											(local.set $new (call $update-ref (i32.add (local.get $n) (local.get $d)) (local.get $new)))
											(local.set $d (i32.add (local.get $d) (i32.const 8)))
324 325 326 327
											(br $update-array-pointers)
										)
									)

328
									(local.set $size (i32.sub (local.get $size) (i32.const 1)))
329
									(local.set $n (i32.add (local.get $n) (local.get $arity)))
330 331 332 333
									(br $update-array)
								)
							)
						)
334
						;; not a basic type
335 336
						(local.set $arity (i32.load16_s (i32.sub (local.get $d) (i32.const 2))))
						(local.set $a-arity (local.get $arity))
337
						(if
338
							(i32.gt_s (local.get $arity) (i32.const 256))
339 340
							(then
								(local.set $a-arity (i32.load16_s (local.get $d)))
341
								(local.set $arity (i32.sub (local.get $arity) (i32.const 256)))
342 343 344 345
							)
						)

						(if
346
							(i32.gt_s (local.get $arity) (i32.const 2))
347
							(then
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
								;; large node
								(if
									(i32.gt_s (local.get $a-arity) (i32.const 0))
									(then
										(local.set $new (call $update-ref (i32.add (local.get $n) (i32.const 8)) (local.get $new)))

										(local.set $n (i32.add (local.get $n) (i32.const 16)))
										(local.set $a-arity (i32.shl (local.get $a-arity) (i32.const 3)))
										(block $end-update-large-hnf
											(loop $update-large-hnf
												(local.set $a-arity (i32.sub (local.get $a-arity) (i32.const 8)))
												(br_if $end-update-large-hnf (i32.eqz (local.get $a-arity)))
												(local.set $new (call $update-ref (i32.add (local.get $n) (local.get $a-arity)) (local.get $new)))
												(br $update-large-hnf)
											)
										)

365
										(local.set $n (i32.add (local.get $n) (i32.shl (local.get $arity) (i32.const 3))))
366 367 368
										(br $update-refs)
									)
									(else
369
										(local.set $n (i32.add (local.get $n) (i32.shl (i32.add (i32.const 2) (local.get $arity)) (i32.const 3))))
370 371 372 373 374 375
										(br $update-refs)
									)
								)
							)
							(else
								;; small node
376 377 378 379 380 381
								(block $skip-b-elems
									(br_if $skip-b-elems (i32.eqz (local.get $a-arity)))
									(local.set $new (call $update-ref (i32.add (local.get $n) (i32.const 8)) (local.get $new)))
									(br_if $skip-b-elems (i32.eq (local.get $a-arity) (i32.const 1)))
									(local.set $new (call $update-ref (i32.add (local.get $n) (i32.const 16)) (local.get $new)))
								)
382
								(local.set $n (i32.add (local.get $n) (i32.add (i32.shl (local.get $arity) (i32.const 3)) (i32.const 8))))
383 384 385 386 387 388 389
								(br $update-refs)
							)
						)
					)
				)

				;; thunk
390
				(local.set $arity (i32.load (i32.sub (local.get $d) (i32.const 4))))
391
				(if
392
					(i32.lt_s (local.get $arity) (i32.const 0))
393 394
					(then
						(local.set $a-arity (i32.const 1))
395
						(local.set $arity (i32.const 1))
396 397
					)
					(else
398 399 400
						(local.set $a-arity (i32.sub (i32.and (local.get $arity) (i32.const 0xff))
							(i32.and (i32.shr_u (local.get $arity) (i32.const 8)) (i32.const 0xff))))
						(local.set $arity (i32.and (local.get $arity) (i32.const 0xff)))
401 402 403
					)
				)

404
				(local.set $d (i32.const 1)) ;; iterator
405 406
				(block $end-update-thunk-refs
					(loop $update-thunk-refs
407 408 409
						(br_if $end-update-thunk-refs (i32.gt_s (local.get $d) (local.get $a-arity)))
						(local.set $new (call $update-ref (i32.add (local.get $n) (i32.shl (local.get $d) (i32.const 3))) (local.get $new)))
						(local.set $d (i32.add (local.get $d) (i32.const 1)))
410 411 412
						(br $update-thunk-refs)
					)
				)
413 414 415 416 417
				(local.set $n
					(i32.add
						(local.get $n)
						(i32.shl
							(select
418
								(i32.add (local.get $arity) (i32.const 1))
419
								(i32.const 3)
420
								(i32.gt_s (local.get $arity) (i32.const 2))
421 422 423
							)
							(i32.const 3)
						)
424 425 426 427 428 429 430
					)
				)
				(br $update-refs)
			)
		)

		;; return
431
		(global.set $in-first-semispace (i32.sub (i32.const 1) (global.get $in-first-semispace)))
432 433 434 435 436 437
		(call $set-hp (local.get $new))
		(call $set-hp-free
			(i32.shr_u
				(i32.sub
					(select (global.get $half-heap) (global.get $end-heap) (global.get $in-first-semispace))
					(local.get $new)
438
				)
439 440
				(i32.const 3)
			))
441 442

		(call $gc-end)
443 444
	)

445 446
	(func $update-ref (param $ref i32) (param $hp i32) (result i32)
		(local $n i32)
447
		(local $d i32)
448
		(local $arity i32)
449
		(local $size i32)
450

451 452 453 454 455 456 457 458 459 460 461 462 463 464
		(local.set $n (i32.load (local.get $ref)))

		(if
			(i32.or
				(i32.gt_s (global.get $start-heap) (local.get $n))
				(i32.lt_s (global.get $end-heap) (local.get $n))
			)
			(then (return (local.get $hp)))
		)

		(local.set $d (i32.load (local.get $n)))
		(if
			(i32.and (local.get $d) (i32.const 1))
			(then
465
				(i64.store (local.get $ref) (i64.extend_i32_u (i32.sub (local.get $d) (i32.const 1))))
466 467 468 469
				(return (local.get $hp))
			)
		)

470
		(i64.store (local.get $ref) (i64.extend_i32_u (local.get $hp)))
471

472
		(if (i32.and (local.get $d) (i32.const 2))
473
			;; hnf
474 475
			(then
				(if
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
					(i32.eq (local.get $d) (i32.const 210)) ;; INT+2
					(then
						(if
							(i64.lt_u (i64.load offset=8 (local.get $n)) (i64.const 33))
							(then
								(local.set $d (i32.add (i32.const 248) (i32.shl (i32.load offset=8 (local.get $n)) (i32.const 4)))) ;; small integer
								(i64.store (local.get $ref) (i64.extend_i32_u (local.get $d)))
								(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $d) (i32.const 1))))
								(return (local.get $hp))
							)
							(else
								(i64.store (local.get $hp) (i64.extend_i32_u (local.get $d)))
								(i64.store offset=8 (local.get $hp) (i64.load offset=8 (local.get $n)))
								(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 1))))
								(return (i32.add (local.get $hp) (i32.const 16)))
491 492 493
							)
						)
					)
494 495 496
				)
				(if
					(i32.eq (local.get $d) (i32.const 90)) ;; BOOL+2
497
					(then
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
						(local.set $d (select (i32.const 5344) (i32.const 5328) (i32.load offset=8 (local.get $n)))) ;; static FALSE/TRUE
						(i64.store (local.get $ref) (i64.extend_i32_u (local.get $d)))
						(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $d) (i32.const 1))))
						(return (local.get $hp))
					)
				)
				(if
					(i32.eq (local.get $d) (i32.const 130)) ;; CHAR+2
					(then
						(local.set $d (i32.add (i32.const 1176) (i32.shl
							(i32.and (i32.load8_u offset=8 (local.get $n)) (i32.const 0xff)) (i32.const 4)))) ;; static character
						(i64.store (local.get $ref) (i64.extend_i32_u (local.get $d)))
						(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $d) (i32.const 1))))
						(return (local.get $hp))
					)
				)
				(if
					(i32.eq (local.get $d) (i32.const 170)) ;; REAL+2
					(then
						(i64.store (local.get $hp) (i64.extend_i32_u (local.get $d)))
518
						(i64.store offset=8 (local.get $hp) (i64.load offset=8 (local.get $n)))
519
						(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 1))))
520
						(return (i32.add (local.get $hp) (i32.const 16)))
521 522
					)
				)
523 524 525 526
				(if
					;; _STRING_+2
					(i32.eq (local.get $d) (i32.const 50))
					(then
527 528 529
						(i64.store (local.get $hp) (i64.extend_i32_u (local.get $d)))
						(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 1))))

530
						(local.set $size (i32.load offset=8 (local.get $n))) ;; size
531
						(i64.store offset=8 (local.get $hp) (i64.extend_i32_u (local.get $size)))
532 533 534
						(local.set $hp (i32.add (local.get $hp) (i32.const 16)))
						(local.set $n (i32.add (local.get $n) (i32.const 16)))
						(local.set $size (i32.shr_u (i32.add (local.get $size) (i32.const 7)) (i32.const 3)))
535 536
						(block $end-copy-string
							(loop $copy-string
537 538 539 540 541
								(br_if $end-copy-string (i32.eqz (local.get $size)))
								(i64.store (local.get $hp) (i64.load (local.get $n)))
								(local.set $size (i32.sub (local.get $size) (i32.const 1)))
								(local.set $hp (i32.add (local.get $hp) (i32.const 8)))
								(local.set $n (i32.add (local.get $n) (i32.const 8)))
542 543 544
								(br $copy-string)
							)
						)
545
						(return (local.get $hp))
546 547
					)
				)
548 549 550 551
				(if
					;; _ARRAY_+2
					(i32.eq (local.get $d) (i32.const 10))
					(then
552 553 554
						(i64.store (local.get $hp) (i64.extend_i32_u (local.get $d)))
						(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 1))))

555 556
						(local.set $size (i32.load offset=8 (local.get $n))) ;; size
						(local.set $d (i32.load offset=16 (local.get $n))) ;; element descriptor
557

558 559
						(i64.store offset=8 (local.get $hp) (i64.extend_i32_u (local.get $size)))
						(i64.store offset=16 (local.get $hp) (i64.extend_i32_u (local.get $d)))
560 561
						(local.set $hp (i32.add (local.get $hp) (i32.const 24)))
						(local.set $n (i32.add (local.get $n) (i32.const 24)))
562

563
						(if
564
							(i32.eqz ;; not
565 566
								(i32.or
									(i32.eqz (local.get $d)) ;; boxed
567 568 569
									(i32.or ;; unboxed INT/REAL
										(i32.eq (i32.const 170) (local.get $d))
										(i32.eq (local.get $d) (i32.const 210))
570 571 572 573
									)
								)
							)
							(then
574 575 576
								(if
									(i32.eq (local.get $d) (i32.const 90))
									(then ;; BOOL
577
										(local.set $size (i32.shr_u (i32.add (local.get $size) (i32.const 7)) (i32.const 3)))
578 579
									)
									(else ;; unboxed record
580
										(local.set $size (i32.mul (local.get $size)
581 582 583
											(i32.sub (i32.load16_s (i32.sub (local.get $d) (i32.const 2))) (i32.const 256))))
									)
								)
584 585
							)
						)
586

587 588
						(block $end-copy-array
							(loop $copy-array
589 590 591 592 593
								(br_if $end-copy-array (i32.eqz (local.get $size)))
								(i64.store (local.get $hp) (i64.load (local.get $n)))
								(local.set $size (i32.sub (local.get $size) (i32.const 1)))
								(local.set $hp (i32.add (local.get $hp) (i32.const 8)))
								(local.set $n (i32.add (local.get $n) (i32.const 8)))
594 595 596
								(br $copy-array)
							)
						)
597
						(return (local.get $hp))
598 599
					)
				)
600
				;; not a basic type
601 602 603 604 605 606 607 608 609 610 611 612 613

				(if
					(i32.eq (local.get $d) (global.get $js-ref-constructor))
					(then
						(i64.store (local.get $hp) (i64.extend_i32_u (local.get $d)))
						(local.set $d (i32.load offset=8 (local.get $n)))
						(call $js-ref-found (local.get $d))
						(i64.store offset=8 (local.get $hp) (i64.extend_i32_u (local.get $d)))
						(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 1))))
						(return (i32.add (local.get $hp) (i32.const 16)))
					)
				)

614
				(local.set $arity (i32.load16_s (i32.sub (local.get $d) (i32.const 2))))
615
				(if
616
					(i32.gt_s (local.get $arity) (i32.const 256))
617
					(then
618
						(local.set $arity (i32.sub (local.get $arity) (i32.const 256)))
619 620
					)
				)
621

622
				(if
623
					(i32.eqz (local.get $arity))
624 625 626 627 628 629 630 631
					(then
						(local.set $d (i32.sub (local.get $d) (i32.const 10)))
						(i64.store (local.get $ref) (i64.extend_i32_u (local.get $d)))
						(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $d) (i32.const 1))))
						(return (local.get $hp))
					)
				)

632 633 634
				(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 1))))
				(i64.store (local.get $hp) (i64.extend_i32_u (local.get $d)))

635
				(i64.store offset=8 (local.get $hp) (i64.load offset=8 (local.get $n)))
636
				(if
637
					(i32.gt_s (local.get $arity) (i32.const 2))
638
					(then
639
						;; hnf spread over two blocks
640
						(i64.store offset=16 (local.get $hp) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 24))))
641 642
						(local.set $hp (i32.add (local.get $hp) (i32.const 24)))
						(local.set $n (i32.load offset=16 (local.get $n)))
643 644 645

						(block $end-copy-large-hnf
							(loop $copy-large-hnf
646 647
								(local.set $arity (i32.sub (local.get $arity) (i32.const 1)))
								(br_if $end-copy-large-hnf (i32.eqz (local.get $arity)))
648 649 650
								(i64.store (local.get $hp) (i64.load (local.get $n)))
								(local.set $hp (i32.add (local.get $hp) (i32.const 8)))
								(local.set $n (i32.add (local.get $n) (i32.const 8)))
651 652 653
								(br $copy-large-hnf)
							)
						)
654 655 656 657 658
						(return (local.get $hp))
					)
					(else
						;; small hnf
						(i64.store offset=16 (local.get $hp) (i64.load offset=16 (local.get $n)))
659
						(return (i32.add (local.get $hp) (i32.add (i32.shl (local.get $arity) (i32.const 3)) (i32.const 8))))
660 661 662 663 664
					)
				)
			)
		)

665
		;; thunk
666 667 668
		(i64.store (local.get $n) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 1))))
		(i64.store (local.get $hp) (i64.extend_i32_u (local.get $d)))

669
		(local.set $arity (i32.load (i32.sub (local.get $d) (i32.const 4))))
670
		(if
671
			(i32.lt_s (local.get $arity) (i32.const 0))
672
			(then
673
				(local.set $arity (i32.const 1)) ;; negative for selectors etc.
674 675
			)
			(else
676
				(local.set $arity (i32.and (local.get $arity) (i32.const 0xff)))
677 678
			)
		)
679

680
		(local.set $size (local.get $arity)) ;; $size is just an iterator
681 682
		(block $end-copy-thunk
			(loop $copy-thunk
683 684 685 686 687
				(br_if $end-copy-thunk (i32.eqz (local.get $size)))
				(local.set $size (i32.sub (local.get $size) (i32.const 1)))
				(i64.store
					(local.tee $hp (i32.add (local.get $hp) (i32.const 8)))
					(i64.load (local.tee $n (i32.add (local.get $n) (i32.const 8)))))
688 689 690 691
				(br $copy-thunk)
			)
		)

692
		(select
693
			(i32.add (local.get $hp) (i32.shl (i32.sub (i32.const 3) (local.get $arity)) (i32.const 3)))
694
			(i32.add (local.get $hp) (i32.const 8))
695
			(i32.lt_s (local.get $arity) (i32.const 2))
696 697
		)
	)
Camil Staps's avatar
Camil Staps committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

	(func $copy (param $to i32) (param $from i32) (param $n-words i32)
		(block $end
			(loop $loop
				(local.set $n-words (i32.sub (local.get $n-words) (i32.const 1)))
				(br_if $end (i32.lt_s (local.get $n-words) (i32.const 0)))
				(i64.store
					(i32.add (local.get $to) (i32.shl (local.get $n-words) (i32.const 3)))
					(i64.load (i32.add (local.get $from) (i32.shl (local.get $n-words) (i32.const 3)))))
				(br $loop)
			)
		)
	)

	(func (export "copy_from_string")
		(param $s i32) (param $len i32)
		(param $asp i32) (param $bsp i32) (param $hp i32)
		(param $code-offset i32)
		(result i32)

		(local $ptr-stack i32)
		(local $a-size-stack i32)
		(local $i i32)
		(local $desc i32)
		(local $a-arity i32)
		(local $b-arity i32)
		(local $arity i32)
		(local $a i32)
		(local $j i32)
		(local $k i32)

		(local.set $ptr-stack (local.get $asp))
		(local.set $a-size-stack (local.get $bsp))

		(i32.store (local.tee $ptr-stack (i32.add (local.get $ptr-stack) (i32.const 4))) (local.get $s)) ;; dummy
		(i32.store (local.tee $a-size-stack (i32.sub (local.get $a-size-stack) (i32.const 2))) (i32.const 1))

		(local.set $i (i32.const -8))
		(local.set $len (i32.shl (local.get $len) (i32.const 3)))
		(block $end
			(loop $loop
				(local.set $i (i32.add (local.get $i) (i32.const 8)))
				(br_if $end (i32.ge_u (local.get $i) (local.get $len)))

				(if
					(i32.eqz (i32.load16_s (local.get $a-size-stack)))
					(then
						(local.set $i (i32.sub (local.get $i) (i32.const 8)))
						(local.set $a-size-stack (i32.add (local.get $a-size-stack) (i32.const 2)))
						(br $loop)
					)
				)

				(i32.store16 (local.get $a-size-stack) (i32.sub (i32.load16_s (local.get $a-size-stack)) (i32.const 1)))

				(local.set $desc (i32.load (i32.add (local.get $s) (local.get $i))))
				(i64.store (i32.add (local.get $s) (local.get $i)) (i64.extend_i32_u (local.get $hp)))

756 757 758
				;; "loop"
				;;(call $debug (local.get $desc) (local.get $i) (local.get $len) (local.get $hp))

Camil Staps's avatar
Camil Staps committed
759 760 761 762 763 764 765
				(if ;; redirection or predefined constructor
					(i32.lt_s (local.get $desc) (i32.const 0))
					(then
						(block $no-predefined-constructor
							(block $predefined-constructor
								;; predefined constructors: see ABC.Interpreter for the $desc values;
								;; the constructors written to the heap are from the interpreter generator.
766
								;; TODO: use small_integers, static_characters, and static_booleans here
Camil Staps's avatar
Camil Staps committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
								(if ;; BOOL
									(i32.eq (local.get $desc) (i32.const -22))
									(then
										(i64.store (local.get $hp) (i64.const 90))
										(i64.store offset=8 (local.get $hp) (i64.load offset=8 (i32.add (local.get $s) (local.get $i))))
										(br $predefined-constructor)
									)
								)
								(if ;; CHAR
									(i32.eq (local.get $desc) (i32.const -30))
									(then
										(i64.store (local.get $hp) (i64.const 130))
										(i64.store offset=8 (local.get $hp) (i64.load offset=8 (i32.add (local.get $s) (local.get $i))))
										(br $predefined-constructor)
									)
								)
								(if ;; REAL
									(i32.eq (local.get $desc) (i32.const -38))
									(then
										(i64.store (local.get $hp) (i64.const 170))
										(i64.store offset=8 (local.get $hp) (i64.load offset=8 (i32.add (local.get $s) (local.get $i))))
										(br $predefined-constructor)
									)
								)
								(if ;; INT
									(i32.eq (local.get $desc) (i32.const -46))
									(then
										(i64.store (local.get $hp) (i64.const 210))
										(i64.store offset=8 (local.get $hp) (i64.load offset=8 (i32.add (local.get $s) (local.get $i))))
										(br $predefined-constructor)
									)
								)

								(br $no-predefined-constructor)
							)

							(i64.store (i32.load (local.get $ptr-stack)) (i64.extend_i32_u (local.get $hp)))
							(local.set $ptr-stack (i32.sub (local.get $ptr-stack) (i32.const 4)))
							(local.set $hp (i32.add (local.get $hp) (i32.const 16)))
							(local.set $i (i32.add (local.get $i) (i32.const 8)))
							(br $loop)
						)

						(if ;; _STRING_
							(i32.eq (local.get $desc) (i32.const -14))
							(then
								(i64.store (i32.load (local.get $ptr-stack)) (i64.extend_i32_u (local.get $hp)))
								(local.set $ptr-stack (i32.sub (local.get $ptr-stack) (i32.const 4)))

								(i64.store (local.get $hp) (i64.const 50))
								(local.set $a (i32.load offset=8 (i32.add (local.get $s) (local.get $i))))
								(i64.store offset=8 (local.get $hp) (i64.extend_i32_u (local.get $a)))
								(local.set $a (i32.shr_u (i32.add (local.get $a) (i32.const 7)) (i32.const 3)))
								(call $copy
									(i32.add (local.get $hp) (i32.const 16))
									(i32.add (local.get $s) (i32.add (local.get $i) (i32.const 16)))
									(local.get $a))

Camil Staps's avatar
Camil Staps committed
825 826
								(local.set $hp (i32.add (local.get $hp) (i32.add (i32.const 16) (i32.shl (local.get $a) (i32.const 3)))))
								(local.set $i (i32.add (local.get $i) (i32.add (i32.const 8) (i32.shl (local.get $a) (i32.const 3)))))
Camil Staps's avatar
Camil Staps committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
								(br $loop)
							)
						)

						(if ;; _ARRAY_
							(i32.eq (local.get $desc) (i32.const -6))
							(then
								(i64.store (i32.load (local.get $ptr-stack)) (i64.extend_i32_u (local.get $hp)))
								(local.set $ptr-stack (i32.sub (local.get $ptr-stack) (i32.const 4)))

								(local.set $a (i32.load offset=8 (i32.add (local.get $s) (local.get $i))))
								(local.set $desc (i32.load offset=16 (i32.add (local.get $s) (local.get $i))))

								(i64.store (local.get $hp) (i64.const 10))
								(i64.store offset=8 (local.get $hp) (i64.extend_i32_u (local.get $a)))

								(if ;; INT elements
									(i32.eq (local.get $desc) (i32.const -46))
									(then
										(i64.store offset=16 (local.get $hp) (i64.const 210))
										(call $copy
											(i32.add (local.get $hp) (i32.const 24))
											(i32.add (local.get $s) (i32.add (local.get $i) (i32.const 24)))
											(local.get $a))
										(local.set $a (i32.shl (local.get $a) (i32.const 3)))
										(local.set $hp (i32.add (local.get $hp) (i32.add (i32.const 24) (local.get $a))))
										(local.set $i (i32.add (local.get $i) (i32.add (i32.const 16) (local.get $a))))
										(br $loop)
									)
								)
								(if ;; REAL elements
									(i32.eq (local.get $desc) (i32.const -38))
									(then
										(i64.store offset=16 (local.get $hp) (i64.const 170))
										(call $copy
											(i32.add (local.get $hp) (i32.const 24))
											(i32.add (local.get $s) (i32.add (local.get $i) (i32.const 24)))
											(local.get $a))
										(local.set $a (i32.shl (local.get $a) (i32.const 3)))
										(local.set $hp (i32.add (local.get $hp) (i32.add (i32.const 24) (local.get $a))))
										(local.set $i (i32.add (local.get $i) (i32.add (i32.const 16) (local.get $a))))
										(br $loop)
									)
								)
								(if ;; BOOL elements
									(i32.eq (local.get $desc) (i32.const -22))
									(then
										(i64.store offset=16 (local.get $hp) (i64.const 90))
										(local.set $a (i32.shr_u (i32.add (local.get $a) (i32.const 7)) (i32.const 3)))
										(call $copy
											(i32.add (local.get $hp) (i32.const 24))
											(i32.add (local.get $s) (i32.add (local.get $i) (i32.const 24)))
											(local.get $a))
										(local.set $a (i32.shl (local.get $a) (i32.const 3)))
										(local.set $hp (i32.add (local.get $hp) (i32.add (i32.const 24) (local.get $a))))
										(local.set $i (i32.add (local.get $i) (i32.add (i32.const 16) (local.get $a))))
										(br $loop)
									)
								)

								(if
									(i32.eqz (local.get $desc))
									(then
										(local.set $arity (i32.const 1))
										(local.set $a-arity (i32.const 1))
									)
									(else
										(local.set $desc (i32.add (local.get $desc) (local.get $code-offset)))
										(local.set $arity (i32.sub (i32.load16_u (i32.sub (local.get $desc) (i32.const 2))) (i32.const 256)))
										(local.set $a-arity (i32.load16_u (local.get $desc)))
									)
								)
								(local.set $b-arity (i32.sub (local.get $arity) (local.get $a-arity)))

								(i64.store offset=16 (local.get $hp) (i64.extend_i32_u (local.get $desc)))
								(local.set $hp (i32.add (local.get $hp) (i32.const 24)))
								(local.set $i (i32.add (local.get $i) (i32.const 16)))

								(local.set $ptr-stack (i32.add (local.get $ptr-stack)
									(i32.shl (i32.mul (local.get $a) (local.get $a-arity)) (i32.const 2))))
								(i32.store16 (local.tee $a-size-stack (i32.sub (local.get $a-size-stack) (i32.const 2)))
									(i32.mul (local.get $a) (local.get $a-arity)))

								(local.set $arity (i32.shl (local.get $arity) (i32.const 3)))
								(local.set $j (i32.const 0))
								(block $end-copy-array-elements
									(loop $copy-array-elements
										(br_if $end-copy-array-elements (i32.eq (local.get $j) (local.get $a)))

										(local.set $k (i32.const 0))
										(block $end-push-pointers
											(loop $push-pointers
												(br_if $end-push-pointers (i32.eq (local.get $k) (local.get $a-arity)))
												(i32.store
													(i32.sub (local.get $ptr-stack)
														(i32.shl (i32.add (i32.mul (local.get $j) (local.get $a-arity)) (local.get $k)) (i32.const 2)))
													(i32.add (local.get $hp) (i32.shl (local.get $k) (i32.const 3))))
												(local.set $k (i32.add (local.get $k) (i32.const 1)))
												(br $push-pointers)
											)
										)

										(call $copy
											(i32.add (local.get $hp) (i32.shl (local.get $a-arity) (i32.const 3)))
											(i32.add (local.get $s) (i32.add (local.get $i) (i32.const 8)))
											(local.get $b-arity))
										(local.set $i (i32.add (local.get $i) (i32.shl (local.get $b-arity) (i32.const 3))))

										(local.set $hp (i32.add (local.get $hp) (local.get $arity)))
										(local.set $j (i32.add (local.get $j) (i32.const 1)))
										(br $copy-array-elements)
									)
								)

								(br $loop)
							)
						)

						;; not a predefined constructor; redirection
						(i64.store
							(i32.load (local.get $ptr-stack))
							(i64.load (i32.add (local.get $s) (i32.add (local.get $i) (i32.sub (local.get $desc) (i32.const 1))))))
						(local.set $ptr-stack (i32.sub (local.get $ptr-stack) (i32.const 4)))
						(br $loop)
					)
				)

				(local.set $desc (i32.add (local.get $desc) (local.get $code-offset)))

				;; "desc"
				;;(call $debug (i32.const 1) (local.get $desc) (i32.const 0) (i32.const 0))

				(if
					(i32.and (local.get $desc) (i32.const 2))
					(then
						;; hnf
						(local.set $arity (i32.load16_s (i32.sub (local.get $desc) (i32.const 2))))
						(local.set $a-arity (local.get $arity))

						;; "arity"
						;;(call $debug (i32.const 2) (local.get $arity) (i32.const 0) (i32.const 0))

						(if
							(i32.gt_u (local.get $arity) (i32.const 256))
							(then
								(local.set $a-arity (i32.load16_s (local.get $desc)))
								(local.set $arity (i32.sub (local.get $arity) (i32.const 256)))
							)
						)

						(if
Camil Staps's avatar
Camil Staps committed
978
							(i32.eqz (local.get $arity))
Camil Staps's avatar
Camil Staps committed
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
							(then
								(local.set $desc (i32.sub (local.get $desc) (i32.const 10)))
								(i64.store (i32.load (local.get $ptr-stack)) (i64.extend_i32_u (local.get $desc)))
								(local.set $ptr-stack (i32.sub (local.get $ptr-stack) (i32.const 4)))
								(i64.store (i32.add (local.get $s) (local.get $i)) (i64.extend_i32_u (local.get $desc)))
								(i32.store16 (local.tee $a-size-stack (i32.sub (local.get $a-size-stack) (i32.const 2))) (i32.const 0))
								(br $loop)
							)
						)

						(i64.store (i32.load (local.get $ptr-stack)) (i64.extend_i32_u (local.get $hp)))
						(local.set $ptr-stack (i32.sub (local.get $ptr-stack) (i32.const 4)))

						(i64.store (local.get $hp) (i64.extend_i32_u (local.get $desc)))

						(if
							(i32.eq (local.get $arity) (i32.const 1))
							(then
								(if
									(i32.eq (local.get $a-arity) (i32.const 1))
									(then
										(i32.store
											(local.tee $ptr-stack (i32.add (local.get $ptr-stack) (i32.const 4)))
											(i32.add (local.get $hp) (i32.const 8)))
									)
									(else
										(i64.store offset=8 (local.get $hp)
											(i64.load (i32.add (local.get $s)
												(local.tee $i (i32.add (local.get $i) (i32.const 8))))))
									)
								)
								(local.set $hp (i32.add (local.get $hp) (i32.const 16)))
1011
								(i32.store16 (local.tee $a-size-stack (i32.sub (local.get $a-size-stack) (i32.const 2))) (local.get $a-arity))
Camil Staps's avatar
Camil Staps committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
								(br $loop)
							)
						)
						(if
							(i32.eq (local.get $arity) (i32.const 2))
							(then
								(if
									(i32.eq (local.get $a-arity) (i32.const 2))
									(then
										(i32.store offset=8 (local.get $ptr-stack) (i32.add (local.get $hp) (i32.const 8)))
										(i32.store offset=4 (local.get $ptr-stack) (i32.add (local.get $hp) (i32.const 16)))
										(local.set $ptr-stack (i32.add (local.get $ptr-stack) (i32.const 8)))
									)
									(else
										(if
											(i32.eq (local.get $a-arity) (i32.const 1))
											(then
												(i32.store offset=4 (local.get $ptr-stack) (i32.add (local.get $hp) (i32.const 8)))
												(local.set $ptr-stack (i32.add (local.get $ptr-stack) (i32.const 4)))
												(i64.store offset=16 (local.get $hp)
													(i64.load (i32.add (local.get $s)
														(local.tee $i (i32.add (local.get $i) (i32.const 8))))))
											)
											(else ;; b-arity=2
												(i64.store offset=8 (local.get $hp)
													(i64.load (i32.add (local.get $s) (i32.add (local.get $i) (i32.const 8)))))
												(i64.store offset=16 (local.get $hp)
													(i64.load (i32.add (local.get $s) (i32.add (local.get $i) (i32.const 16)))))
												(local.set $i (i32.add (local.get $i) (i32.const 16)))
											)
										)
									)
								)
								(local.set $hp (i32.add (local.get $hp) (i32.const 24)))
1046
								(i32.store16 (local.tee $a-size-stack (i32.sub (local.get $a-size-stack) (i32.const 2))) (local.get $a-arity))
Camil Staps's avatar
Camil Staps committed
1047 1048 1049 1050 1051 1052 1053 1054
								(br $loop)
							)
						)

						;; large hnf
						(i64.store offset=16 (local.get $hp) (i64.extend_i32_u (i32.add (local.get $hp) (i32.const 24))))

						(if
Camil Staps's avatar
Camil Staps committed
1055
							(i32.eqz (local.get $a-arity))
Camil Staps's avatar
Camil Staps committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
							(then
								(i64.store offset=8 (local.get $hp)
									(i64.load (i32.add (local.get $s)
										(local.tee $i (i32.add (local.get $i) (i32.const 8))))))
								(local.set $arity (i32.sub (local.get $arity) (i32.const 1)))
								(call $copy
									(i32.add (local.get $hp) (i32.const 24))
									(i32.add (local.get $s) (local.get $i))
									(local.get $arity))
								(local.set $i (i32.add (local.get $i) (i32.shl (local.get $arity) (i32.const 3))))
							)
							(else
								(i32.store
									(i32.add (local.get $ptr-stack) (i32.shl (local.get $a-arity) (i32.const 2)))
									(i32.add (local.get $hp) (i32.const 8)))
								(local.set $a (i32.const 1))
								(block $end-push-pointers
									(loop $push-pointers
										(br_if $end-push-pointers (i32.eq (local.get $a) (local.get $a-arity)))
										(i32.store
											(i32.add (local.get $ptr-stack) (i32.shl (local.get $a) (i32.const 2)))
											(i32.add (local.get $hp) (i32.add (i32.const 16) (i32.shl (i32.sub (local.get $a-arity) (local.get $a)) (i32.const 3)))))
										(local.set $a (i32.add (local.get $a) (i32.const 1)))
										(br $push-pointers)
									)
								)
								(local.set $ptr-stack (i32.add (local.get $ptr-stack) (i32.shl (local.get $a-arity) (i32.const 2))))
								(call $copy
									(i32.add (local.get $hp) (i32.add (i32.const 16) (i32.shl (local.get $a-arity) (i32.const 3))))
									(i32.add (local.get $s) (i32.add (i32.const 8) (local.get $i)))
									(i32.sub (local.get $arity) (local.get $a-arity)))
								(local.set $i (i32.add (local.get $i) (i32.shl (i32.sub (local.get $arity) (local.get $a-arity)) (i32.const 3))))
							)
						)

						(local.set $hp (i32.add (local.get $hp) (i32.add (i32.shl (local.get $arity) (i32.const 3)) (i32.const 16))))
						(i32.store16 (local.tee $a-size-stack (i32.sub (local.get $a-size-stack) (i32.const 2))) (local.get $a-arity))
						(br $loop)
					)
					(else
1096
						;; thunk
Camil Staps's avatar
Camil Staps committed
1097 1098 1099 1100 1101 1102 1103
						(local.set $arity (i32.load (i32.sub (local.get $desc) (i32.const 4))))
						(local.set $a-arity (local.get $arity))

						(if
							(i32.lt_s (local.get $arity) (i32.const 0))
							(then
								(local.set $arity (i32.const 1))
1104
								(local.set $a-arity (i32.const 1))
Camil Staps's avatar
Camil Staps committed
1105 1106 1107 1108 1109
							)
							(else
								(if
									(i32.gt_u (local.get $arity) (i32.const 256))
									(then
1110
										(local.set $b-arity (i32.shr_u (local.get $arity) (i32.const 8)))
Camil Staps's avatar
Camil Staps committed
1111
										(local.set $arity (i32.and (local.get $arity) (i32.const 0xff)))
1112
										(local.set $a-arity (i32.sub (local.get $arity) (local.get $b-arity)))
Camil Staps's avatar
Camil Staps committed
1113 1114 1115 1116 1117
									)
								)
							)
						)

1118 1119 1120
						;; "arity"
						;;(call $debug (i32.const 3) (local.get $arity) (local.get $a-arity) (local.get $b-arity))

Camil Staps's avatar
Camil Staps committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
						(i64.store (i32.load (local.get $ptr-stack)) (i64.extend_i32_u (local.get $hp)))
						(local.set $ptr-stack (i32.sub (local.get $ptr-stack) (i32.const 4)))

						(i64.store (local.get $hp) (i64.extend_i32_u (local.get $desc)))
						(local.set $hp (i32.add (local.get $hp) (i32.const 8)))

						(i32.store16 (local.tee $a-size-stack (i32.sub (local.get $a-size-stack) (i32.const 2))) (local.get $a-arity))

						(local.set $a-arity (i32.shl (local.get $a-arity) (i32.const 2)))
						(local.set $a (i32.const 0))
						(block $end-push-pointers
							(loop $push-pointers
								(br_if $end-push-pointers (i32.eq (local.get $a) (local.get $a-arity)))
								(i32.store
									(i32.add (local.get $ptr-stack) (i32.sub (local.get $a-arity) (local.get $a)))
									(i32.add (local.get $hp) (i32.shl (local.get $a) (i32.const 1))))
								(local.set $a (i32.add (local.get $a) (i32.const 4)))
								(br $push-pointers)
							)
						)
						(local.set $ptr-stack (i32.add (local.get $ptr-stack) (local.get $a-arity)))

						(local.set $a (i32.shl (local.get $a) (i32.const 1)))
						(local.set $arity (i32.shl (local.get $arity) (i32.const 3)))
						(block $end-add-thunk-values
							(loop $add-thunk-values
								(br_if $end-add-thunk-values (i32.eq (local.get $a) (local.get $arity)))
								(i64.store
									(i32.add (local.get $hp) (local.get $a))
									(i64.load (i32.add (local.get $s)
										(local.tee $i (i32.add (local.get $i) (i32.const 8))))))
								(local.set $a (i32.add (local.get $a) (i32.const 8)))
								(br $add-thunk-values)
							)
						)

						(local.set $hp (i32.add (local.get $hp)
							(select
								(i32.const 16)
								(local.get $arity)
								(i32.lt_s (local.get $arity) (i32.const 24)))))
						(br $loop)
					)
				)

				(unreachable)
			)
		)

		(local.get $hp)
	)
1172
)