summaryrefslogtreecommitdiff
path: root/parse.asm
blob: 1b237459439b8c9a24b04b029673dc312c58ae1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
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
756
757
758
759
760
761
762
763
764
765
766
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
825
826
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
# Copyright (C) 2026 Sebastian G. Kirmayer <gloria@gloria-mundi.eu>
#
#  This file is part of sysf-i386.
#
#  sysf-i386 is free software: you can redistribute it and/or modify it under
#  the terms of the GNU General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option)
#  any later version.
#
#  sysf-i386 is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#  details.
#
#  You should have received a copy of the GNU General Public License along
#  with sysf-i386. If not, see <https://www.gnu.org/licenses/>.
#

bss
align
label lineno
	long 0
label error_flag
	long 0
label macro_flag
	long 0

label parse_stack_top
	skip $((PARSE_STACK_SIZE*4))
label parse_stack_bottom
label macro_types_start
	skip $((MACROS_SIZE*4))
label macro_types_end
label macro_start
	skip $((PARSE_STACK_SIZE*16))
label macro_end
label input
	long 0
label input_end
	long 0
data
align
label parse_stack_ptr
	long parse_stack_bottom
label macro_type_ptr
	long macro_types_start
label macro_ptr
	long macro_start
text

# STK
# { IDENT START, IDENT END } (if STK & STK_HAVE_IDENT)
# LINENO (if STK & STK_HAVE_LINENO)

# On the type stack:
# callee type (if STK & STK_HAVE_VALUE)
# argument type (if STK & STK_SMALL_LAMBDA)

# stack control words:
STK_BOTTOM=1
STK_PAREN=2
STK_SMALL_LAMBDA=4
STK_BIG_LAMBDA=8

STK_TYPE_BOTTOM=256
STK_TYPE_ARROW=512
STK_TYPE_PAREN=1024
STK_TYPE_FORALL=2048

STK_HAVE_VALUE=16
STK_HAVE_IDENT=$((STK_SMALL_LAMBDA|STK_BIG_LAMBDA|STK_TYPE_FORALL))
STK_HAVE_LINENO=$((STK_PAREN|STK_TYPE_PAREN))

BACKSLASH=0x5C
SLASH=0x2F
LPAREN=0x28
RPAREN=0x29
LBRACKET=0x5B
RBRACKET=0x5D
COLON=0x3A
SEMICOLON=0x3B
OCTOTHORPE=0x23
SPACE=0x20
TAB=0x09
NEWLINE=0x0A
HYPHEN=0x2D
GREATER=0x3E
UNDERSCORE=0x5F

label parse_push # (val: i32) -> ()
	movl @parse_stack_ptr %ecx
	cmpl parse_stack_top %ecx
	je overflow.
	subl 4 %ecx
	movl %eax @%ecx
	movl %ecx @parse_stack_ptr
	ret

label parse_peek # () -> (val: i32)
	movl @parse_stack_ptr %eax
	movl @%eax %eax
	ret

label parse_pop # () -> (val: i32)
	movl @parse_stack_ptr %eax
	addl 4 @parse_stack_ptr
	movl @%eax %eax
	ret

L overflow
	movl msg_parse_overflow %eax
	movl msg_parse_overflow_end %ecx
	call report_error
	jmp error
rodata
string msg_parse_overflow "Parse stack overflow
"
text

label parse # (start: *i8, end: *i8) -> (term: *term)
	pushl %ebx
	pushl %edi
	pushl %esi
	pushl %ebp
	movl %eax @input
	movl %ecx @input_end
	call align_heap
	movl $STK_BOTTOM %eax
	call parse_push
	# push two bottoms -- this simplifies handling of unmatched )
	movl $STK_BOTTOM %eax
	call parse_push
	# push an extra poison to the type stack
	call ty_poison
	movl 0 @lineno
	movl 0 @error_flag # this must occur after the ty_poison above
L loop
	call skip_white
	movl @input %eax
	cmpl @input_end %eax
	je eof.
	cmpb $RPAREN @%eax
	je right_paren.
	call parse_peek
	testl $STK_HAVE_VALUE %eax
	jz parse_value.
	call ty_top_is_forall
	jne parse_value.
	call parse_type
	call ty_apply_forall
	jmp loop^

L parse_value
	movl @input %eax
	movb @%eax %al
	cmpb $BACKSLASH %al
	je small_lambda.
	cmpb $SLASH %al
	je big_lambda.
	cmpb $LPAREN %al
	je left_paren.
	# Variable terms
	call read_ident
	movl $STK_SMALL_LAMBDA %edx
	call find_ident
	movl %eax %esi
	movl %ecx %ebp
	movl %edx %edi
	movl @heap_ptr %ecx
	call push_value
	testl %esi %esi
	js bad_ident.
	movl %edi %eax
	movl %ebp %ecx
	call ty_copy
	jmp value_done.
L bad_ident
	movl %esp %eax
	call ty_poison
L value_done
	movl %esp %eax
	call apply
	jmp loop^

L small_lambda
	incl @input
	call skip_white
	call read_ident
	movl %eax %esi
	movl %ecx %eax
	call parse_push
	movl %esi %eax
	call parse_push
	movl $STK_SMALL_LAMBDA %eax
	call parse_push
	call skip_white
	movl @input %eax
	cmpl @input_end %eax
	je no_colon.
	movb @%eax %al
	cmpb $COLON %al
	jne no_colon.
	incl @input
	call parse_type
	jmp loop^

L no_colon
	call stderr_lineno
	stderr "Missing argument type in lambda abstraction
"
	call ty_poison
	movl 1 @error_flag
	jmp loop^

L big_lambda
	movl @input %eax
	incl %eax
	cmpl @input_end %eax
	je bad_char
	movb @%eax %al
	cmpb $BACKSLASH %al
	jne bad_char
	addl 2 @input
	call skip_white
	call read_ident
	movl %eax %ebx
	movl %ecx %eax
	call parse_push
	movl %ebx %eax
	call parse_push
	movl $STK_BIG_LAMBDA %eax
	call parse_push
	jmp loop^

L left_paren
	incl @input
	movl @lineno %eax
	call parse_push
	movl $STK_PAREN %eax
	call parse_push
	jmp loop^

L right_paren
	incl @input
	movl $STK_PAREN %eax
	call close_paren
	jmp loop^

L eof
	movl $STK_BOTTOM %eax
	call close_paren
	popl %ebp
	popl %esi
	popl %edi
	popl %ebx
	movl @error_flag %eax
	testl %eax %eax
	jnz error
	ret

label close_paren # (type: i32) -> ()
	pushl %ebx
	pushl %edi
	pushl %ebp
	movl %eax %ebx
	# %ebx -- type wanted
	call parse_pop
	movl %eax %edi
	# %edi -- type on stack
	testl $STK_HAVE_VALUE %edi
	jz no_value.
L have_value
	testl $STK_HAVE_IDENT %edi
	jz skip.
	call parse_pop
	call parse_pop
L skip
	testl $STK_SMALL_LAMBDA %edi
	jnz small_lambda.
	testl $STK_BIG_LAMBDA %edi
	jz skip.
L big_lambda
	call ty_forall
	jmp skip.
L small_lambda
	call ty_arrow
	movl @heap_ptr %ecx
	movl @%ecx+-8 %ecx
	movl $VAL_ABS %eax
	call push_value
L skip
	testl $STK_HAVE_LINENO %edi
	jz no_lineno.
	call parse_pop
	movl %eax %ebp
	# %ebp -- lineno
L no_lineno
	call apply
	testl %ebx %edi
	jnz end.
	testl $STK_BOTTOM %edi
	jnz unmatched_right.
	testl $STK_PAREN %edi
	jnz unmatched_left.
L continue
	call parse_pop
	movl %eax %edi
	jmp have_value^ # must have value since we just called apply

L end
	popl %ebp
	popl %edi
	popl %ebx
	ret

L unmatched_right
	movl 1 @error_flag
	call stderr_lineno
	stderr "Unmatched ')'
"
	call parse_pop
	movl $STK_BOTTOM %eax
	call parse_push
	movl $((STK_BOTTOM|STK_HAVE_VALUE)) %eax
	call parse_push
	jmp end^

L unmatched_left
	movl 1 @error_flag
	xchgl %ebp @lineno
	call stderr_lineno
	movl %ebp @lineno
	stderr "Unmatched '('
"
	jmp continue^

L no_value
	# Push a poison value so we can continue
	orl $STK_HAVE_VALUE %edi
	movl 1 @error_flag
	movl @heap_ptr %ecx
	call push_value
	call ty_poison
	call stderr_lineno
	# %edi -- parse stack element
	testl $STK_PAREN %edi
	jnz paren.
	testl $STK_SMALL_LAMBDA %edi
	jnz small.
	testl $STK_BIG_LAMBDA %edi
	jnz big.
	testl $STK_BOTTOM %edi
	jnz bottom.
	ud2
L paren
	stderr "Empty parentheses
"
	jmp have_value^
L small
	stderr "Empty lambda abstraction
"
	jmp have_value^
L big
	stderr "Empty type lambda abstraction
"
	jmp have_value^
L bottom
	stderr "Empty program
"
	jmp have_value^

# We have just pushed a value. Check if it is an argument, and push the
# application if so.
label apply # () -> ()
	call parse_peek
	testl $STK_HAVE_VALUE %eax
	jnz do_apply.
	call parse_pop
	orl $STK_HAVE_VALUE %eax
	call parse_push
	ret
L do_apply
	call ty_apply
	movl @heap_ptr %ecx
	movl @%ecx+-8 %ecx
	movl @%ecx+-8 %ecx
	movl $VAL_APP %eax
	call push_value
	ret

label parse_type # () -> ()
	pushl %ebx
	pushl %edi
	pushl %esi
	movl $STK_TYPE_BOTTOM %eax
	call parse_push
L loop
	call skip_white
	movl @input %eax
	cmpl @input_end %eax
	je bad_eof
	movb @%eax %al
	cmpb $BACKSLASH %al
	je forall.
	cmpb $LPAREN %al
	je left_paren.
	# Variable
	call read_ident
	movl %eax %edi
	movl %ecx %esi
	# %edi..%esi -- ident
	movl @macro_ptr %ebx
	# %ebx -- macro iterator
L find_macro
	cmpl macro_start %ebx
	je no_macro.
	subl 16 %ebx
	movl %edi %eax
	movl %esi %ecx
	movl @%ebx %edx
	pushl @%ebx+4
	call streq
	je found_macro.
	jmp find_macro^

L found_macro
	movl @%ebx+12 %eax
	movl @%ebx+8 %ebx
	call ty_copy
	# %ebx -- remaining arguments
	call skip_white
	movl @input %eax
	cmpl @input_end %eax
	je no_args.
	cmpb $LBRACKET @%eax
	jne no_args.
	incl @input
L next_arg
	call parse_type
	decl %ebx
	js extra_arg.
	call ty_apply_forall
L arg_done
	call skip_white
	movl @input %eax
	cmpl @input_end %eax
	je bad_eof
	cmpb $RBRACKET @%eax
	jne next_arg^

	incl @input
L no_args
	testl %ebx %ebx
	jg missing_args.
	jl extra_args.
L have_args
	jmp op.

L extra_arg
	call ty_pop
	jmp arg_done^

L extra_args
rodata
string msg_extra_arg "Extraneous macro arguments
"
text
	movl msg_extra_arg %eax
	movl msg_extra_arg_end %ecx
	call report_error
	movl 1 @error_flag
	jmp have_args^

L missing_args
rodata
string msg_missing_args "Missing macro arguments
"
text
	movl msg_missing_args %eax
	movl msg_missing_args_end %ecx
	call report_error
	movl 1 @error_flag
L fake_args
	call ty_poison
	call ty_apply_forall
	decl %ebx %ebx
	jnz fake_args^
	jmp have_args^

L no_macro
	movl %edi %eax
	movl %esi %ecx
	movl $((STK_BIG_LAMBDA|STK_TYPE_FORALL)) %edx
	call find_ident
	testl %eax %eax
	js not_found.
	movl %ecx %eax
	call ty_push
	jmp op.
L not_found
	call ty_poison
	jmp op.

L forall
	movl @input %eax
	incl %eax
	cmpl @input_end %eax
	je bad_eof
	addl 2 @input
	movb @%eax %al
	cmpb $SLASH %al
	jne bad_char
	call skip_white
	call read_ident
	movl %eax %ebx
	movl %ecx %eax
	call parse_push
	movl %ebx %eax
	call parse_push
	movl $STK_TYPE_FORALL %eax
	call parse_push
	jmp loop^

L left_paren
	incl @input
	movl @lineno %eax
	call parse_push
	movl $STK_TYPE_PAREN %eax
	call parse_push
	jmp loop^

L op
	call skip_white
	movl @input %eax
	cmpl @input_end %eax
	je end.
	movb @%eax %al
	cmpb $HYPHEN %al
	je arrow.
	cmpb $RPAREN %al
	je right_paren.
	jmp end.

L arrow
	movl @input %eax
	incl %eax
	cmpl @input_end %eax
	je end.
	movb @%eax %al
	cmpb $GREATER %al
	jne end.
	addl 2 @input
	movl $STK_TYPE_ARROW %eax
	call parse_push
	jmp loop^

L right_paren
	movl $((STK_TYPE_PAREN|STK_TYPE_BOTTOM)) %eax
	call close_type_paren
	testl $STK_TYPE_PAREN %eax
	jz done.
	incl @input
	jmp op^

L end
	movl $STK_TYPE_BOTTOM %eax
	call close_type_paren
L done
	popl %esi
	popl %edi
	popl %ebx
	ret

label close_type_paren # (types: i32) -> (type: i32)
	pushl %ebx
	pushl %edi
	pushl %esi
	movl %eax %ebx
	# %ebx -- wanted types
L loop
	call parse_pop
	movl %eax %edi
	# %edi -- current type
	testl $STK_HAVE_IDENT %edi
	jz no_ident.
	call parse_pop
	call parse_pop
L no_ident
	testl $STK_HAVE_LINENO %edi
	jz no_lineno.
	call parse_pop
	movl %eax %esi
	# %esi -- lineno
L no_lineno
	testl %ebx %edi
	jnz done.
	testl $STK_TYPE_ARROW %edi
	jnz arrow.
	testl $STK_TYPE_PAREN %edi
	jnz paren.
	testl $STK_TYPE_FORALL %edi
	jnz forall.
	ud2

L arrow
	call ty_arrow
	jmp loop^

L paren
	movl 1 @error_flag
	xchgl %esi @lineno
	call stderr_lineno
	movl %esi @lineno
	stderr "Unmatched '('
"
	jmp loop^

L forall
	call ty_forall
	jmp loop^

L done
	movl %edi %eax
	popl %esi
	popl %edi
	popl %ebx
	ret

label find_ident # (start: *i8, end: *i8, type: i32) -> (vidx: i32, tidx: i32, type: *type)
	pushl %ebx
	pushl %esi
	pushl %edi
	pushl %ebp
	movl %edx %ebx
	pushl 0
	pushl 0
	pushl %ecx
	pushl %eax
	call ty_top
	movl %eax %edi
	movl @parse_stack_ptr %esi
	# %ebx -- type
	# @%esp -- ident
	# @%esp+4 -- ident end
	# @%esp+8 -- value index
	# @%esp+12 -- type index
	# %esi -- pointer running down the stack
	# %edi -- type stack pointer
L loop
	# %ebp -- type & flags
	movl @%esi %ebp
	addl 4 %esi
	testl $STK_HAVE_VALUE %ebp
	jz no_value.
	movl %edi %eax
	call ty_skip
	movl %eax %edi
L no_value
	testl $STK_HAVE_IDENT %ebp
	jz no_ident.
	movl @%esi %eax
	movl @%esi+4 %ecx
	addl 8 %esi
	movl @%esp %edx
	pushl @%esp+4
	call streq
	je found.
L no_ident
	testl $STK_SMALL_LAMBDA %ebp
	jz no_type.
	movl %edi %eax
	call ty_skip
	movl %eax %edi
L no_type
	leal @%esi+4 %eax
	testl $STK_HAVE_LINENO %ebp
	cmovnz %eax %esi
	testl $((STK_BIG_LAMBDA|STK_TYPE_FORALL)) %ebp
	jz no_type_inc.
	incl @%esp+12
L no_type_inc
	testl $STK_SMALL_LAMBDA %ebp
	jz no_value_inc.
	incl @%esp+8
L no_value_inc
	testl $STK_BOTTOM %ebp
	jz loop^
	movl @%esp %eax
	movl @%esp+4 %ecx
	call missing_ident
L error
	movl -1 %eax
	movl 1 @error_flag
	jmp end.
L found
	testl %ebx %ebp
	jz bad_ident.
	movl @%esp+8 %eax
	movl @%esp+12 %ecx
	movl %edi %edx
L end
	addl 16 %esp
	popl %ebp
	popl %edi
	popl %esi
	popl %ebx
	ret

L bad_ident
	movl @%esp %eax
	movl @%esp+4 %ecx
	movl %ebx %edx
	call bad_ident
	jmp error^

label read_ident # () -> (start: *i8, end: *i8)
	movl @input %eax
	movl %eax %ecx
L loop
	cmpl @input_end %ecx
	je done.
	movb @%ecx %dl
	incl %ecx
	cmpb $UNDERSCORE %dl
	je loop^
	cmpb 0x30 %dl
	jb done_ungetc.
	cmpb 0x39 %dl
	jbe loop^
	cmpb 0x41 %dl
	jb done_ungetc.
	cmpb 0x5A %dl
	jbe loop^
	cmpb 0x61 %dl
	jb done_ungetc.
	cmpb 0x7A %dl
	jbe loop^
L done_ungetc
	decl %ecx
L done
	cmpl %eax %ecx
	je bad_char
	movl %ecx @input
	ret

label bad_char
	movl msg_bad_char %eax
	movl msg_bad_char_end %ecx
	call report_error
	jmp error

label bad_eof
	cmpl 0 @macro_flag
	jnz macro.
	movl msg_bad_eof %eax
	movl msg_bad_eof_end %ecx
	call report_error
	jmp error
L macro
	movl msg_bad_eof_macro %eax
	movl msg_bad_eof_macro_end %ecx
	call report_error
	jmp error

rodata
string msg_bad_char "Unexpected character
"
string msg_bad_eof "Unexpected end of file
"
string msg_bad_eof_macro "Unexpected end of line
"
text

# Skip whitespace including comments and macros.
label skip_white
	movl @input %edx
	xorl %ecx %ecx
	cmpl %ecx @lineno
	jne loop.
	incl @lineno
	incl %ecx
L loop
	cmpl @input_end %edx
	je done.
	movb @%edx %al
	cmpb $SPACE %al
	je white.
	cmpb $NEWLINE %al
	je newline.
	cmpb $TAB %al
	je white.
	cmpb $SEMICOLON %al
	je comment.
	cmpb $OCTOTHORPE %al
	je macro.
L done
	movl %edx @input
	ret
L comment
	incl %edx
	cmpl @input_end %edx
	je done^
	cmpb $NEWLINE @%edx
	jne comment^
L newline
	incl @lineno
	movl 1 %ecx
	incl %edx
	jmp loop^
L white
	xorl %ecx %ecx
	incl %edx
	jmp loop^
L macro
	testl %ecx %ecx
	jz done^

	pushl %ebx
	pushl %edi

	# Macro handlings.
	# 1. Find end of line
	incl %edx
	movl %edx @input
L loop
	cmpl @input_end %edx
	je eol.
	cmpb $NEWLINE @%edx
	je eol.
	incl %edx
	jmp loop^
L eol
	pushl @input_end
	movl %edx @input_end
	movl 1 @macro_flag
	# Now, the entire input is just this line. So we can use normal parse
	# functions to parse the line. There is no risk of infinite recursion since
	# the input consists of a single line, so there are no newlines.
	movl $STK_BOTTOM %eax
	call parse_push

	call read_ident
	movl @macro_ptr %edx
	cmpl macro_end %edx
	je macro_overflow.
	movl %eax @%edx
	movl %ecx @%edx+4
	movl @input %eax
	cmpl @input_end %eax
	je bad_eof
	cmpb $LBRACKET @%eax
	jne no_args.
	incl @input
	call skip_white
L loop
	call read_ident
	movl %eax %ebx
	movl %ecx %eax
	call parse_push
	movl %ebx %eax
	call parse_push
	movl $STK_BIG_LAMBDA %eax
	call parse_push
	call skip_white
	movl @input %eax
	cmpl @input_end %eax
	je bad_eof
	cmpb $RBRACKET @%eax
	jne loop^
	incl @input
L no_args
	call parse_type
	movl @input %eax
	cmpl @input_end %eax
	je ok.
rodata
string msg_macro_extra "Extraneous input at end of macro
"
text
	movl msg_macro_extra %eax
	movl msg_macro_extra_end %ecx
	call report_error
	movl 1 @error_flag

L ok
	xorl %edi %edi
	# %edi -- num args
L loop
	call parse_pop
	testl $STK_BOTTOM %eax
	jnz done.
	incl %edi
	call parse_pop
	call parse_pop
	call ty_forall
	jmp loop^
L done
	movl @macro_ptr %eax
	movl %edi @%eax+8
	movl @macro_type_ptr %edi
	movl %edi @%eax+12
	call ty_top
	movl %eax %ebx
	call ty_skip
	leal @%edi+%eax %ecx
	subl %ebx %ecx
	cmpl macro_types_end %ecx
	ja macro_overflow.
	movl %ecx @macro_type_ptr
	# %ebx -- src
	# %eax -- src end
	# %edi -- dst
	# %ecx -- dst end
L loop
	movl @%ebx %edx
	movl %edx @%edi
	addl 4 %ebx
	addl 4 %edi
	cmpl %eax %ebx
	jne loop^

	call ty_pop
	addl 16 @macro_ptr
	movl @input_end %eax
	movl %eax @input
	popl @input_end
	movl 0 @macro_flag
	popl %edi
	popl %ebx
	jmp skip_white

L macro_overflow
	movl msg_macro_overflow %eax
	movl msg_macro_overflow_end %ecx
	call report_error
	jmp error
rodata
string msg_macro_overflow "Too many macros
"
text

label push_value # (val: i32, start ptr: *i32)
	movl @heap_ptr %edx
	addl 8 %edx
	movl %edx @heap_ptr
	cmpl @heap_end %edx
	ja alloc.
L have_space
	movl %eax @%edx+-4
	movl %ecx @%edx+-8
	ret
L alloc
	pushl %eax
	pushl %ecx
	call alloc
	popl %ecx
	popl %eax
	movl @heap_ptr %edx
	jmp have_space^

label missing_ident # (start: *i8, end: *i8) -> ()
	pushl %eax
	pushl %ecx
	call stderr_lineno
	stderr "Name not found: "
	popl %ecx
	popl %eax
	call stderr
	stderr "
"
	ret

label bad_ident # (start: *i8, end: *i8, wanted-type: i32)
	pushl %edx
	pushl %eax
	pushl %ecx
	call stderr_lineno
	popl %ecx
	popl %eax
	call stderr
	stderr " is not a "
	popl %eax
	call stderr_type
	stderr "
"
	ret

label stderr_type # (type: i32)
	testl $STK_SMALL_LAMBDA %eax
	jnz small.
L big
	stderr "type variable"
	ret
L small
	stderr "term variable"
	ret