Skip to content
CShift0.45

The Amiga: the m68k backend

CShift programs run on the Amiga (68000 and up, AmigaOS 1.3 and later). For them the compiler has its own backend: it turns the IR into 68000 code, assembles it and writes an AmigaOS executable itself. LLVM, clang and an Amiga cross toolchain are not needed (libclang only for importing C headers of the NDK).

cshiftc --target m68k-amigaos hello.cshift -o hello # an AmigaOS executable (hunk format)
{
"name": "demo",
"sources": ["src"],
"target": "m68k-amigaos",
"ndk": "../NDK3.2"
}

Three demos show what is possible: demo-amiga-hw (copper raster bars on the custom chips, 50 frames per second on an A500), demo-amiga-gfx (bouncing balls with the blitter, double buffering, a sprite and text) and demo-amiga-ndk (a rotating cube in an Intuition window, written against the NDK 3.2).

Option cshift.json Meaning
--backend llvm "backend": "llvm" LLVM IR, compiled and linked by clang (the default for all targets but AmigaOS)
--backend m68k "backend": "m68k" CShift’s own 68000 code generator (the default for m68k-amigaos)
--ndk <dir> "ndk": "<dir>" the AmigaOS NDK (also the environment variable CSHIFT_NDK); a path in cshift.json is relative to it
--emit-asm – write the assembly (.s, GNU syntax) instead of an executable; a comment before every function names it (| function: Name(params))
-g "debug": true write the names of the functions and globals into the executable (HUNK_SYMBOL), for debuggers and the profiler; the code stays the same
-O0 … -O3 "optimize": 0 … 3 -O2 (the default) and -O3 inline larger functions in loops: faster, but bigger; -O1 only inlines functions that are not larger than their call (smaller programs, e.g. for floppy disks); -O0 inlines nothing

The m68k backend accepts m68k targets only:

  • m68k-amigaos: an AmigaOS executable (hunk format), with its own startup code and C library.
  • m68k-linux-gnu: a program for m68k Linux, linked statically by a cross gcc (m68k-linux-gnu-gcc, or --cc), or an ELF object (-c). It runs under qemu-m68k. This is how the backend is tested: the test suite runs with it (CSHIFT_TARGET=m68k-linux-gnu CSHIFT_BACKEND=m68k tests/run_tests.sh, see Testing Amiga programs).

The whole language and the standard library work, with these differences:

  • No threads. Thread, Mutex<T> and SharedPtr<T> need pthreads, which AmigaOS does not have. A program that uses them does not link: the compiler names the missing functions.
  • Floating point in software. double and float are computed by stdlib/m68k/softfloat.cshift (IEEE 754, exact rounding); Math (Sqrt, Sin, Exp, Pow, …) by stdlib/m68k/math.cshift and mathtrans.cshift (within 2 ULP of glibc). This works but is slow on a 68000: for graphics use integers, fixed point and FastTrig (sine and cosine from tables).
  • The C library (stdlib/amiga/libc.cshift) is written on exec.library and dos.library: memory, printf, files, directories, Process.GetEnv, time, Process.Run. Functions that need a newer dos.library (environment variables, the current directory, the exit code of a command) check its version.
  • Only what is used. An executable contains the functions of the program, the standard library and the C library that it calls, nothing else: the startup code and the runtime are taken in pieces as well. printf’s formatting (with the code for double) is only in a program that calls printf or formats floating point numbers; Console.WriteLine and the integer ToString do not need it.
  • The stack. A program gets its own stack of 256 KB at startup (the stack of a CLI program is often only 4 KB).
  • int is 32 bits, pointers are 32 bits (nint, sizeof, lengths). 64-bit integers work, in software.

The NDK describes every library in an SFD file: the offset of each function, the registers of its arguments, its C prototype. CShift imports it like a C header:

using Gfx from "graphics_lib.sfd";
using Intui from "intuition_lib.sfd";
using I from "intuition/intuition.h"; // structures, flags and tags come from the C headers
void* window = Intui.OpenWindowTags(null, I.WA_Width, 320, I.WA_Height, 200, I.TAG_DONE);
Gfx.SetAPen(rastPort, 1);
Gfx.Move(rastPort, 10, 10);
Gfx.Draw(rastPort, 100, 50);
  • The SFD file is looked up next to the importing file, then in <ndk>/SFD.
  • A call puts the arguments in the registers the SFD names and calls the library through its base in a6 (jsr -offset(a6)). For the varargs versions (OpenWindowTags, …) the arguments after the fixed ones stay on the stack as a tag list, and the last register gets its address.
  • The libraries that are used are opened when the program starts (with version 0) and closed at its end; the program ends with a message if one cannot be opened. exec.library and dos.library are always there.
  • Types: the integer typedefs of exec/types.h (LONG, UWORD, BOOL, …) become their CShift types, pointers become void*, CONST_STRPTR becomes a string parameter (passed as a C string).
  • C headers of the NDK: for amigaos targets <ndk>/Include_H is searched, and structs are laid out as on the Amiga (packed to 2 bytes).

System.Ui opens an Intuition window on the Workbench screen and draws its widgets with graphics.library, from drawing commands instead of pixels (AmigaOS 3.0 and later, see ui.md). The same program runs on Windows, Linux and in the browser.

For demos and games that bypass the operating system (stdlib/amiga/hardware.cshift, only for amigaos targets):

using Amiga;
unsafe
{
uint16* list = (uint16*)Hardware.AllocChip(1024); // chip memory, cleared (null if there is none left)
... // write a copper list
Hardware.TakeOver(); // the OS stops drawing; DMA and interrupts are ours
Hardware.StartCopper(list);
while (!Hardware.LeftMouseButton())
{
Hardware.WaitVBlank(); // until the beam is below the picture (line 300)
Hardware.Write(Custom.COLOR00, 0x0F00);
}
Hardware.Restore(); // the OS display, DMA and interrupts are back
Hardware.FreeChip(list, 1024);
}
Function
TakeOver() / Restore() take the machine from the OS (Forbid, the view, DMA, interrupts, the blitter) and give it back
StartCopper(list) run a copper list (in chip memory)
WaitVBlank(), RasterLine() wait for the vertical blank; the current line of the beam
Write(reg, value), Read(reg), WriteLong(reg, address), Register(reg) the custom chip registers (Custom.COLOR00, DMACON, BPL1PTH, …), with volatile accesses
LeftMouseButton() the left button (port 1)
AllocChip(size) / FreeChip(memory, size) chip memory for copper lists, bitplanes, sprites and sounds

Graphics: Amiga.Screen, Bitmap, the blitter and sprites

Section titled “Graphics: Amiga.Screen, Bitmap, the blitter and sprites”

On top of Amiga.Hardware, stdlib/amiga/graphics.cshift has what a game or a demo draws with (low resolution PAL, OCS/ECS, every Amiga from the A500 on). demo-amiga-gfx shows all of it.

using Amiga;
Hardware.TakeOver();
if (Screen.Open(320, 256, 4, true) is Screen screen) // 16 colors, double buffering
{
screen.SetColor(1, 0xFFF);
screen.Copper.Wait(100); // own copper instructions after the screen's setup
screen.Copper.Color(0, 0x00F); // from line 100 on the background is blue
screen.Show();
while (!Hardware.LeftMouseButton())
{
Bitmap b = screen.Back;
b.Clear();
b.FillRect(10, 10, 100, 50, 2);
b.DrawLine(0, 0, 319, 255, 3);
b.DrawText(20, 100, "Hello, Amiga", 1);
screen.Swap(); // Back is shown from the next frame on
}
Hardware.Restore();
screen.Close();
}
Type
Screen Open(width, height, depth, doubleBuffer) (width 128..320, a multiple of 16; height up to 256; 2..32 colors), Front/Back (the bitmaps), SetColor(index, 0xRGB), Show(), Swap() (shows Back, waits for the vertical blank), ShowSprite(n, sprite)/HideSprite(n), Copper (own instructions; Copper.Reset() removes them), Close()
Bitmap Create(width, height, depth) (chip memory, Free()), Width, Height, Depth, BytesPerRow, Plane(p); with the blitter: Clear(), FillRect, DrawRect, Copy(source, sx, sy, x, y, w, h), DrawMasked(source, mask, sx, sy, x, y, w, h) (a “bob”: only where the mask has a 1; MakeMask() makes it from the colors that are not 0); with the CPU: SetPixel, GetPixel, DrawLine, DrawPattern(x, y, rows) (pixels from strings), DrawText(x, y, text, color)
Sprite a hardware sprite, 16 pixels wide, 3 colors: Create(rows) ('1'..'3', others transparent), MoveTo(x, y), Free()
SystemFont the font of DrawText (Topaz 8 from the ROM, or the one of the preferences): Height(), TextWidth(text)
CopperList a copper list of its own: Create(n), Move(register, value), Color(index, rgb), MovePointer, Wait(line), Change(index, value), Mark()/Reset(), Address() for Hardware.StartCopper
Blitter Wait(): until the blitter is done, before the CPU touches what it draws (the CPU functions of Bitmap wait themselves)
  • The blitter works in parallel with the CPU: a blitter function of Bitmap starts the blit and returns; the next one waits for it. It needs Hardware.TakeOver() (the program owns the blitter then).
  • Drawing is clipped to the bitmap. Copy uses the blitter when x % 16 >= sx % 16 (e.g. the same positions in a background bitmap, or sources at multiples of 16), otherwise the CPU; DrawMasked needs sx % 16 == 0 and uses the CPU when the shape is not completely inside from left to right.
  • Coordinates of sprites and copper waits count from the top left corner of the picture (the standard PAL display window: raster line 44, horizontal position 0x81). Sprites 0/1 use the colors 17-19, 2/3 21-23, 4/5 25-27, 6/7 29-31.
  • Bitmaps are planar: one bitplane after the other (Plane(p)), 1 bit per pixel each.

The sources are in selfhost/src/M68k; the compiler writes its IR as always, and the backend reads it back:

IR (text) ─▶ IrReader ─▶ Prepare (inlining, folding, facts, loops) ─▶ Regalloc ─▶ Gen (68000 assembly) ─▶ Peephole ─▶ Asm ─▶ Hunk / Elf
  • IrReader.cshift reads the LLVM IR the front end writes (types, constants, functions, instructions).
  • Prepare.cshift, Inline.cshift: small functions are inlined: up to 6 instructions everywhere; with -O2/-O3 in loops also those whose code that runs is small (what costs nothing once inlined is not counted: the panics of failed checks, variables, addresses), the deepest loops first, while the function grows by at most 240 instructions. Variables written once become their value (where the store dominates every load); the length of an array or string is read once per value (a check that an earlier one dominates uses its length; in a loop, the length of an array from outside of it is read before the loop); constants are folded; && and || in a condition become branches (no bool is made) and a branch on !c branches on c with its targets swapped; dead code is removed; a pointer that is only used by loads and stores (one, or the load and the store of a[i] ^= x) becomes an addressing mode ((d16,An), (d8,An,Dn.l), with the register of a 32-bit index as it is). Structs: a load of a struct (a slice: pointer, data, length) that only field reads use loads just those fields, a struct variable that is written once (a slice or struct parameter) is its value, and the field of a struct value is read once, right after the value, so data[at] in a loop over a ReadOnlySlice<uint8> reads the data pointer and the length before the loop instead of copying the slice into the frame for every byte.
  • Facts.cshift: what the IR knows at a place. A load of a variable that was stored or loaded before on every way to it (no store on the ways from there) is that value; the same computation of the same values twice is computed once; a block whose only predecessor branched on a comparison knows its outcome (so does every block it dominates), so the same comparison there is a constant: the bounds check of data[i] ^= x is made once. Checked additions and subtractions that cannot overflow become plain ones and lose their panics: i + 1 where i < n or i < length is known, length - 1, and operations whose operands have small ranges ((d << 4) + d + 87 with d = x & 0xffff). A loop counter that only gets values that are never negative (var i = 0, i += 1 that cannot overflow) is never negative, so where i < a.Length holds the bounds check of a[i] goes away. Facts are combined through other values (to < stop and stop <= d.Length give to < d.Length), the side of a branch on c1 || c2 (or &&) where the way came through c2 knows both outcomes, and two variables that a loop steps by the same amount keep their difference: after from = to - offset (offset >= 0) and if (from < 0 || stop > d.Length) ..., the loop while (to < stop) { d[to] = d[from]; to += 1; from += 1; } needs neither bounds check. A sum that does not wrap is above its base (i + 1 > i), and a variable that only grows in a loop keeps the lower bound it had when the loop was entered (at >= 0 from a check before the loop, for every data[at] in it). The joins of && and || are left out before the facts are gathered, so the blocks of c1 && c2 follow the block of c2 and know it. A bounds check that passed tells i >= 0 and i < n, a < x tells a + 1 <= x, and n - 1 < n: after while (i < data.Length - 1) and the check of data[i], data[i + 1] needs none. Where such an index starts at a value that may be negative (a parameter), the block before the loop asks whether it is (GuardLowerBounds): then a copy of the loop’s test and of the bounds check runs (and panics as the check would), else the loop is entered with the index >= 0 and its check needs only the upper bound. x & 0xffff (or & 0xff) of plain additions, subtractions, and, or, xor and shifts left by a constant is computed in 16 (8) bits and zero-extended (NarrowMasks): the low bits of these depend only on the low bits of their operands. Branches on constants go to their target, unreachable blocks are removed, and a block that only one block branches to is joined to it. A trunc is no instruction (an 8- or 16-bit value is the low bits of its register), and a value that only one later instruction of its block uses is computed right before it (the first operand last), so that it goes through d0.
  • Loops.cshift: counted loops, for (var i = start; i < n; i += 1) with n from before the loop. An element a[i] of an array that does not change in the loop gets an address register of its own: set to the element of the first round before the loop, moved on by the element size at the end of each round, so row[i] = color is move.w d4,(a4)+ instead of a copy and a shift of i and move.w d4,(8,a2,d1.l); so does an element whose index is another variable that steps with i (d[from] above), and when nothing else reads that variable in the loop and it is written before it is read after the loop, it is not stepped at all. The test is repeated at the end of the round, which then ends in one taken branch back; so is the test of another loop whose header only loads, compares and branches (for (var bit = 0x80; bit != 0; bit >>= 1): asr.l #1,d3 / bne). After the rotation the test before the loop is reached from the block before it only, so a start stored there (var n = 0) is the value it tests and counts from: n < 8 is known, the count is the constant 7. Where the bound has a known range ((x & 15) + 3: 3 to 18), the test is known when it holds for all of its values, and a count below 65536 is a 16-bit count, so the round ends in a plain dbra. When i only counts the rounds (nothing else in the loop reads it, and after the loop either nothing or, when the loop ends only by its test, its value n stored on the way out), a count of the rounds left after the current one replaces it, down to -1: dbra d7,loop (dbra counts the low word; when that ends, a swap and a sub.w count the high word, so any count works). With -O2/-O3 a body of one to three instructions without branches (after the steps above, so with the bounds checks gone) becomes a jump tower (Peephole.cshift): it is copied 16 times (8 times for two or three instructions), and a count of at least that many enters the copies through a table, so that the first pass does the rounds left over and every further pass 16 for one dbra; a smaller count runs the plain loop. Filling a uint16[] takes about 9 cycles per element (one move.w d4,(a4)+ and a sixteenth of a dbra) instead of about 75 with 0.38; a program that clears a 10 KB bitplane and fills 320-pixel rows ran in 4.9 instead of 51 million cycles. A register allocation detail matters here: an integer that would get an address register (no dbra, no byte operations) takes the data register of a value that is worth less, which moves to the address register instead.
  • Regalloc.cshift: a linear scan over live intervals (with holes: a value holds its register from its first to its last point in each block where it lives, not in the blocks in between); values, variables and parameters get d4-d7 and a2-a5 (and d2/d3 in a function whose code needs neither: no 64-bit arithmetic, no product but by a constant of 16 bits, no division) by their uses, weighted by the depth of the loops they are in (a block that leaves a loop, a panic or a return, is not in it); a pointer that got a data register and an integer that got an address register over the same time swap them (with the values of both registers over that time; an integer and a pointer too, where the pointer is used more: move.b (a5)+,(a3)+ in a loop through pointers); a value that got no register gets one where the other values of that register are only in blocks the value lives through without being used (it is written to its slot where it is computed and loaded again on the ways out of those blocks), where its uses are worth more than the loads (a use in a block that every round of its loop passes counts twice); a comparison or an overflow flag that only decides the branch after it is a flag, not a register; a value loaded from a variable shares its register (also when the variable is written after the value’s last use, in a block where it ends); a value that only the next instruction uses, as its first operand, stays in d0 and needs neither a register nor its slot.
  • Gen.cshift writes the code: 16-bit fast paths for multiplication and division (muls.w, divs.w), overflow checks fused with their branch (bvs; the code of the panics at the end of the function), absolute addresses for constant pointers (custom chip registers), division by constants (shifts for powers of two, none by 1), 8- and 16-bit operations of their size (add.w, lsl.w), p - x as a sub.l from the pointer, asl for checked products by powers of two, products by other constants as three mulu.w of the 16-bit halves (no call), variables in their slots as frame operands ((-8,a6)), the length of a string or array for its bounds check without a call, only the registers that are used are saved.
  • Peephole.cshift simplifies the assembly (for example a register copied back right after it was copied, moveq #0 before loading a byte or word instead of masking it afterwards, move.b (a3)+,(a4)+ instead of a move through d0 where no way afterwards reads d0, eor.b d2,(8,a2,d4.l) instead of a load, the operation and a store, add.l #2,d4 instead of a copy, the add and a copy back, btst #7,d7 instead of a copy, an and.l #128 and the branch on it, cmp.l a2,a5 instead of a copy of the pointer into a data register, (a3) instead of (0,a3), moveq #0,d6 / move.b (a5),d6 instead of a load into d0 and a copy); a register that is only tested by btst #7 (with a beq/bne after it) and shifted by lsl.l #1 (each test followed by a shift on every way, each shift by a test) is tested by add.b / bcc instead and the shifts go (FuseBitShifts: only bit 7 of the low byte is read, and no way reads the X flag before it is set again); with -O2/-O3 a bra to the end of a loop’s round (its last few instructions with the dbra) is a copy of that end; the address of an access by i + c is (c + 8,a2,d4.l) with i in d4 (Prepare.cshift); Asm.cshift encodes it (68000 only, branches made short where they fit); Hunk.cshift writes the AmigaOS executable, Elf.cshift an ELF object.
  • AmigaRuntime.cshift, Runtime.cshift: the startup code, the library stubs, printf, memcpy & co, and the helpers for 32/64-bit multiplication and division. memcpy, memmove and memset go through jump towers: the move is unrolled 16 times and the loop jumps into the middle of it, so that the first pass does the remainder and every further pass 16 moves for one subq/bcc - longs where the addresses allow it (both even, or both odd after one byte), bytes otherwise. A copy of 64 KB takes about 6 cycles per byte instead of 44, a fill 3.4 instead of 38. Chunks.cshift cuts them into pieces at their labels; only the pieces that the program reaches are written.

The calling convention is the one of GCC for m68k: arguments on the stack, results in d0 (and d1), a0 as well for pointers; d2-d7 and a2-a6 are kept.

  • vamos (amitools) runs AmigaOS command-line programs on the PC without an Amiga ROM: good for everything but the hardware. vamos -v also prints the number of CPU cycles, a measure of speed. (pip install amitools "machine68k<0.4": amitools 0.8 does not run with machine68k 0.4.) When vamos is installed, tests/run_tests.sh runs tests/amiga/memory.cshift with it.
  • FS-UAE (or WinUAE) with an A500 configuration and a Kickstart ROM (or the free AROS ROM) for graphics and the custom chips.
  • The test suite with the backend: CSHIFT_TARGET=m68k-linux-gnu CSHIFT_BACKEND=m68k CSHIFT_SKIP_SELFHOST=1 tests/run_tests.sh compiles the programs of the tests with the 68000 backend for m68k Linux and runs them under qemu-m68k (Debian/Ubuntu: qemu-user gcc-m68k-linux-gnu libc6-dev-m68k-cross); the CI does that too. The cases with threads are left out (// skip-target: m68k): the backend has no atomic operations, AmigaOS has no threads.

Where the time goes: tools/amiga/profile.py

Section titled “Where the time goes: tools/amiga/profile.py”

tools/amiga/profile.py runs a program under vamos with the instruction trace and counts the executed instructions of every function. Build the program with -g (the names of the functions):

cshiftc build AmbermoonPack --target m68k-amigaos -g -o AmbermoonPack
python3 tools/amiga/profile.py AmbermoonPack UNPACK Floors.amb out # (--ram: the memory in KB, 8192 by default)
python3 tools/amiga/profile.py --annotate Lob.Decompress AmbermoonPack UNPACK Floors.amb out
4216290 instructions, 41572088 cycles (5.86 s on an A500)
instructions share entries function
1457505 34.6% 14 Ambermoon.Data.Legacy.Compression.Lob.Decompress(ref ...DataReader,uint32)
1453547 34.5% 17 Ambermoon.Data.Legacy.Compression.JH.Crypt(uint8[],uint16,int32)
731387 17.3% 17009 Ambermoon.Data.Legacy.Serialization.DataReader.ReadByte()

entries counts how often the function was entered (its calls). --annotate NAME lists the instructions of the functions whose name contains NAME with how often each ran: the loops that matter, in the code the backend wrote. The trace is slow (about 30,000 instructions per second); vamos -v alone gives the cycles quickly.