Z / libzlang 0.29.0
C99 runtime library and the Zlang 0.29 source-to-source compiler
Loading...
Searching...
No Matches
runtime.h File Reference

Runtime primitives called by C code emitted from Zlang. More...

#include "zlang/detail/inline.h"
#include "zlang/runtime/context.h"
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  zlang_arguments_t
struct  zlang_string_t

Macros

#define ZLANG_TRAP_OVERFLOW   1U
#define ZLANG_TRAP_BOUNDS   2U
#define ZLANG_TRAP_CAPACITY   3U
#define ZLANG_TRAP_INVALID_ARGUMENT   4U
#define ZLANG_TRAP_DIVISION_BY_ZERO   5U
#define ZLANG_TRAP_SINK(code)

Typedefs

typedef struct zlang_arguments_t zlang_arguments_t
typedef struct zlang_string_t zlang_string_t

Functions

ZLANG_STATIC_INLINE bool zlang_write (FILE *stream, zlang_string_t value)
 Write the exact declared byte count of a string to a stream.
ZLANG_STATIC_INLINE bool zlang_print (zlang_string_t value)
 Write a string view to standard output.
ZLANG_STATIC_INLINE void zlang_trap (uint8_t code)
 Terminate execution with a fixed trap code.
ZLANG_STATIC_INLINE int32_t zlang_add_i32 (int32_t left, int32_t right)
 Add two signed integers, trapping on overflow.
ZLANG_STATIC_INLINE uint32_t zlang_add_u32 (uint32_t left, uint32_t right)
 Add two unsigned integers, trapping on overflow.
ZLANG_STATIC_INLINE bool zlang_print_i32 (int32_t value)
 Print a signed integer using fixed stack storage.
ZLANG_STATIC_INLINE bool zlang_print_u32 (uint32_t value)
 Print an unsigned integer using fixed stack storage.
ZLANG_STATIC_INLINE bool zlang_print_bool (bool value)
 Print a boolean as true or false.
ZLANG_STATIC_INLINE int32_t zlang_neg_i32 (int32_t value)
 Negate a signed 32-bit integer, trapping on overflow.
ZLANG_STATIC_INLINE int64_t zlang_neg_i64 (int64_t value)
 Negate a signed 64-bit integer, trapping on overflow.
ZLANG_STATIC_INLINE int32_t zlang_sub_i32 (int32_t left, int32_t right)
 Subtract two signed integers, trapping on overflow.
ZLANG_STATIC_INLINE uint32_t zlang_sub_u32 (uint32_t left, uint32_t right)
 Subtract two unsigned integers, trapping on overflow.
ZLANG_STATIC_INLINE int32_t zlang_mul_i32 (int32_t left, int32_t right)
 Multiply two signed integers, trapping on overflow.
ZLANG_STATIC_INLINE uint32_t zlang_mul_u32 (uint32_t left, uint32_t right)
 Multiply two unsigned integers, trapping on overflow.
ZLANG_STATIC_INLINE int32_t zlang_div_i32 (int32_t left, int32_t right)
 Divide two signed integers, trapping on zero and overflow.
ZLANG_STATIC_INLINE uint32_t zlang_div_u32 (uint32_t left, uint32_t right)
 Divide two unsigned integers, trapping on zero.
ZLANG_STATIC_INLINE int32_t zlang_mod_i32 (int32_t left, int32_t right)
 Remainder of two signed integers, trapping on zero and overflow.
ZLANG_STATIC_INLINE uint32_t zlang_mod_u32 (uint32_t left, uint32_t right)
 Remainder of two unsigned integers, trapping on zero.
ZLANG_STATIC_INLINE bool zlang_print_i64 (int64_t value)
 Print a signed 64-bit integer using fixed stack storage.
ZLANG_STATIC_INLINE bool zlang_print_u64 (uint64_t value)
 Print an unsigned 64-bit integer using fixed stack storage.
ZLANG_STATIC_INLINE bool zlang_print_f32 (float value)
 Print a 32-bit float with a deterministic decimal form.
ZLANG_STATIC_INLINE bool zlang_print_f64 (double value)
 Print a 64-bit double with a deterministic decimal form.
ZLANG_STATIC_INLINE int64_t zlang_add_i64 (int64_t left, int64_t right)
 Add two signed 64-bit integers, trapping on overflow.
ZLANG_STATIC_INLINE uint64_t zlang_add_u64 (uint64_t left, uint64_t right)
 Add two unsigned 64-bit integers, trapping on overflow.
ZLANG_STATIC_INLINE int64_t zlang_sub_i64 (int64_t left, int64_t right)
 Subtract two signed 64-bit integers, trapping on overflow.
ZLANG_STATIC_INLINE uint64_t zlang_sub_u64 (uint64_t left, uint64_t right)
 Subtract two unsigned 64-bit integers, trapping on overflow.
ZLANG_STATIC_INLINE int64_t zlang_mul_i64 (int64_t left, int64_t right)
 Multiply two signed 64-bit integers, trapping on overflow.
ZLANG_STATIC_INLINE uint64_t zlang_mul_u64 (uint64_t left, uint64_t right)
 Multiply two unsigned 64-bit integers, trapping on overflow.
ZLANG_STATIC_INLINE int64_t zlang_div_i64 (int64_t left, int64_t right)
 Divide two signed 64-bit integers, trapping on zero and overflow.
ZLANG_STATIC_INLINE uint64_t zlang_div_u64 (uint64_t left, uint64_t right)
 Divide two unsigned 64-bit integers, trapping on zero.
ZLANG_STATIC_INLINE int64_t zlang_mod_i64 (int64_t left, int64_t right)
 Remainder of two signed 64-bit integers.
ZLANG_STATIC_INLINE uint64_t zlang_mod_u64 (uint64_t left, uint64_t right)
 Remainder of two unsigned 64-bit integers.
ZLANG_STATIC_INLINE int32_t zlang_to_i32_i64 (int64_t value)
 Convert a signed 64-bit value to int32_t, trapping on overflow.
ZLANG_STATIC_INLINE int32_t zlang_to_i32_u32 (uint32_t value)
 Convert an unsigned 32-bit value to int32_t, trapping on overflow.
ZLANG_STATIC_INLINE int32_t zlang_to_i32_f64 (double value)
 Convert a floating value to int32_t, trapping on invalid input.
ZLANG_STATIC_INLINE uint32_t zlang_to_u32_i64 (int64_t value)
 Convert a signed 64-bit value to uint32_t, trapping on overflow.
ZLANG_STATIC_INLINE uint32_t zlang_to_u32_u32 (uint32_t value)
 Convert an unsigned 32-bit value to uint32_t.
ZLANG_STATIC_INLINE uint32_t zlang_to_u32_f64 (double value)
 Convert a floating value to uint32_t, trapping on invalid input.
ZLANG_STATIC_INLINE int32_t zlang_to_i32_u64 (uint64_t value)
 Convert an unsigned 64-bit value to int32_t, trapping on overflow.
ZLANG_STATIC_INLINE uint32_t zlang_to_u32_u64 (uint64_t value)
 Convert an unsigned 64-bit value to uint32_t, trapping on overflow.
ZLANG_STATIC_INLINE int64_t zlang_to_i64_u64 (uint64_t value)
 Convert an unsigned 64-bit value to int64_t, trapping on overflow.
ZLANG_STATIC_INLINE uint64_t zlang_to_u64_i64 (int64_t value)
 Convert a signed 64-bit value to uint64_t, trapping on overflow.
ZLANG_STATIC_INLINE uint64_t zlang_to_u64_u64 (uint64_t value)
 Convert an unsigned 64-bit value to uint64_t.
ZLANG_STATIC_INLINE uint64_t zlang_to_u64_f64 (double value)
 Convert a floating value to uint64_t, trapping on invalid input.
ZLANG_STATIC_INLINE int64_t zlang_to_i64_i64 (int64_t value)
 Convert a signed 64-bit value to int64_t, trapping on overflow.
ZLANG_STATIC_INLINE int64_t zlang_to_i64_f64 (double value)
 Convert a floating value to int64_t, trapping on invalid input.
ZLANG_STATIC_INLINE int32_t zlang_string_equal_compare (zlang_string_t left, zlang_string_t right)
 Compare two string views by content.
ZLANG_STATIC_INLINE bool zlang_string_equal (zlang_string_t left, zlang_string_t right)
 Test two string views for content equality.
ZLANG_STATIC_INLINE zlang_string_t zlang_string_concat (char *storage, uint32_t capacity, const zlang_string_t *parts, size_t count)
 Concatenate string views into caller-owned storage.
ZLANG_STATIC_INLINE zlang_string_t zlang_string_array_append (char *storage, zlang_string_t *data, uint32_t *length, uint32_t capacity, uint32_t element_capacity, const zlang_string_t *parts, size_t count)
 Append a bounded string to caller-owned sequence storage.
ZLANG_STATIC_INLINE void * zlang_array_get (void *data, uint32_t index, uint32_t length, size_t element_size)
 Read an element pointer with a bounds trap.
ZLANG_STATIC_INLINE int32_t zlang_array_get_i32 (const int32_t *data, uint32_t index, uint32_t length)
 Read a signed integer element with a bounds trap.
ZLANG_STATIC_INLINE uint32_t zlang_array_get_u32 (const uint32_t *data, uint32_t index, uint32_t length)
 Read an unsigned integer element with a bounds trap.
ZLANG_STATIC_INLINE bool zlang_array_get_bool (const bool *data, uint32_t index, uint32_t length)
 Read a boolean element with a bounds trap.
ZLANG_STATIC_INLINE zlang_string_t zlang_array_get_string (const zlang_string_t *data, uint32_t index, uint32_t length)
 Read a bounded string element with a bounds trap.
ZLANG_STATIC_INLINE uint32_t zlang_string_byte (zlang_string_t value, uint32_t index)
 Read one byte of a bounded string with a bounds trap.
ZLANG_STATIC_INLINE void zlang_array_append (void *data, uint32_t *length, uint32_t capacity, size_t element_size, const void *value)
 Append one element to a fixed-capacity sequence, trapping on overflow.

Detailed Description

Runtime primitives called by C code emitted from Zlang.

Generated code never formats an error string at runtime. Errors are reported as fixed numeric trap codes declared in SPEC.md.

Macro Definition Documentation

◆ ZLANG_TRAP_BOUNDS

#define ZLANG_TRAP_BOUNDS   2U

◆ ZLANG_TRAP_CAPACITY

#define ZLANG_TRAP_CAPACITY   3U

◆ ZLANG_TRAP_DIVISION_BY_ZERO

#define ZLANG_TRAP_DIVISION_BY_ZERO   5U

◆ ZLANG_TRAP_INVALID_ARGUMENT

#define ZLANG_TRAP_INVALID_ARGUMENT   4U

◆ ZLANG_TRAP_OVERFLOW

#define ZLANG_TRAP_OVERFLOW   1U

◆ ZLANG_TRAP_SINK

#define ZLANG_TRAP_SINK ( code)
Value:
((void)(code), (void)raise(SIGABRT))

Typedef Documentation

◆ zlang_arguments_t

typedef struct zlang_arguments_t zlang_arguments_t

Read-only view of host process arguments created by generated main.

◆ zlang_string_t

typedef struct zlang_string_t zlang_string_t

A non-owning byte string: a pointer and an explicit length.

Function Documentation

◆ zlang_add_i32()

ZLANG_STATIC_INLINE int32_t zlang_add_i32 ( int32_t left,
int32_t right )

Add two signed integers, trapping on overflow.

Parameters
leftLeft operand.
rightRight operand.
Returns
The sum when it is representable.

◆ zlang_add_i64()

ZLANG_STATIC_INLINE int64_t zlang_add_i64 ( int64_t left,
int64_t right )

Add two signed 64-bit integers, trapping on overflow.

◆ zlang_add_u32()

ZLANG_STATIC_INLINE uint32_t zlang_add_u32 ( uint32_t left,
uint32_t right )

Add two unsigned integers, trapping on overflow.

Parameters
leftLeft operand.
rightRight operand.
Returns
The sum when it is representable.

◆ zlang_add_u64()

ZLANG_STATIC_INLINE uint64_t zlang_add_u64 ( uint64_t left,
uint64_t right )

Add two unsigned 64-bit integers, trapping on overflow.

◆ zlang_array_append()

ZLANG_STATIC_INLINE void zlang_array_append ( void * data,
uint32_t * length,
uint32_t capacity,
size_t element_size,
const void * value )

Append one element to a fixed-capacity sequence, trapping on overflow.

Parameters
dataSequence storage.
lengthCurrent element count; updated on success.
capacityMaximum element count.
element_sizeSize of one element in bytes.
valueElement to copy from.

◆ zlang_array_get()

ZLANG_STATIC_INLINE void * zlang_array_get ( void * data,
uint32_t index,
uint32_t length,
size_t element_size )

Read an element pointer with a bounds trap.

◆ zlang_array_get_bool()

ZLANG_STATIC_INLINE bool zlang_array_get_bool ( const bool * data,
uint32_t index,
uint32_t length )

Read a boolean element with a bounds trap.

◆ zlang_array_get_i32()

ZLANG_STATIC_INLINE int32_t zlang_array_get_i32 ( const int32_t * data,
uint32_t index,
uint32_t length )

Read a signed integer element with a bounds trap.

◆ zlang_array_get_string()

ZLANG_STATIC_INLINE zlang_string_t zlang_array_get_string ( const zlang_string_t * data,
uint32_t index,
uint32_t length )

Read a bounded string element with a bounds trap.

◆ zlang_array_get_u32()

ZLANG_STATIC_INLINE uint32_t zlang_array_get_u32 ( const uint32_t * data,
uint32_t index,
uint32_t length )

Read an unsigned integer element with a bounds trap.

◆ zlang_div_i32()

ZLANG_STATIC_INLINE int32_t zlang_div_i32 ( int32_t left,
int32_t right )

Divide two signed integers, trapping on zero and overflow.

◆ zlang_div_i64()

ZLANG_STATIC_INLINE int64_t zlang_div_i64 ( int64_t left,
int64_t right )

Divide two signed 64-bit integers, trapping on zero and overflow.

◆ zlang_div_u32()

ZLANG_STATIC_INLINE uint32_t zlang_div_u32 ( uint32_t left,
uint32_t right )

Divide two unsigned integers, trapping on zero.

◆ zlang_div_u64()

ZLANG_STATIC_INLINE uint64_t zlang_div_u64 ( uint64_t left,
uint64_t right )

Divide two unsigned 64-bit integers, trapping on zero.

◆ zlang_mod_i32()

ZLANG_STATIC_INLINE int32_t zlang_mod_i32 ( int32_t left,
int32_t right )

Remainder of two signed integers, trapping on zero and overflow.

◆ zlang_mod_i64()

ZLANG_STATIC_INLINE int64_t zlang_mod_i64 ( int64_t left,
int64_t right )

Remainder of two signed 64-bit integers.

◆ zlang_mod_u32()

ZLANG_STATIC_INLINE uint32_t zlang_mod_u32 ( uint32_t left,
uint32_t right )

Remainder of two unsigned integers, trapping on zero.

◆ zlang_mod_u64()

ZLANG_STATIC_INLINE uint64_t zlang_mod_u64 ( uint64_t left,
uint64_t right )

Remainder of two unsigned 64-bit integers.

◆ zlang_mul_i32()

ZLANG_STATIC_INLINE int32_t zlang_mul_i32 ( int32_t left,
int32_t right )

Multiply two signed integers, trapping on overflow.

◆ zlang_mul_i64()

ZLANG_STATIC_INLINE int64_t zlang_mul_i64 ( int64_t left,
int64_t right )

Multiply two signed 64-bit integers, trapping on overflow.

◆ zlang_mul_u32()

ZLANG_STATIC_INLINE uint32_t zlang_mul_u32 ( uint32_t left,
uint32_t right )

Multiply two unsigned integers, trapping on overflow.

◆ zlang_mul_u64()

ZLANG_STATIC_INLINE uint64_t zlang_mul_u64 ( uint64_t left,
uint64_t right )

Multiply two unsigned 64-bit integers, trapping on overflow.

◆ zlang_neg_i32()

ZLANG_STATIC_INLINE int32_t zlang_neg_i32 ( int32_t value)

Negate a signed 32-bit integer, trapping on overflow.

◆ zlang_neg_i64()

ZLANG_STATIC_INLINE int64_t zlang_neg_i64 ( int64_t value)

Negate a signed 64-bit integer, trapping on overflow.

◆ zlang_print()

ZLANG_STATIC_INLINE bool zlang_print ( zlang_string_t value)

Write a string view to standard output.

Parameters
valueString view to write.
Returns
true on success.

◆ zlang_print_bool()

ZLANG_STATIC_INLINE bool zlang_print_bool ( bool value)

Print a boolean as true or false.

Parameters
valueValue to write.
Returns
true on success.

◆ zlang_print_f32()

ZLANG_STATIC_INLINE bool zlang_print_f32 ( float value)

Print a 32-bit float with a deterministic decimal form.

◆ zlang_print_f64()

ZLANG_STATIC_INLINE bool zlang_print_f64 ( double value)

Print a 64-bit double with a deterministic decimal form.

◆ zlang_print_i32()

ZLANG_STATIC_INLINE bool zlang_print_i32 ( int32_t value)

Print a signed integer using fixed stack storage.

Parameters
valueValue to write.
Returns
true on success.

◆ zlang_print_i64()

ZLANG_STATIC_INLINE bool zlang_print_i64 ( int64_t value)

Print a signed 64-bit integer using fixed stack storage.

◆ zlang_print_u32()

ZLANG_STATIC_INLINE bool zlang_print_u32 ( uint32_t value)

Print an unsigned integer using fixed stack storage.

Parameters
valueValue to write.
Returns
true on success.

◆ zlang_print_u64()

ZLANG_STATIC_INLINE bool zlang_print_u64 ( uint64_t value)

Print an unsigned 64-bit integer using fixed stack storage.

◆ zlang_string_array_append()

ZLANG_STATIC_INLINE zlang_string_t zlang_string_array_append ( char * storage,
zlang_string_t * data,
uint32_t * length,
uint32_t capacity,
uint32_t element_capacity,
const zlang_string_t * parts,
size_t count )

Append a bounded string to caller-owned sequence storage.

Parameters
storageFlat storage with capacity * (element_capacity + 1) bytes.
dataString views updated with the stored element.
lengthCurrent element count; updated on success.
capacityMaximum element count.
element_capacityContent capacity of one element.
partsString views to concatenate into the new element.
countNumber of views.
Returns
The stored string view.

◆ zlang_string_byte()

ZLANG_STATIC_INLINE uint32_t zlang_string_byte ( zlang_string_t value,
uint32_t index )

Read one byte of a bounded string with a bounds trap.

◆ zlang_string_concat()

ZLANG_STATIC_INLINE zlang_string_t zlang_string_concat ( char * storage,
uint32_t capacity,
const zlang_string_t * parts,
size_t count )

Concatenate string views into caller-owned storage.

Parameters
storageDestination buffer with room for capacity + 1 bytes.
capacityMaximum number of content bytes, excluding the terminator.
partsString views to concatenate in order.
countNumber of views.
Returns
A view of storage containing the concatenated bytes.

Parts are copied from last to first so a caller-owned buffer may also be one of the inputs. Overflow and insufficient capacity trap instead of growing.

◆ zlang_string_equal()

ZLANG_STATIC_INLINE bool zlang_string_equal ( zlang_string_t left,
zlang_string_t right )

Test two string views for content equality.

◆ zlang_string_equal_compare()

ZLANG_STATIC_INLINE int32_t zlang_string_equal_compare ( zlang_string_t left,
zlang_string_t right )

Compare two string views by content.

Returns
-1 when left is byte-wise smaller, 0 when equal, 1 when greater.

◆ zlang_sub_i32()

ZLANG_STATIC_INLINE int32_t zlang_sub_i32 ( int32_t left,
int32_t right )

Subtract two signed integers, trapping on overflow.

◆ zlang_sub_i64()

ZLANG_STATIC_INLINE int64_t zlang_sub_i64 ( int64_t left,
int64_t right )

Subtract two signed 64-bit integers, trapping on overflow.

◆ zlang_sub_u32()

ZLANG_STATIC_INLINE uint32_t zlang_sub_u32 ( uint32_t left,
uint32_t right )

Subtract two unsigned integers, trapping on overflow.

◆ zlang_sub_u64()

ZLANG_STATIC_INLINE uint64_t zlang_sub_u64 ( uint64_t left,
uint64_t right )

Subtract two unsigned 64-bit integers, trapping on overflow.

◆ zlang_to_i32_f64()

ZLANG_STATIC_INLINE int32_t zlang_to_i32_f64 ( double value)

Convert a floating value to int32_t, trapping on invalid input.

◆ zlang_to_i32_i64()

ZLANG_STATIC_INLINE int32_t zlang_to_i32_i64 ( int64_t value)

Convert a signed 64-bit value to int32_t, trapping on overflow.

◆ zlang_to_i32_u32()

ZLANG_STATIC_INLINE int32_t zlang_to_i32_u32 ( uint32_t value)

Convert an unsigned 32-bit value to int32_t, trapping on overflow.

◆ zlang_to_i32_u64()

ZLANG_STATIC_INLINE int32_t zlang_to_i32_u64 ( uint64_t value)

Convert an unsigned 64-bit value to int32_t, trapping on overflow.

◆ zlang_to_i64_f64()

ZLANG_STATIC_INLINE int64_t zlang_to_i64_f64 ( double value)

Convert a floating value to int64_t, trapping on invalid input.

◆ zlang_to_i64_i64()

ZLANG_STATIC_INLINE int64_t zlang_to_i64_i64 ( int64_t value)

Convert a signed 64-bit value to int64_t, trapping on overflow.

◆ zlang_to_i64_u64()

ZLANG_STATIC_INLINE int64_t zlang_to_i64_u64 ( uint64_t value)

Convert an unsigned 64-bit value to int64_t, trapping on overflow.

◆ zlang_to_u32_f64()

ZLANG_STATIC_INLINE uint32_t zlang_to_u32_f64 ( double value)

Convert a floating value to uint32_t, trapping on invalid input.

◆ zlang_to_u32_i64()

ZLANG_STATIC_INLINE uint32_t zlang_to_u32_i64 ( int64_t value)

Convert a signed 64-bit value to uint32_t, trapping on overflow.

◆ zlang_to_u32_u32()

ZLANG_STATIC_INLINE uint32_t zlang_to_u32_u32 ( uint32_t value)

Convert an unsigned 32-bit value to uint32_t.

◆ zlang_to_u32_u64()

ZLANG_STATIC_INLINE uint32_t zlang_to_u32_u64 ( uint64_t value)

Convert an unsigned 64-bit value to uint32_t, trapping on overflow.

◆ zlang_to_u64_f64()

ZLANG_STATIC_INLINE uint64_t zlang_to_u64_f64 ( double value)

Convert a floating value to uint64_t, trapping on invalid input.

◆ zlang_to_u64_i64()

ZLANG_STATIC_INLINE uint64_t zlang_to_u64_i64 ( int64_t value)

Convert a signed 64-bit value to uint64_t, trapping on overflow.

◆ zlang_to_u64_u64()

ZLANG_STATIC_INLINE uint64_t zlang_to_u64_u64 ( uint64_t value)

Convert an unsigned 64-bit value to uint64_t.

◆ zlang_trap()

ZLANG_STATIC_INLINE void zlang_trap ( uint8_t code)

Terminate execution with a fixed trap code.

Parameters
codeOne of the ZLANG_TRAP_* values.

◆ zlang_write()

ZLANG_STATIC_INLINE bool zlang_write ( FILE * stream,
zlang_string_t value )

Write the exact declared byte count of a string to a stream.

Parameters
streamDestination stream.
valueString view to write.
Returns
true on success.