Landin library reference discuss source

Target scope is enforced by the module assertion below.

compiler.assert(compiler.c_sysv_lp64 or compiler.c_darwin_lp64
                or compiler.c_aapcs64_lp64 or compiler.c_riscv_lp64d
                or compiler.c_aapcs32 or compiler.c_win64_llp64
                or compiler.c_windows_arm64_llp64)

C scalar spellings for the target's C ABI, on every target whose C ABI is enabled: the four LP64 ones and Cortex-M's ILP32 AAPCS32.

LP64 and ILP32 are data models, not architectures:

  • Under LP64, C int is 32 bits while long and pointers are 64, as on every 64-bit Unix.
  • Under Cortex-M's ILP32, all three are 32 bits.
  • Windows' LLP64 keeps long at 32 bits with 64-bit pointers. It is not admitted here.
  • RV32's and Xtensa's ILP32 ABIs are not admitted either: the compiler describes them but does not enable them.

The C ABI, the application binary interface, depends on the operating system as well as the architecture. So the compiler names it with one fact per ABI rather than reading it from compiler.arch or the pointer width:

Target ABI fact Plain char
linux-x86-64, freebsd-x86-64 compiler.c_sysv_lp64: the System V AMD64 ABI i8
darwin-arm64 compiler.c_darwin_lp64: Apple's arm64 variant of AAPCS64 i8
linux-arm64, freebsd-arm64 compiler.c_aapcs64_lp64: the standard AAPCS64 u8
linux-rv64 compiler.c_riscv_lp64d: RISC-V LP64D, LP64 with double-precision float registers u8
cortex-m0, every level compiler.c_aapcs32: AAPCS32, its VFP variant when the device description has a float unit u8
windows-x86-64 compiler.c_win64_llp64: the Windows x64 convention, LLP64 i8
windows-arm64 compiler.c_windows_arm64_llp64: Microsoft's arm64 variant of AAPCS64, LLP64 i8

Darwin and Linux share compiler.arch == arm64 and still disagree on char, which is why the fact names the ABI.

The other aliases differ only in long:

Alias LP64 AAPCS32 Windows' LLP64
c_int i32 i32 i32
c_long i64 i32 i32
c_longlong i64 i64 i64
c_size usize, 64 bits usize, 32 bits usize, 64 bits

The aliases keep ordinary Landin scalar identity; c_char is a numeric byte and never a Unicode scalar.

On Cortex-M a variadic C function is not called; import a fixed-arity C function that calls it instead.

Items

public c_bool: type = bool

Alias for C _Bool under every supported ABI.

c_char

type ↑ index

When compiler.c_aapcs64_lp64 or compiler.c_riscv_lp64d platform/c/c.ldn:28

public c_char: type = u8

The selected ABI's plain C char: unsigned under standard AAPCS64, RISC-V LP64D and AAPCS32, signed under SysV and Darwin LP64. A numeric byte, not a Unicode scalar.

When not (compiler.c_aapcs64_lp64 or compiler.c_riscv_lp64d) platform/c/c.ldn:33

public c_char: type = i8

The selected ABI's plain C char: unsigned under standard AAPCS64, RISC-V LP64D and AAPCS32, signed under SysV and Darwin LP64. A numeric byte, not a Unicode scalar.

public c_double: type = f64

Alias for C double under every supported ABI.

public c_float: type = f32

Alias for C float under every supported ABI.

public c_int: type = i32

Alias for C signed int under every supported ABI.

c_long

type ↑ index

When compiler.c_aapcs32 or compiler.c_win64_llp64 platform/c/c.ldn:51

public c_long: type = i32

Alias for C signed long: 32 bits under AAPCS32's ILP32 and the LLP64 of both Windows ABIs, 64 under the LP64 ABIs.

When not (compiler.c_aapcs32 or compiler.c_win64_llp64) platform/c/c.ldn:58

public c_long: type = i64

Alias for C signed long: 32 bits under AAPCS32's ILP32 and the LLP64 of both Windows ABIs, 64 under the LP64 ABIs.

c_longlong

type platform/c/c.ldn:64 ↑ index
public c_longlong: type = i64

Alias for C signed long long under every supported ABI.

public c_ptrdiff: type = isize

Alias for C ptrdiff_t under every supported ABI: the target's isize, 32 bits under AAPCS32.

public c_schar: type = i8

Alias for C signed char under every supported ABI.

public c_short: type = i16

Alias for C signed short under every supported ABI.

public c_size: type = usize

Alias for C size_t under every supported ABI: the target's usize, 32 bits under AAPCS32.

public c_uchar: type = u8

Alias for C unsigned char under every supported ABI.

public c_uint: type = u32

Alias for C unsigned int under every supported ABI.

c_ulong

type ↑ index

When compiler.c_aapcs32 or compiler.c_win64_llp64 platform/c/c.ldn:54

public c_ulong: type = u32

Alias for C unsigned long: 32 bits under AAPCS32's ILP32 and the LLP64 of both Windows ABIs, 64 under the LP64 ABIs.

When not (compiler.c_aapcs32 or compiler.c_win64_llp64) platform/c/c.ldn:61

public c_ulong: type = u64

Alias for C unsigned long: 32 bits under AAPCS32's ILP32 and the LLP64 of both Windows ABIs, 64 under the LP64 ABIs.

c_ulonglong

type platform/c/c.ldn:66 ↑ index
public c_ulonglong: type = u64

Alias for C unsigned long long under every supported ABI.

public c_ushort: type = u16

Alias for C unsigned short under every supported ABI.

Example program

This complete program is one of the compiler's runtime tests. View source.

The program, 66 lines
compiler/tests/fixtures/runtime/r440-c-aliases/main.ldn66 lines
import platform/c

public main: () -> (code: i32) =
    --  c_char is deliberately a signed numeric byte, not Unicode u32.
    character: c.c_char = -1
    signed_character: c.c_schar = -2
    unsigned_character: c.c_uchar = 255
    short_value: c.c_short = -123
    unsigned_short: c.c_ushort = 456
    integer: c.c_int = -789
    unsigned_integer: c.c_uint = 987
    long_value: c.c_long = c.c_long(integer)
    unsigned_long: c.c_ulong = c.c_ulong(unsigned_integer)
    long_long: c.c_longlong = c.c_longlong(long_value)
    unsigned_long_long: c.c_ulonglong = c.c_ulonglong(unsigned_long)
    size: c.c_size = c.c_size(24)
    difference: c.c_ptrdiff = c.c_ptrdiff(-24)
    single: c.c_float = c.c_float(3)
    double: c.c_double = c.c_double(single)
    truth: c.c_bool = c.c_bool(1)

    --  Alias identity permits these ordinary assignments without adapters.
    ordinary_character: i8 = character
    ordinary_short: i16 = short_value
    ordinary_integer: i32 = integer
    ordinary_long: i64 = long_value
    ordinary_unsigned_character: u8 = unsigned_character
    ordinary_unsigned_short: u16 = unsigned_short
    ordinary_unsigned_integer: u32 = unsigned_integer
    ordinary_unsigned_long: u64 = unsigned_long
    ordinary_size: usize = size
    ordinary_difference: isize = difference
    ordinary_single: f32 = single
    ordinary_double: f64 = double
    ordinary_truth: bool = truth

    if sizeof c.c_char == 1 and alignof c.c_char == 1
      and sizeof c.c_schar == 1 and alignof c.c_schar == 1
      and sizeof c.c_uchar == 1 and alignof c.c_uchar == 1
      and sizeof c.c_short == 2 and alignof c.c_short == 2
      and sizeof c.c_ushort == 2 and alignof c.c_ushort == 2
      and sizeof c.c_int == 4 and alignof c.c_int == 4
      and sizeof c.c_uint == 4 and alignof c.c_uint == 4
      and sizeof c.c_long == 8 and alignof c.c_long == 8
      and sizeof c.c_ulong == 8 and alignof c.c_ulong == 8
      and sizeof c.c_longlong == 8 and alignof c.c_longlong == 8
      and sizeof c.c_ulonglong == 8 and alignof c.c_ulonglong == 8
      and sizeof c.c_size == 8 and alignof c.c_size == 8
      and sizeof c.c_ptrdiff == 8 and alignof c.c_ptrdiff == 8
      and sizeof c.c_float == 4 and alignof c.c_float == 4
      and sizeof c.c_double == 8 and alignof c.c_double == 8
      and sizeof c.c_bool == 1 and alignof c.c_bool == 1
      and ordinary_character == -1 and ordinary_short == -123
      and ordinary_integer == -789 and ordinary_long == -789
      and ordinary_unsigned_character == 255 and ordinary_unsigned_short == 456
      and ordinary_unsigned_integer == 987 and ordinary_unsigned_long == 987
      and long_long == -789 and unsigned_long_long == 987
      and ordinary_size == 24 and ordinary_difference == -24
      and ordinary_single == 3.0 and ordinary_double == 3.0
      and ordinary_truth
    then
        code = 42
    else
        code = 1
    end if
end main