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).
Choosing the backend
Section titled “Choosing the backend”| 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 crossgcc(m68k-linux-gnu-gcc, or--cc), or an ELF object (-c). It runs underqemu-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).
What a program can use
Section titled “What a program can use”The whole language and the standard library work, with these differences:
- No threads.
Thread,Mutex<T>andSharedPtr<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.
doubleandfloatare computed bystdlib/m68k/softfloat.cshift(IEEE 754, exact rounding);Math(Sqrt,Sin,Exp,Pow, …) bystdlib/m68k/math.cshiftandmathtrans.cshift(within 2 ULP of glibc). This works but is slow on a 68000: for graphics use integers, fixed point andFastTrig(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 fordouble) is only in a program that callsprintfor formats floating point numbers;Console.WriteLineand the integerToStringdo 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).
intis 32 bits, pointers are 32 bits (nint,sizeof, lengths). 64-bit integers work, in software.
AmigaOS libraries from SFD files
Section titled “AmigaOS libraries from SFD files”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 becomevoid*,CONST_STRPTRbecomes a string parameter (passed as a C string). - C headers of the NDK: for amigaos targets
<ndk>/Include_His searched, and structs are laid out as on the Amiga (packed to 2 bytes).
Windows with buttons: System.Ui
Section titled “Windows with buttons: System.Ui”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.
The custom chips: Amiga.Hardware
Section titled “The custom chips: Amiga.Hardware”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
Bitmapstarts the blit and returns; the next one waits for it. It needsHardware.TakeOver()(the program owns the blitter then). - Drawing is clipped to the bitmap.
Copyuses the blitter whenx % 16 >= sx % 16(e.g. the same positions in a background bitmap, or sources at multiples of 16), otherwise the CPU;DrawMaskedneedssx % 16 == 0and 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.
How the backend works
Section titled “How the backend works”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/-O3in 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 (noboolis made) and a branch on!cbranches oncwith its targets swapped; dead code is removed; a pointer that is only used by loads and stores (one, or the load and the store ofa[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, sodata[at]in a loop over aReadOnlySlice<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] ^= xis made once. Checked additions and subtractions that cannot overflow become plain ones and lose their panics:i + 1wherei < nori < lengthis known,length - 1, and operations whose operands have small ranges ((d << 4) + d + 87withd = x & 0xffff). A loop counter that only gets values that are never negative (var i = 0,i += 1that cannot overflow) is never negative, so wherei < a.Lengthholds the bounds check ofa[i]goes away. Facts are combined through other values (to < stopandstop <= d.Lengthgiveto < d.Length), the side of a branch onc1 || c2(or&&) where the way came throughc2knows both outcomes, and two variables that a loop steps by the same amount keep their difference: afterfrom = to - offset(offset >= 0) andif (from < 0 || stop > d.Length) ..., the loopwhile (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 >= 0from a check before the loop, for everydata[at]in it). The joins of&&and||are left out before the facts are gathered, so the blocks ofc1 && c2follow the block ofc2and know it. A bounds check that passed tellsi >= 0andi < n,a < xtellsa + 1 <= x, andn - 1 < n: afterwhile (i < data.Length - 1)and the check ofdata[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. Atruncis 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 throughd0. - Loops.cshift: counted loops,
for (var i = start; i < n; i += 1)withnfrom before the loop. An elementa[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, sorow[i] = colorismove.w d4,(a4)+instead of a copy and a shift ofiandmove.w d4,(8,a2,d1.l); so does an element whose index is another variable that steps withi(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 < 8is 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 plaindbra. Whenionly 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 valuenstored 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, aswapand asub.wcount the high word, so any count works). With-O2/-O3a 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 onedbra; a smaller count runs the plain loop. Filling auint16[]takes about 9 cycles per element (onemove.w d4,(a4)+and a sixteenth of adbra) 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 (nodbra, 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-d7anda2-a5(andd2/d3in 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 ind0and 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 - xas asub.lfrom the pointer,aslfor checked products by powers of two, products by other constants as threemulu.wof 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 #0before loading a byte or word instead of masking it afterwards,move.b (a3)+,(a4)+instead of a move throughd0where no way afterwards readsd0,eor.b d2,(8,a2,d4.l)instead of a load, the operation and a store,add.l #2,d4instead of a copy, the add and a copy back,btst #7,d7instead of a copy, anand.l #128and the branch on it,cmp.l a2,a5instead of a copy of the pointer into a data register,(a3)instead of(0,a3),moveq #0,d6/move.b (a5),d6instead of a load intod0and a copy); a register that is only tested bybtst #7(with a beq/bne after it) and shifted bylsl.l #1(each test followed by a shift on every way, each shift by a test) is tested byadd.b/bccinstead 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/-O3abrato the end of a loop’s round (its last few instructions with thedbra) is a copy of that end; the address of an access byi + cis(c + 8,a2,d4.l)withiind4(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,memmoveandmemsetgo 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 onesubq/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.
Testing Amiga programs
Section titled “Testing Amiga programs”- vamos (amitools) runs AmigaOS command-line programs on the PC without an
Amiga ROM: good for everything but the hardware.
vamos -valso 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.) Whenvamosis installed,tests/run_tests.shruns 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.shcompiles the programs of the tests with the 68000 backend for m68k Linux and runs them underqemu-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 AmbermoonPackpython3 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 out4216290 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.