Now Reading
Cowgol 2.0

Cowgol 2.0

2023-12-10 05:15:08

Cowgol is an experimental, Ada-inspired language for very small programs
(6502, Z80, and many others). It’s totally different as a result of it’s meant to be self-hosted on
these gadgets: the top purpose is to have the ability to rebuild the complete compiler on
an 8-bit micro, though we’re not there but.

Right here’s the bullet level record of options:

  • a correctly kind protected, trendy language impressed by Ada

  • the compiler is written in itself and is absolutely bootstrapped

  • a table-driven, simple to port backend (the 80386 backend is 1.2kloc with
    no different compiler modifications wanted)

  • tiny: the 80386 Linux compiler binary is 70kB (together with ELF overhead)
    The 8080 CP/M compiler 58kB (cut up throughout two executables)

  • quick: on my PC it’ll compile itself in 360ms 130ms 80ms.

  • world evaluation: lifeless code elimination and static variable allocation,
    resulting in small and environment friendly binaries

In regards to the compiler

Proper now it’s in a state the place you’ll be able to construct the cross-compiler on a PC, then
use it to compile the compiler on your chosen gadget, and if it’s small
sufficient to suit use that to compile and run actual packages. Realistically you’ll
be cross-compiling on a PC.

The next targets are supported. Including extra is simple.

  • Z80 and 8080, on CP/M.

  • 6502 and 65c02, on the BBC Micro with Tube second processor.

  • 6303, on the 6303 model of Fuzix (if anybody is aware of about FLEX and needs to
    make this work, please get in contact).

  • 6502 interpreted bytecode, on the BBC Micro with Tube second processor;
    this works similar to above, however produces a stack-based bytecode with
    built-in interpreter. It’s a lot smaller, but in addition slower.

  • 80386, on Linux.

  • ARM Thumb2, on Linux.

  • PowerPC, on Linux.

  • 68000, on Atari ST TOS (though a lot of the system calls aren’t attached
    but so you’ll be able to’t do something greater than print stuff) and Linux m68k (if you happen to
    can nonetheless discover a machine which is able to run this).

  • 8086, on DOS (it emits small mode .exe information with 64kB of code and 64kB of knowledge).

  • PDP11, on V7 Unix (because of
    shattered@github for contributing the PDP11
    backend for this).

  • Generic and horrible C. This produces very large and sluggish binaries which
    are used for bootstrapping the compiler if you happen to don’t have a Cowgol

  • Primary. Sure, actually — there’s a backend which is able to transpile into horrible
    Microsoft Primary. I wrote this primarily as a joke and solely a subset of the
    language is supported, however it does work.

As well as, there’s emulator and assembler assist for these platforms, however no compiler:

  • the OBP spaceflight laptop (utilized by, amongst different issues, the OAO-3
    Copernicus orbiting observatory)

(It used to support the Apollo Guidance
used within the
Apollo spacecraft, however I needed to take away the code generator whereas rewriting the
compiler and I haven’t reworked the AGC backend.)

By way of machines you’ll be able to run the compiler on:

  • 80386 and ARM and PowerPC and 68000 Linux, duh.

  • A BBC Micro with Tube second processor. Follow this link for a live

    courtesy of Matt Godbolt’s wonderful JSBeeb emulator: press SHIFT+F12
    (that’s SHIFT+BREAK on a BBC Micro) to start out the compiler, and when it’s
    executed kind OUT to run the consequence. That is producing full 65c02 machine

  • Generic CP/M (each Z80 and 8080).

Lots of the different platforms have unfinished system name libraries, so whereas the
compiler exams all move and the compilers are being constructed, they gained’t work if
you run them. This ought to be simple to repair if essential — let me know and ask.
(I simply haven’t acquired spherical to it but.) Different platforms have working system name
libraries however Cowgol doesn’t present an assembler, so that you must supply your
personal (for instance: MS-DOS and Atari ST TOS). So the compiler technically works
there; you simply can’t do something helpful with it.

In regards to the language

Right here’s a randomly chosen instance pulled from the compiler supply.

# Unlock the node tree rooted within the parameter. That is extra thrilling than it
# ought to be as a result of we do not have recursion.
# Editorial word: truly this subroutine not exists within the compiler
# supply code as a result of I changed it with one thing easier and higher. No
# matter, the instance nonetheless stands.
sub Discard(node: [Node]) is
        var pending := node;
        whereas pending != (0 as [Node]) loop
                node := pending;
                pending := node.dlink;

                # Unlink and push any youngsters.
                if node.left != (0 as [Node]) then
                        node.left.dlink := pending;
                        pending := node.left;
                finish if;
                if node.proper != (0 as [Node]) then
                        node.proper.dlink := pending;
                        pending := node.proper;
                finish if;

                # Now free this node.
                Free(node as [uint8]);
        finish loop;
finish sub;

The bullet record set of options is:

  • strongly typed — no implicit casting (not even between integers of
    totally different widths of signedness)

  • information, pointers and many others

  • subroutines with a number of enter and output arguments

  • arbitrarily nested subroutines, with entry to variables outlined in an
    outer subroutine

  • no recursion and restricted stack use (a lot of the platforms I’m concentrating on
    don’t actually assist stack frames)

  • byte, phrase and quad arithmetic for environment friendly implementation on small

  • easy kind inference of variables in the event that they’re assigned throughout a declaration

  • seperate compilation with world evaluation

There’s extra in regards to the language within the hyperlinks under.

I’ve at all times been desirous about compilers, and have had numerous different
compiler tasks: the Amsterdam Compiler Kit
and Cowbel, to call two. (The
languages part of my web site
incorporates a good variety of entries. The oldest compiler which nonetheless exists
dates from about 1998.)

Cowgol relies on what I’ve learnt from all this. It’s purported to be
helpful, not only a toy. I’m pleasantly shocked by how good the generated
code is; not that it’s something as much as that of, say, gcc, however the principle code
technology binary of gcc is 23552kB, and Cowgol’s is 65kB…

See Also

We have now documentation! Admittedly, not a lot of it.

Why not?

It’s new, it’s buggy, it’s underdeveloped, and thus far just one precise program
is written in Cowgol, which is the Cowgol compiler. (And cowlink and cowwrap.)

Aside from precise bugs, there are some unimplemented components of the language.

  • no null. This one’s semantic, however proper now you must solid 0 to
    pointer sorts to make use of null. (I do learn about languages which don’t have
    null however they’re all for bigger machines than Cowgol’s aimed toward.)

  • no debugging. Nicely… there’s print().

  • no secure normal library. I hack stuff in as I would like it.

Your mileage (or kilometreage, relying) could very. You Have Been Warned.

Cowgol was written principally by me, David Given, with extra contributions
from shattered@github. Be at liberty to contact me
by e mail at You might also like to
visit my website
; there could or will not be one thing
attention-grabbing there.


Cowgol is open supply software program accessible under the 2-clause BSD
. Simplified
abstract: do what you want with it, simply don’t declare you wrote it.

The exceptions are the contents of the third_party listing, which had been
written by different individuals and usually are not coated by this license. This listing as
an entire incorporates GPL software program, which signifies that if you happen to redistribute the complete
listing, you have to conform to the phrases of the GPL.

third_party/lib6502 incorporates a hacked copy of the lib6502 library, which is ©
2005 Ian Plumarta and is accessible beneath the phrases of the MIT license. See
third_party/lib6502/COPYING.lib6502 for the total textual content.

third_party/zmac incorporates a duplicate of the venerable zmac 8080 and Z80
assembler. It’s within the public area.

third_party/lemon incorporates a duplicate of the lemon parser generator. It’s within the
public area.

third_party/apout incorporates a duplicate of the apout PDP-11 SysV binary emulator,
primarily written by Warren Toomey and Eric A. Edwards. It’s distributed beneath
the phrases of the MIT license; see third_party/apout/COPYRIGHT for the total
textual content.

third_party/rc2014emu incorporates a subset of the RC2014 emulator written by
Alan Cox. It’s distributed beneath the phrases of the GPL 3.0 license; see
third_party/rc2014emu/COPYING for the total textual content.

third_party/emu2 constains a duplicate of the emu2 DOS emulator written by
dmsc@github (and others). It’s distributed beneath the phrases of the GPL 2.0
license; see third_party/emu2/LICENSE for the total textual content.

third_party/djlink incorporates a duplicate of the djlink 16-bit linker written by It’s distributed beneath the phrases of the GPL 2.0 licesne; see
third_party/djlink/copying for the total textual content, with extra grants
described in third_party/djlink/

third_party/musashi incorporates a duplicate of the Musashi 68000 emulation library,
written by Karl Stenerud. It’s distributable beneath the phrases of the MIT
license; see third_party/musashi/readme.txt for the total textual content. It additionally in
flip incorporates a duplicate of John R. Hauser’s softfloat library, distributable beneath
a customized however MIT-like license; see third_party/musashi/softfloat/README.txt
for the textual content.

Next page

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top