AST-33: Word{8, 16, 32} <--> {Nat, Int} conversions#190
Conversation
|
I would prefer the interpreter, as it is our spec, to get things right from the start. The compiler may lag behind. Maybe the interpreter should just use |
|
@nomeata For the reference interpreter I'd even prefer I agree, that the implementation should strive for being the most clean possible. The semantics of the |
Yes, that works simply by subtyping: Every |
* along with tagging, untagging * updated conversion primitives This is still work in progress, esp. naming needs to be ironed out. Refactoring due later.
ggreif
left a comment
There was a problem hiding this comment.
Naming is still a bit inconsistent, the small things should be i32, probably.
|
Are you reviewing your own code? :-) |
|
(all comments reasonable, btw) |
... as they have the same bodies
... along with minor reactoring in BoxedInt also
nomeata
left a comment
There was a problem hiding this comment.
Sorry, I missed that this PR changed from Draft to ready. Looks good!
| Small values (just <2^5 for now, so that both code paths are well-tested) | ||
| are stored unboxed, tagged, see BitTagged. | ||
|
|
||
| The heap layout of a BoxedWord is: |
There was a problem hiding this comment.
You write BoxedWord, but the module is still called BoxedInt.
There was a problem hiding this comment.
I'll address this in the follow-up.
| *) | ||
|
|
||
| let payload_field = Int32.add Tagged.header_size 0l | ||
| let payload_field = Tagged.header_size |
There was a problem hiding this comment.
That was actually intentional, to make it clear it’s the “0ths” field after the header. But maybe it’s too confusing.
There was a problem hiding this comment.
Nobody looks at the expansion anyway...
## Changelog for ic-hs: Branch: master Commits: [dfinity/ic-hs@52369fa1...f092a2d5](dfinity/ic-hs@52369fa...f092a2d) * [`12590345`](dfinity/ic-hs@1259034) bump cachix/install-nix-action ([dfinity/ic-hs#201](https://github.com/dfinity/ic-hs/issues/201)) * [`2ef5690c`](dfinity/ic-hs@2ef5690) Bump openssl from 0.10.48 to 0.10.55 in /httpbin-rs ([dfinity/ic-hs#202](https://github.com/dfinity/ic-hs/issues/202)) * [`46af8266`](dfinity/ic-hs@46af826) sync httpbin implementation with ic repo ([dfinity/ic-hs#193](https://github.com/dfinity/ic-hs/issues/193)) * [`1fd276eb`](dfinity/ic-hs@1fd276e) implement Call context removal transition ([dfinity/ic-hs#191](https://github.com/dfinity/ic-hs/issues/191)) * [`11c00d2a`](dfinity/ic-hs@11c00d2) catch HTTP exception from HTTPS outcalls ([dfinity/ic-hs#195](https://github.com/dfinity/ic-hs/issues/195)) * [`9eb5bf48`](dfinity/ic-hs@9eb5bf4) deleted call contexts prevent canister from stopping (again) ([dfinity/ic-hs#190](https://github.com/dfinity/ic-hs/issues/190)) * [`f092a2d5`](dfinity/ic-hs@f092a2d) implement and test canister history ([dfinity/ic-hs#198](https://github.com/dfinity/ic-hs/issues/198))
## Changelog for ic-hs: Branch: master Commits: [dfinity/ic-hs@52369fa1...f092a2d5](dfinity/ic-hs@52369fa...f092a2d) * [`12590345`](dfinity/ic-hs@1259034) bump cachix/install-nix-action ([dfinity/ic-hs#201](https://github.com/dfinity/ic-hs/issues/201)) * [`2ef5690c`](dfinity/ic-hs@2ef5690) Bump openssl from 0.10.48 to 0.10.55 in /httpbin-rs ([dfinity/ic-hs#202](https://github.com/dfinity/ic-hs/issues/202)) * [`46af8266`](dfinity/ic-hs@46af826) sync httpbin implementation with ic repo ([dfinity/ic-hs#193](https://github.com/dfinity/ic-hs/issues/193)) * [`1fd276eb`](dfinity/ic-hs@1fd276e) implement Call context removal transition ([dfinity/ic-hs#191](https://github.com/dfinity/ic-hs/issues/191)) * [`11c00d2a`](dfinity/ic-hs@11c00d2) catch HTTP exception from HTTPS outcalls ([dfinity/ic-hs#195](https://github.com/dfinity/ic-hs/issues/195)) * [`9eb5bf48`](dfinity/ic-hs@9eb5bf4) deleted call contexts prevent canister from stopping (again) ([dfinity/ic-hs#190](https://github.com/dfinity/ic-hs/issues/190)) * [`f092a2d5`](dfinity/ic-hs@f092a2d) implement and test canister history ([dfinity/ic-hs#198](https://github.com/dfinity/ic-hs/issues/198))
A blueprint for the conversion semantics (
Word32in particular) is written down in https://dfinity.atlassian.net/browse/AST-63. The interpreter is now ready (with the exception ofWord64which is a bit tricky as it may be lossy when converting toInt/Natin the absence of proper bignums).I would have preferred to handle the conversions
Word* -> {Nat,Int}without the shifts inprelude.ml, as it breaks abstraction, but could not find a suitable method that would do the shifting based on the representation (SubRep) for me. I'd welcome a tip how to funnel the shift amount through the module machinery (if this is possible at all; I honestly doubt it). What I was trying to do is to somehow bake it intoMakeWord'sto_bitsmethod, but I have failed so far.Compilation of many
Word*arithmetics and bitwise primitives is lacking (test/run/words.astracks this), I'd tackle this and the trivial refactoring in other PRs.Below is the laundry list:
Nat -> Word32only works up to2**31-1in the interpreter (compiler should be fine).Nat -> Word32does not check for proper bignums (in the compiler CG).unreachabletrap (missing(==) @Word32codegen).Word32has underlying OCamlInt32, which is signed (thus reading is signed too).BoxedSmallWordinstead ofBoxedInt.Wasm.Value,unboxed_zero,unboxed_one, etc.).Word*arithmetics and bitwise primitives.Word64.awkto obtain a Haskell version oftest/run/words.as, and run it, comparing outputs.Any hints welcome.