Cowgol programming language

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
hjalfi
Posts: 107
Joined: 12 Oct 2017

Cowgol programming language

Post by hjalfi »

...so at this point, anybody who also reads stardot is now rolling their eyes, because I've been boring people stiff about this there. But, hey, I feel I'm entitled to at least a few minutes smugness, and it may be of interest here.

I've just released Cowgol, a fully compiled, almost self-hosted strongly typed Ada-inspired modern programming language for the 6502.

http://cowlark.com/cowgol
https://github.com/davidgiven/cowgol

It's not quite self-hosting (yet) due to not quite having enough RAM but I'm sure could be made so. The generated code quality is pretty naff but it works. It's a multipass compiler with eight separate executables and it makes heavy use of disk storage, making it very very slow to compile --- eight minutes for a 'Hello world'! But it works. Mostly I cross compile from Linux.

It's targeting a BBC Micro with Tube, because that's the machine I'm used to and because it provides about 61kB of RAM plus disk operating system with file streams. It should be fairly easily portable to other platforms. I'm sure there's a C64 DOS which uses mapping tricks to get enough memory, somewhere? I've never worked with a C64; anyone who wants to help, please get in touch...

The language itself is rather boring but has a number of features I really like: multiple return parameters, nested subroutines, and strict typing. It turns out to be surprisingly comfortable to, e.g., write compilers in, which is a good thing, really. It forbids recursion to allow static placement of variables, and it walks the call graph to figure out which variables can safely share storage. This works really well, and even the biggest programs can, e.g., fit every pointer variable into less than half of zero page.

Here's some sample code:

https://github.com/davidgiven/cowgol/bl ... shower.cow

...I've been thinking about this since I was 8, so even in its incomplete, buggy state, I'm kinda pleased to actually achieve this!
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Cowgol programming language

Post by BigEd »

Excellent! I like the idea of following the Algol heritage in that Wirthian direction - more types, more scope.
bdk6
Posts: 20
Joined: 01 Oct 2017

Re: Cowgol programming language

Post by bdk6 »

That's a really nice piece of work! I personally think Ada is underrated and should be used more. I hope to get some time to play around with Cowgol in the near future. It looks pretty interesting.
User avatar
hjalfi
Posts: 107
Joined: 12 Oct 2017

Re: Cowgol programming language

Post by hjalfi »

I've just committed the basic skeleton of Commodore 64 support --- the compiler won't run natively (the disk system's a bit weird and doesn't support seekable byte streams), but you can cross compile simple programs.

Any other 6502 based systems I should look at? Apple II DOS? I haven't found a programming manual yet...
Tor
Posts: 597
Joined: 10 Apr 2011
Location: Norway/Japan

Re: Cowgol programming language

Post by Tor »

I like the look of this language. Very interesting.
No recursion isn't much of a problem - I only use recursion for some particular parsers I write. That this removes the need for stack frames is a big help for e.g. the 6502.
User avatar
hjalfi
Posts: 107
Joined: 12 Oct 2017

Re: Cowgol programming language

Post by hjalfi »

This isn't dead! I've just kicked 0.3 out the door. Lots of bug fixes and much faster code (compilation on the BBC Micro is literally twice as fast, although that's mostly due to disk buffering).

I need to seriously start thinking about 16-bit 6502 arithmetic and pointer accesses. It won't be pretty.
kakemoms
Posts: 349
Joined: 02 Mar 2016

Re: Cowgol programming language

Post by kakemoms »

hjalfi wrote:
This isn't dead! I've just kicked 0.3 out the door. Lots of bug fixes and much faster code (compilation on the BBC Micro is literally twice as fast, although that's mostly due to disk buffering).

I need to seriously start thinking about 16-bit 6502 arithmetic and pointer accesses. It won't be pretty.
Any thoughts about 65C816 support?
User avatar
hjalfi
Posts: 107
Joined: 12 Oct 2017

Re: Cowgol programming language

Post by hjalfi »

It'd be totally possible. Right now the whole toolchain assumes 16 bit addresses, so dealing with multiple banks would be tricky, but assuming your program fit within a single bank then doing a code generator probably wouldn't be hard.

The 6502 is tough to generate code for because of the unorthogonal addressing modes and very limited register sets. Using 16-bit register mode would simplify things a lot (lda (ptr), y with unlimited offsets!). It'd also mean much more natural 16-bit arithmetic, which would be a big win.

But I'm not planning on doing this any time soon --- too much other stuff to work on (in rough order: improve 6502 code generation; throw away the parser and replace it with a smaller and less buggy table-based one; do Z80 port). OTOH the interfaces are reasonably stable, if hideously undocumented, so if anyone wants a look...

https://github.com/davidgiven/cowgol/tr ... /arch/6502

(Probably wouldn't recommend it. The 6502 code generator is awful. Hopefully once I've done the Z80 one I'll have had a chance to clean up the API a bit.)
User avatar
hjalfi
Posts: 107
Joined: 12 Oct 2017

Re: Cowgol programming language

Post by hjalfi »

Cowgol's still going strong, BTW. The latest release is 0.6.1 and features substantially improved code generation for the 6502 plus a whole new Z80 code generator for CP/M and Fuzix (but nobody here will care about that, right?). It's also got some minor architectural improvements which let it make better use of memory; self-hosting may finally be possible on a system with enough RAM, and you should be able to compile useful (but small) programs in 48kB.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Cowgol programming language

Post by BigEd »

As Acorn's BBC Micro happily acts as a front-end to a Z80, which can run CP/M, and maybe one day Fuzix, yes - interested!
Post Reply