Unsigned Integer Support #213
No reviewers
Labels
No Milestone
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: M-Labs/nac3#213
Loading…
Reference in New Issue
No description provided.
Delete Branch "uint"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Add support for
uint32
anduint64
, with corresponding integer type conversions and irrt support.One thing unsure about the
u64
constant support:Currently I attempted to change the parser output of constant integers from
i64
tou64
instead ofi128
or a(u64, sign)
. Performance-wisei64
would be better than usingi128
or(u64, sign)
, but now the constant integer bound check will be not available, and the interpretation of types of constant integers will be entirely depending on the annotations likeint64(..)
oruint32(..)
or no special annotation(which will be int32).Should we use
i128
or(u64, sign)
for a more comprehensive support foru64
constantsu64
constants directly since they seems to be less common? (they should still be able to be created by leveraging llvm constant optimization, for example to getu64::MAX
:output ir under current optimization:
print:
562054bca1
toba7afc8928
force-pushed to rebase on the latest master branch
What about making the parser represent:
u64
asInt(Some(u64))
.u64
asInt(None)
.u64
asUSub(Int(Some(u64)))
.u64
asUSub(Int(None))
.This way we do not lose any useful information at the parsing stage, and I think the code structure of the compiler should be the simplest with this design.
Note that the compiler could still report an overflow e.g. when processing
USub(Int(Some(u64)))
, or when convertingInt(Some(u64))
intoi64
.Thanks for the suggestion! After thinking and trying it for some time I think this would be good, as long as we are careful to handle a deeper ast representing integer constants in type check and codegen when we are traversing the ast.
ba7afc8928
toe7df755435
force-pushed to rebase on the latest master branch
Update: deeper ast for constant should be fine without too much modification, we can modify the parser to limit the depth of ast representing int constants to be smaller than 2, and then handle these in the type check and codegen. I will finish the detailed implementation tomorrow.
Why not just i128? I don't think the performance penalty would be that large: we basically never touch the integer until we turn them into constants, and we will figure out their type when we turn them into constants. The memory overhead is not large either,
Option<u64>
will likely occupy similar size due to alignment requirements in 64 bits machine.I think i128 will be a lot simpler to handle without so many different cases.
While they are uncommon, there would be a problem with constants (including in non-NAC3 code) that do not fit in i128.
Indeed, a simple hack would be to encode those constants with a i128 value that is invalid in nac3 code, such as
i128::MAX
.Option<i128>
would require a bit larger size.Yes it should be fine.
I didn't realize there would be an issue there. The
i128::MAX
option seems ok.Thanks for the suggestions! I will revert the changes and use
i128::MAX
instead.e7df755435
to50bc5ee361
@ -316,6 +323,20 @@ pub fn set_primitives_magic_methods(store: &PrimitiveStore, unifier: &mut Unifie
impl_comparison(unifier, store, int64_t, int64_t);
impl_eq(unifier, store, int64_t);
for t in [uint32_t, uint64_t] {
Why not
t in [int32_t, int64_t, uint32_t, uint64_t]
and remove the two blocks above?Oh ok I will merge the two blocks above into this loop
Done
50bc5ee361
to3ddfd85bf0
@ -112,7 +112,7 @@ impl fmt::Display for Tok {
use Tok::*;
match self {
Name { name } => write!(f, "'{}'", ast::get_str_from_ref(&ast::get_str_ref_lock(), *name)),
Int { value } => if let Some(value) = value { write!(f, "'{}'", value) } else { write!(f, "'#OFL#'") },
We may want to keep the special treatment of
i128::MAX
here.updated to keep the special treatment of
i128::MAX
3ddfd85bf0
tod848c2284e