Page 10 of 12

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

Posted: Tue Apr 21, 2020 2:23 pm
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.

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

Posted: Fri Apr 24, 2020 7:13 am
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?

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

Posted: Fri Apr 24, 2020 6:49 pm
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.

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

Posted: Sat Apr 25, 2020 8:54 am
by qus
Heh! Indeed, the easiest solution. I probably need to do the same to frame stack.

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

Posted: Sat May 02, 2020 10:26 am
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?

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

Posted: Sat May 02, 2020 10:31 am
by BigEd
Is it possible you have to put the export after the definition?

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

Posted: Sat May 02, 2020 10:39 am
by qus
It was possible, but it didn't help.

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

Posted: Sat May 02, 2020 10:44 am
by BigEd
Hmm, can you show the source with the line numbers, just a few lines above and below the line it reports?

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

Posted: Sat May 02, 2020 10:45 am
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

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

Posted: Sat May 02, 2020 10:47 am
by BigEd
Is it now reporting line 11 as the problem line?
(Edit: Previously it was line 92 of the assembler.s)

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

Posted: Sat May 02, 2020 11:40 am
by qus
This is not a full source. The line is the same - where __wolin_pl_qus_wolin_extPrint is referenced.

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

Posted: Sat May 02, 2020 11:46 am
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

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

Posted: Sat May 02, 2020 11:52 am
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".

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

Posted: Sat May 02, 2020 11:58 am
by BigEd
The only other thing I can think of is to try putting library.o after assembler.s, even though that feels wrong!

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

Posted: Sat May 02, 2020 12:00 pm
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