Type-Level Operations

The previous generics section described the “type-level types”, or metatypes, that Spade supports: #int, #uint, #bool, and #str and hinted at the possibility of operating on type values by writing type-level expressions called const generics, surrounded by curly braces inside type expressions, or naked in other contexts like gen if conditions. This section describes all the operations available.

The following operators are supported:

These behave just like their value-level counterparts (they have the same precedence and perform the same operations, just on metavalues with infinite range). For example, uint<16> can be written as uint<{8 + 8}>, or uint<{4 * 4}>. Const generics also allow writing types that depend on generic parameters. These allow writing types adapted to a particular requirement, for example, ensuring that addition cannot overflow by always returning a value with one bit more than its input:

Functions together with Entities together with fn and Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline. are the basic building blocks of Spade circuits. Unlike fn, entity can contain registers and therefore have state and unlike Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline., they do not have a statically known latency or Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline. structure. and Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline. are the basic building blocks of Spade circuits. Unlike entities and pipelines, functions have no internal state, they are combinational (pure). add<#uint N>(a: Type used to represent signed integers. The generic parameter (<N>) specReturns the value of the first branch if the condition is true, otherwise the second branch.
Note that unlike software languages, Spade does not have conditional execution. You do not conditionally assign values inside if expressions, you compute a value and return it.
ies the number of bits
, b: Type used to represent signed integers. The generic parameter (<N>) specReturns the value of the first branch if the condition is true, otherwise the second branch.
Note that unlike software languages, Spade does not have conditional execution. You do not conditionally assign values inside if expressions, you compute a value and return it.
ies the number of bits
) -> Type used to represent signed integers. The generic parameter (<N>) specReturns the value of the first branch if the condition is true, otherwise the second branch.
Note that unlike software languages, Spade does not have conditional execution. You do not conditionally assign values inside if expressions, you compute a value and return it.
ies the number of bits
{ a + b }

Some extra function-like operations are supported:

Using Const Generics as Values

Type-level operations can also be included in expressions directly using const type { ... }. This provides a shortcut around certain language impositions, for example allowing arbitrary-precision arithmetic and division to be performed freely and unconstrained from concerns like bit widths.

    const type { 300 / 7 /* spooky! but it's not actually a divider */ }