Wolin - a minimal Kotlin-like language compiler for 65xx

Programming the 6502 microprocessor and its relatives in assembly and other languages.
resman
Posts: 154
Joined: 12 Dec 2015
Location: Lake Tahoe
Contact:

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by resman »

qus wrote:
Thanks for your insights - yes, the optimizer code is a real mess, especialy pointer/reference substitution is rather... chaotic and probably wrong and will kick me in the face some day, but it works... for now.
Parsing and code generation for indirection and dereferencing expressions was without a doubt the hardest part of the PLASMA compiler. I guarantee there are cases it still gets wrong.
qus wrote:
Reading your PLASMA docs I guess I could learn a lot from you, so when you have time I would probably have some questions to ask!
I'd be happy to share any insight I may have. Our projects are aimed at slightly different environments, but any way to move the 6502 forward as a compilation target is helpful to all of us. Ping me on the PLASMA project page or PM me any time.
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

Great, thanks!

So now I'm working on the last thing that is stopping Wolin from generating sane code - shuffling allocations and deallocations around. First - due to how optimizer works, sometimes a register is moved beyond its deallocation. Second - I need to move allocations/deallocations out of loops, so they don't get unnecessarily created and destroyed on each iteration. When this is ready, all that will be left (besides those damn pointers) is the language itself.

But meantime I have a question for anyone who ever created a copiler. How do you handle a return witha value that is not last piece of code before endproc? How do you clean open stacks?
resman
Posts: 154
Joined: 12 Dec 2015
Location: Lake Tahoe
Contact:

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by resman »

qus wrote:
Great, thanks!

...

But meantime I have a question for anyone who ever created a copiler. How do you handle a return witha value that is not last piece of code before endproc? How do you clean open stacks?
For PLASMA, I keep a count of how many items are on the stack. It's really easy for me though, PLASMA only keeps values on the evaluation stack (your SP) between expressions during FOR/NEXT loops. For your case, the easiest and most bullet-proof approach could be to save the SP index (X register) on the stack (HW or SPF) as part of your function entry setup, and restore it when leaving the function.
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

Heh! Indeed, the easiest solution. I probably need to do the same to frame stack.
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

Can anyone help with linking in ca65/cl65?

I've added "external" annotation to Wolin, so I can have empty functions like these:

Code: Select all

external fun extPrint(what: string)
Mangled into asm as: "__wolin_pl_qus_wolin_extPrint". The function is implemented in some asm library, ie:

Code: Select all

__wolin_spf := 251 ; function stack ptr

.macro lda_spf_lo idx
	ldy #idx
	lda (__wolin_spf),y
.endmacro

.macro lda_spf_hi
	iny
	lda (__wolin_spf),y
.endmacro

.macro sta_sp idx
	sta idx, x
.endmacro

.macro allocsp_byte
	dex
.endmacro

.macro allocsp_word
	dex
	dex
.endmacro

.macro freesp_word
	inx
	inx
.endmacro


.macro alloc_spf count
    sec
    lda __wolin_spf
    sbc #count
    sta __wolin_spf
    bcs :+
    dec __wolin_spf+1
:
.endmacro

.macro free_spf count
    clc
    lda __wolin_spf
    adc #count
    sta __wolin_spf
    bcc :+
    inc __wolin_spf+1
:
.endmacro


.export __wolin_pl_qus_wolin_extPrint
__wolin_pl_qus_wolin_extPrint:
	allocsp_word
	lda_spf_lo 0
	sta_sp 0
	lda_spf_hi
	sta_sp 1
	lda (0,x)
	sta 53280
	freesp_word
	free_spf 2
I compile the above library into library.o and try to link it:

Code: Select all

cl65.exe -o assembler.prg -t c64 -C c64-asm.cfg -g -Ln assembler.lbl -l assembler.lst --obj library.o assembler.s
assembler.s(92): Error: Symbol '__wolin_pl_qus_wolin_extPrint' is undefined
What's wrong?
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by BigEd »

Is it possible you have to put the export after the definition?
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

It was possible, but it didn't help.
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by BigEd »

Hmm, can you show the source with the line numbers, just a few lines above and below the line it reports?
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

sure

Code: Select all

; 8: function __wolin_pl_qus_wolin_main 

__wolin_pl_qus_wolin_main:

; 9: alloc SPF<__wolin_pl_qus_wolin_extPrint> , #2 


    sec
    lda __wolin_spf
    sbc #2
    sta __wolin_spf
    bcs :+
    dec __wolin_spf+1
:

; 10: let SPF(0)<pl.qus.wolin.extPrint.what>[ubyte*] = #__wolin_lab_stringConst_0[uword] 


    lda #<__wolin_lab_stringConst_0
    ldy #0
    sta (__wolin_spf),y
    lda #>__wolin_lab_stringConst_0
    iny
    sta (__wolin_spf),y

; 11: call __wolin_pl_qus_wolin_extPrint[uword] 

    jsr __wolin_pl_qus_wolin_extPrint

; 12: endfunction 

    rts

; 13: string __wolin_lab_stringConst_0[uword] = $"dupa" 


__wolin_lab_stringConst_0:
    .asciiz "dupa"
    ;.byt 0
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by BigEd »

Is it now reporting line 11 as the problem line?
(Edit: Previously it was line 92 of the assembler.s)
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

This is not a full source. The line is the same - where __wolin_pl_qus_wolin_extPrint is referenced.
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by BigEd »

What if you remove the --obj and just mention library.o as a parameter?
Are you certain that library.o is recently built and from the source that you expect
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

Same thing. I added "--obj" when trying to make it work. I've built the "library" a moment ago, with just "ca65 library.s".
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by BigEd »

The only other thing I can think of is to try putting library.o after assembler.s, even though that feels wrong!
qus
Posts: 104
Joined: 20 Apr 2019

Re: Wolin - a minimal Kotlin-like language compiler for 65xx

Post by qus »

Code: Select all

>cl65.exe -o assembler.prg -t c64 -C c64-asm.cfg -g -Ln assembler.lbl -l assembler.lst  assembler.s library.o
assembler.s(92): Error: Symbol '__wolin_pl_qus_wolin_extPrint' is undefined
Post Reply