Skip to content

Literals

Julian Oppermann edited this page Dec 13, 2022 · 10 revisions

Integer literals

C-style literals

CoreDSL 2 supports C-style literals in binary (prefix 0b), octal (prefix 0), decimal and hexadecimal (prefix 0x) representations. These literals have the unsigned type with the minimal width required to represent the value. The u, l and ll suffixes are ignored.

As bool is an alias for unsigned<1>, we define bool false = 0, true = 1;.

Verilog-style literals

The automatic width-inference may be inconvenient in certain situations. To that end, CoreDSL 2 also supports integer literals with an explicit width specification in a Verilog-inspired syntax, i.e. <size>'<signed><base><value>. The base may be b (binary), o (octal), d (decimal) or h (hexadecimal). The literal <value> must be representable in the specified <size> bits.

Examples

Expression Type Value (decimal)
0 unsigned<1> 0
1 unsigned<1> 1
-1 signed<2> -1
3 unsigned<2> 3
-3 signed<3> -3
0b101010 unsigned<6> 42
(signed) 0b101010 signed<6> -22
052 unsigned<6> 42
0x2A unsigned<6> 42
11'd42 unsigned<11> 42
4'sb1011 signed<4> -5
6'sd42 signed<6> -22
16'hFFFF unsigned<16> 2^16 - 1
5'd42 Error!

NB 1: We do not exploit the asymmetry of the two's complement representation here. Technically, -1 could be represented as a signed<1> type (i.e., just the sign bit), but as it is parsed as UnaryMinus(IntLiteral(1)), the type rules mandate that -1 has the static type signed<2>.

NB 2: Similarly, we decided against representing 0 with the unsigned<0> type. The expected gains are small, and zero-bit types always require special handling somewhere in the compilation pipeline (e.g. LLVM/MLIR do not support them) -- hence, avoiding them at the language level ensures consistency across CoreDSL implementations.

Character/string literals

CoreDSL 2 supports C-like character ('c') and string ("string") literals. Wide characters and unicode strings (L and U prefixes) are not supported. If an implementation supports escape sequences such as '\n', \042, etc., these shall carry the same meaning as in C.

Structure/Union literals

C-like compound literals such as (struct foo){1, 2} and (union bar){.baz = 33} are supported.