Commit graph

84 commits

Author SHA1 Message Date
Brendan Hansknecht
ffee5ab97a add some more tests, including failure for new line 2022-02-25 22:05:21 -08:00
Brendan Hansknecht
c025b8806b disable accidentally enabled test on dev backend 2022-02-25 21:07:20 -08:00
Brendan Hansknecht
f7c0e2ef19 Merge remote-tracking branch 'origin/trunk' into single-quote-literal 2022-02-24 10:13:39 -08:00
Tom Dohrmann
788c8a6af2 reimplement RocList and RocStr 2022-02-23 16:02:13 +01:00
ayazhafiz
ee37d8c4e0 Disable unimplemented int casts on wasm 2022-02-19 17:08:20 -05:00
ayazhafiz
25b355eb38 Fix cast tests 2022-02-19 16:24:49 -05:00
ayazhafiz
13067f2908 Implement Num.toNNNChecked
Closes #2411
2022-02-19 16:20:21 -05:00
ayazhafiz
d90915a8cd Implement Num.to* builtins
Just wrap over Num.intCast
2022-02-19 11:28:41 -05:00
Jan Van Bruggen
e12d98af98 WIP: Start adding new Int.toInt builtins 2022-02-19 11:28:12 -05:00
ayazhafiz
6e5c1d5914 Specialize Num.toFloat for different target float types
Closes #2476
2022-02-13 20:20:25 -05:00
Jan Van Bruggen
9a8a4c6ed7 Add Num.(min/max)(I/U)(8/16) builtins 2022-01-22 00:19:17 -07:00
Folkert de Vries
c0ea664141
Merge pull request #2375 from rtfeldman/expression-monomorphization-inlined
Monomorphize polymorphic non-function values
2022-01-21 20:10:42 +01:00
ayazhafiz
a5de224626 Specialize polymorphic non-function expressions
This commit fixes a long-standing bug wherein bindings to polymorphic,
non-function expressions would be lowered at binding site, rather than
being specialized at the call site.

Concretely, consider the program

```
main =
    n = 1

    idU8 : U8 -> U8
    idU8 = \m -> m

    idU8 n
```

Prior to this commit, we would lower `n = 1` as part of the IR, and the
`n` at the call site `idU8 n` would reference the lowered definition.
However, at the definition site, `1` has the polymorphic type `Num *` -
it is not until the the call site that we are able to refine the type
bound by `n`, but at that point it's too late. Since the default layout
for `Num *` is a signed 64-bit int, we would generate IR like

```
procedure main():
    let App.n : Builtin(Int(I64)) = 1i64;
    ...
    let App.5 : Builtin(Int(U8)) = CallByName Add.idU8 App.n;
    ret App.5;
```

But we know `idU8` expects a `u8`; giving it an `i64` is nonsense.
Indeed this would trigger LLVM miscompilations later on.

To remedy this, we now keep a sidecar table that maps symbols to the
polymorphic expression they reference, when they do so. We then
specialize references to symbols on the fly at usage sites, similar to
how we specialize function usages.

Looking at our example, the definition `n = 1` is now never lowered to
the IR directly. We only generate code for `1` at each place `n` is
referenced. As a larger example, you can imagine that

```
main =
    n = 1

    asU8 : U8 -> U8
    asU32 : U32 -> U8

    asU8 n + asU32 n
```

is lowered to the moral equivalent of

```
main =
    asU8 : U8 -> U8
    asU32 : U32 -> U8

    asU8 1 + asU32 1
```

Moreover, transient usages of polymorphic expressions are lowered
successfully with this approach. See for example the
`monomorphized_tag_with_polymorphic_arg_and_monomorphic_arg` test in
this commit, which checks that

```
main =
    mono : U8
    mono = 15
    poly = A
    wrap = Wrapped poly mono

    useWrap1 : [Wrapped [A] U8, Other] -> U8
    useWrap1 =
        \w -> when w is
            Wrapped A n -> n
            Other -> 0

    useWrap2 : [Wrapped [A, B] U8] -> U8
    useWrap2 =
        \w -> when w is
            Wrapped A n -> n
            Wrapped B _ -> 0

    useWrap1 wrap * useWrap2 wrap
```

has proper code generated for it, in the presence of the polymorphic
`wrap` which references the polymorphic `poly`.

https://github.com/rtfeldman/roc/pull/2347 had a different approach to
this - polymorphic expressions would be converted to (possibly capturing) thunks.
This has the benefit of reducing code size if there are many polymorphic
usages, but may make the generated code slower and makes integration
with the existing IR implementation harder. In practice I think the
average number of polymorphic usages of an expression will be very
small.

Closes https://github.com/rtfeldman/roc/issues/2336
Closes https://github.com/rtfeldman/roc/issues/2254
Closes https://github.com/rtfeldman/roc/issues/2344
2022-01-19 22:52:15 -05:00
Jan Van Bruggen
8e4b6f0cab Remove Num.(min/max)Int
Replace all uses with `Num.(min/max)I64`,
since all uses expect an `I64`.
2022-01-19 09:55:06 -07:00
Jan Van Bruggen
591477e77b Add most remaining Num.min/max* builtins
This skips `min/maxU128`, as they require a subtle change
to the `I128`-centric implementation of `Int`s.
2022-01-17 15:26:23 -07:00
Jan Van Bruggen
d7e2be306f WIP: Add Num.minI128 builtin (TODOs remain) 2022-01-15 17:49:15 -07:00
ayazhafiz
2c41c43aea Implement saturated add/subtract 2022-01-10 22:37:08 -05:00
ayazhafiz
abe42781d5 Use unsigned LLVM intrinsic arithmetic for unsigned integers
Closes #2331
2022-01-10 19:20:51 -05:00
Brian Carroll
594fe9124d Wasm support for 128-bit number literals, isFinite, ==, and != 2021-12-13 09:05:07 +00:00
satotake
a7fe2e3e53 Fix llvm Num.toFloat width conversion
Close #2156
2021-12-11 08:12:57 +00:00
Brendan Hansknecht
251a6e4121 disable not yet working test cases 2021-12-07 17:50:00 -08:00
Brendan Hansknecht
54861ef5fa Pass layout to literal loading in dev backend 2021-12-07 14:31:13 -08:00
satotake
5003223465 create cvtsx2_help 2021-12-05 15:40:00 +00:00
satotake
9ef80444f1 Merge branch 'trunk' into dev-backend-num-to-float 2021-12-05 12:46:40 +00:00
satotake
10af89654b add x86_64 Num.toFloat support for gen_dev 2021-12-05 12:32:16 +00:00
Chelsea Troy
2eefe9ff6d cargo fmt 2021-12-01 18:38:51 -06:00
Brian Carroll
72fa6217fb Refactor wasm lowlevels to make it easier to add more 128-bit ops 2021-12-01 15:09:23 +00:00
rvcas
40090f20e6 test: update tests to use Num.toStr 2021-11-30 14:56:08 -05:00
Chelsea Troy
6cf755ad8d Resolve a bunnnch of merge conflicts 2021-11-29 23:14:29 -06:00
Chelsea Troy
50e0bee22f Fix test for string literal 2021-11-29 22:25:28 -06:00
Brian Carroll
dcd4914ac1 Fix and enable tests involving empty records 2021-11-29 00:57:28 +00:00
satotake
10afadd810 Merge branch 'trunk' into dev-backend-num-is-zero 2021-11-24 10:59:42 +00:00
satotake
0085272cb8 Merge branch 'trunk' into dev-backend-num-is-zero 2021-11-22 15:25:48 +00:00
Folkert
bceebc4f8f first pass at decimal addition in the wasm backend 2021-11-21 23:00:12 +01:00
Folkert
fc635abe0b Revert "decimal add experiment"
This reverts commit bfd5ca623c.
2021-11-21 20:29:12 +01:00
Folkert
bfd5ca623c decimal add experiment 2021-11-21 20:02:33 +01:00
Folkert
5529841d68 Merge remote-tracking branch 'origin/trunk' into decimal-literals 2021-11-21 19:31:38 +01:00
satotake
eec8ad7112 add x86_64 Int is zero support for gen_dev 2021-11-21 10:29:55 +00:00
satotake
3a890f46d3 Add x86_64 Int lt support for gen_dev
Close #2033
2021-11-20 06:17:22 +00:00
satotake
e064deca76 add x86_64 Int neq support for gen_dev 2021-11-19 16:50:41 +00:00
satotake
2eae1a857b Fix comment wording 2021-11-18 11:20:37 +00:00
satotake
71bef85984 add x86_64 Int Num.neg support for gen_dev 2021-11-16 15:22:32 +00:00
Folkert
57538c5e94 store and load decimal numbers 2021-11-12 21:38:22 +01:00
Brian Carroll
7f633c107f Fix a bug in our model of the Wasm VM stack 2021-11-12 17:44:31 +00:00
Brian Carroll
0ce0e337cd Enable more tests 2021-11-10 15:46:34 +00:00
Brian Carroll
7c95189e4a Get lots of Num lowlevel ops working 2021-11-10 14:21:32 +00:00
Folkert
a9ce02799c WIP 2021-11-09 21:37:56 +01:00
Brian Carroll
8392431bd8 Enable a few more tests for gen_wasm 2021-11-09 15:20:55 +00:00
Brendan Hansknecht
360974398a Merge test_dev, test_wasm, and test_wasm_util into test_gen 2021-11-08 19:31:20 -08:00
Folkert
4568412df5 add gen num test 2021-10-02 20:24:24 +02:00