| git.druid.rocks | index | druid520 | nscc | src/ | jcplib/ | util.pm |
src/jcplib/util.pm
use v5.16;
package jcplib::util;
use strict;
use warnings FATAL => 'all';
use Exporter 'import';
# tiny shared vocabulary every j* stage needs: the one type set nsc has
# (explicit-width signed AND unsigned ints, an untyped address, void,
# plus the two aggregate forms struct.NAME and [N]T -- see
# docs/nsc.btft), and the one error-reporting idiom, with exactly two
# shapes, no others:
# err: message.
# err: stage: file:line:col: message.
# the caller owns the whole prefix (stage, and position when it has
# one); this function only adds "err: " and the trailing period. a
# malformed IR is always a build-stopping bug (hard exit 1), never
# something a stage silently skips past.
our @EXPORT_OK = qw(stage_err %WIDTH %IWIDTH is_int is_signed words_of);
# widths in bytes, by type. ptr is an 8-byte untyped address (there are
# no pointed-to types in nsc: deref is an 8-byte load, arithmetic on
# ptrs is byte-scaled -- the "all ptrs are restrict, purely unoptimized"
# consequence of there being no aliasing analysis anywhere). void has no
# width and never appears as a value. aggregates: every struct field and
# every array element occupies one 8-byte word, so struct.NAME is
# 8*nfields words and [N]T is 8*N -- the caller computes those with
# words_of() against its own field/element tables.
our %WIDTH = (i8 => 1, i16 => 2, i32 => 4, i64 => 8,
u8 => 1, u16 => 2, u32 => 4, u64 => 8,
ptr => 8, void => 0);
# widths in bits, int types only (shift/overflow guards).
our %IWIDTH = (i8 => 8, i16 => 16, i32 => 32, i64 => 64, u8 => 8, u16 => 16, u32 => 32, u64 => 64);
sub stage_err {
my ($msg) = @_;
print STDERR "err: $msg.\n";
exit 1;
}
# any integer type, signed or unsigned.
sub is_int {
my ($t) = @_;
return $t eq 'i8' || $t eq 'i16' || $t eq 'i32' || $t eq 'i64'
|| $t eq 'u8' || $t eq 'u16' || $t eq 'u32' || $t eq 'u64';
}
# the signed integer types only.
sub is_signed {
my ($t) = @_;
return $t eq 'i8' || $t eq 'i16' || $t eq 'i32' || $t eq 'i64';
}
1;