Browse code

(asma) Add tail call optimisation, bringing it up-to-date with uxnasm

Andrew Alderwick authored on 07/06/2022 23:10:24
Showing 1 changed files
... ...
@@ -222,7 +222,7 @@
222 222
 	;asma-read-buffer DUP2 ;asma-read-buffer/end ROT2 SUB2 ( func* line^ filename* buf* size^ )
223 223
 	ROT2 ( func* line^ buf* size^ filename* )
224 224
 	,file-read-chunks JSR
225
-	;asma-flush-lit JSR2
225
+	;asma-flush-held JSR2
226 226
 
227 227
 	asma-IF-ERROR ,&error JCN
228 228
 
... ...
@@ -348,7 +348,7 @@
348 348
 @asma [
349 349
 	&pass $1 &state $1 &line $2 &lines $2 &break $1 &eof $1
350 350
 	&comment-level $1
351
-	&token $2 &orig-token $2 &lit $1 &lit-present $1
351
+	&token $2 &orig-token $2 &lit $1 &lit-present $1 &jsr $1
352 352
 	&addr $2 &written-addr $2 &flush-fn $2
353 353
 	&src-filename $2 &dest-filename $2
354 354
 	&error $2 &log-level $1
... ...
@@ -518,6 +518,7 @@
518 518
 
519 519
 @asma-write-lit ( byte -- )
520 520
 	;asma/lit LDA2 ,&present JCN
521
+	,asma-flush-held JSR
521 522
 	POP #01 ;asma/lit STA2
522 523
 	;asma/addr LDA2k INC2 INC2 SWP2 STA2
523 524
 	JMP2r
... ...
@@ -529,18 +530,26 @@
529 530
 	#0000 ;asma/lit STA2
530 531
 	JMP2r
531 532
 
532
-@asma-flush-lit ( -- )
533
-	;asma/lit LDA2 ,&present JCN
534
-	POP JMP2r
535
-	&present
533
+@asma-flush-held ( -- )
534
+	;asma/lit LDA2 ,&lit-present JCN
535
+	POP ,&part2 JMP
536
+	&lit-present
536 537
 	;asma/addr LDA2k #0002 SUB2 SWP2 STA2
537 538
 	LIT LIT ,asma-write-byte/raw JSR
538 539
 	,asma-write-byte/raw JSR
539 540
 	#0000 ;asma/lit STA2
541
+
542
+	&part2
543
+	;asma/jsr LDA DUP ,&jsr-present JCN
544
+	POP JMP2r
545
+	&jsr-present
546
+	;asma/addr LDA2k #0001 SUB2 SWP2 STA2
547
+	,asma-write-byte/raw JSR
548
+	#00 ;asma/jsr STA
540 549
 	JMP2r
541 550
 
542 551
 @asma-write-byte ( byte -- )
543
-	,asma-flush-lit JSR
552
+	,asma-flush-held JSR
544 553
 	&raw
545 554
 	;asma/addr LDA2 ;asma/written-addr LDA2
546 555
 	LTH2k ,&rewound JCN
... ...
@@ -659,7 +668,7 @@
659 668
 	JMP2r
660 669
 
661 670
 @asma-label-define
662
-	;asma-flush-lit JSR2
671
+	;asma-flush-held JSR2
663 672
 	;asma-trees/labels ,asma-label-helper JSR
664 673
 	,&already-existed JCN
665 674
 
... ...
@@ -670,7 +679,7 @@
670 679
 	JMP2r
671 680
 
672 681
 @asma-sublabel-define
673
-	;asma-flush-lit JSR2
682
+	;asma-flush-held JSR2
674 683
 	;asma-trees/scope LDA2 ,asma-label-helper JSR
675 684
 	POP POP2
676 685
 	JMP2r
... ...
@@ -700,11 +709,11 @@
700 709
 	#00 JMP2r
701 710
 
702 711
 @asma-pad-absolute
703
-	;asma-flush-lit JSR2
712
+	;asma-flush-held JSR2
704 713
 	#0000 ,asma-pad-helper JMP
705 714
 
706 715
 @asma-pad-relative
707
-	;asma-flush-lit JSR2
716
+	;asma-flush-held JSR2
708 717
 	;asma/addr LDA2
709 718
 	( fall through )
710 719
 
... ...
@@ -850,8 +859,26 @@
850 859
 
851 860
 @asma-normal-body
852 861
 	;asma-parse-opcode JSR2 ,&not-opcode JCN
862
+	DUP [ LIT JSR ] EQU ,&hold-jsr JCN
863
+	DUP [ LIT JSR2 ] EQU ,&hold-jsr JCN
864
+	DUP [ LIT JMP2r ] NEQ ,&write-opcode JCN
865
+	;asma/jsr LDA ,&optimise-jsr-jmp2r JCN
866
+	&write-opcode
853 867
 	;asma-write-byte JMP2 ( tail call )
854 868
 
869
+	&hold-jsr
870
+	;asma-flush-held JSR2
871
+	;asma/jsr STA
872
+	;asma/addr LDA2k INC2 SWP2 STA2
873
+	JMP2r
874
+
875
+	&optimise-jsr-jmp2r
876
+	POP
877
+	;asma/jsr LDAk #00 SWP2 STA
878
+	#e0 AND [ LIT JMP ] ORA
879
+	;asma/addr LDA2k #0001 SUB2 SWP2 STA2
880
+	,&write-opcode JMP
881
+
855 882
 	&not-opcode
856 883
 	#01 ;asma-parse-hex-string JSR2 JMP
857 884
 	( hex invalid ) ,&not-hex JMP