1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-03 09:41:10 +03:00
Commit Graph

36 Commits

Author SHA1 Message Date
Łukasz Czajka
8b8f323bf1
Cairo Elliptic Curve builtin (#2731)
* Closes #2720 
* Depends on https://github.com/anoma/juvix-cairo-vm/pull/3. For proper
functioning, the latest version of `juvix-cairo-vm` needs to be
installed.
2024-04-22 14:24:09 +02:00
Łukasz Czajka
75b5228258
Cairo Poseidon hash builtin support (#2723)
* Closes #2719
2024-04-18 14:11:57 +02:00
Łukasz Czajka
ad76c7a583
Support for Cairo builtins (#2718)
This PR implements generic support for Cairo VM builtins. The calling
convention in the generated CASM code is changed to allow for passing
around the builtin pointers. Appropriate builtin initialization and
finalization code is added. Support for specific builtins (e.g. Poseidon
hash, range check, Elliptic Curve operation) still needs to be
implemented in separate PRs.

* Closes #2683
2024-04-16 19:01:30 +02:00
Łukasz Czajka
90b35b34eb
Move the Nockma standard library to a separate file (#2701)
* Closes #2688
2024-03-27 13:07:52 +01:00
Łukasz Czajka
ee2f8aefbc
Fix memory access order in the JuvixReg to CASM translation. (#2697)
Cairo VM imposes restrictions on memory access order stricter than
described in the documentation, which necessitates changing the
compilation concept for local variables.

Summary
-------------

To ensure that memory is accessed sequentially at all times, we divide
instructions into basic blocks. Within each basic block, the `ap` offset
(i.e. how much `ap` increased since the beginning of the block) is known
at each instruction, which allows to statically associate `fp` offsets
to local variables while still generating only sequential assignments to
`[ap]` with increasing `ap`. When the `ap` offset can no longer be
statically determined for new local variables (e.g. due to an
intervening recursive call), we switch to the next basic block by
calling it with the `call` instruction. The arguments of the basic block
call are the variables live at the beginning of the called block. Note
that the `fp` offsets of "old" variables are still statically determined
even after the current `ap` offset becomes unknown -- the arbitrary
increase of `ap` does not influence the previous variable associations.
Hence, we can transfer the needed local variables to the next basic
block.

Example
-----------

The JuvixReg function
```
function f(integer) : integer {
  tmp[0] = add arg[0] 1;
  tmp[1] = call g(tmp[0]);
  tmp[2] = add tmp[1] arg[0];
  tmp[3] = mul tmp[2] 2;
  tmp[4] = call g(tmp[2]);
  tmp[5] = add tmp[4] tmp[3];
  ret tmp[5];
}
```
is compiled to
```
f:
  -- code for basic block 1
  [ap] = [fp - 3] + 1; ap++
  -- now [fp] is tmp[0], because fp = ap at function start (ap offset is zero) 
  -- transfer call argument (in this case, could be optimized away)
  [ap] = [fp]; ap++
  call g
  -- now [ap - 1] contains the result tmp[1] (it is already a call argument now)
  -- we additionally transfer arg[0] which is live in the next block
  [ap] = [fp - 3]; ap++
  call rel 3
  ret
  nop

  -- code for basic block 2
  -- the above "call rel" jumps here
  -- [fp - 4] is tmp[1] 
  -- [fp - 3] is arg[0]
  [ap] = [fp - 4] + [fp - 3]; ap++
  -- now [fp] is tmp[2]
  [ap] = [fp] * 2; ap++
  -- now [fp + 1] is tmp[3]
  [ap] = [fp]; ap++
  call g
  -- now [ap - 1] is tmp[4]
  [ap] = [fp + 1]; ap++
  call rel 3
  ret
  nop

  -- code for basic block 3
  -- [fp - 4] is tmp[4]
  -- [fp - 3] is tmp[3]
  [ap] = [fp - 4] + [fp - 3]; ap++
  -- now [fp] is tmp[5]
  -- the next assignment could be optimized away in this case
  [ap] = [fp]; ap++
  ret  
```
There are three basic blocks separated by the `call` instructions. In
each basic block, we know statically the `ap` offset at each instruction
(i.e. how much `ap` increased since the beginning of the block). We can
therefore associate the temporary variables with `[fp + k]` for
appropriate `k`. At basic block boundaries we transfer live temporary
variables as arguments for the call to the next basic block.

Checklist
------------
- [x] Divide JuvixReg instructions into [basic
blocks](https://en.wikipedia.org/wiki/Basic_block).
- [x] Implement liveness analysis for each basic block.
- [x] Translate transitions between basic blocks into CASM relative
calls with local live variable transfer.
- [x] Tests for the translation from JuvixReg to Cairo bytecode executed
with the Cairo VM
2024-03-27 10:40:24 +01:00
Łukasz Czajka
7d559b1f18
CASM serialization (#2679)
* Closes #2563 

Checklist
------------

- [x] Serialization of the Haskell CASM representation to the JSON
format accepted by the Cairo VM.
- [x] Add the `cairo` target to the `compile` commands.
- [x] Output via the Cairo `output` builtin.
- [x] Relativize jumps. Cairo VM doesn't actually support absolute
jumps.
- [x] Test the translation from CASM to Cairo by running the output in
the Cairo VM
- [x] Add Cairo VM to the CI
2024-03-26 17:18:52 +01:00
Łukasz Czajka
0f713c7c84
JuvixReg to CASM translation (#2671)
* Closes #2562 

Checklist
---------

- [x] Translation from JuvixReg to CASM
- [x] CASM runtime
- [x] Juvix to CASM pipeline: combine the right transformations and
check prerequisites
- [x] CLI commands: add target `casm` to the `compile` commands
- [x] Tests:
   - [x] Test the translation from JuvixReg to CASM
   - [x] Test the entire pipeline from Juvix to CASM
2024-03-20 12:14:12 +01:00
Łukasz Czajka
dcea0bbecb
Add field element type (#2659)
* Closes #2571
* It is reasonable to finish this PR before tackling #2562, because the
field element type is the primary data type in Cairo.
* Depends on #2653

Checklist
---------

- [x] Add field type and operations to intermediate representations
(JuvixCore, JuvixTree, JuvixAsm, JuvixReg).
- [x] Add CLI option to choose field size.
- [x] Add frontend field builtins.
- [x] Automatic conversion of integer literals to field elements.
- [x] Juvix standard library support for fields.
- [x] Check if field size matches when loading a stored module.
- [x] Update the Cairo Assembly (CASM) interpreter to use the field type
instead of integer type.
- [x] Add field type to VampIR backend.
- [x] Tests

---------

Co-authored-by: Jan Mas Rovira <janmasrovira@gmail.com>
2024-02-27 14:54:43 +01:00
Jan Mas Rovira
3005772f09
Replace egrep with grep -E (#2618)
Quoting the [release notes of gnu
`grep-3.8`](https://lists.gnu.org/archive/html/info-gnu/2022-09/msg00001.html)

>   The egrep and fgrep commands, which have been deprecated since
  release 2.5.3 (2007), now warn that they are obsolescent and should
  be replaced by grep -E and grep -F.

In order to fix the warning, I've replaced all instances of `egrep` with
`grep -E`
2024-02-05 10:27:46 +01:00
Łukasz Czajka
1153f6b338
JuvixTree "apply" transformation (#2595)
* Moves the "apply" transformation from JuivxAsm to JuvixTree. This
transformation removes the `CallClosures` nodes.
* Makes Nockma compilation tests use JuvixTree instead of JuvixAsm
files.
* Depends on #2594 
* Depends on #2590
* Depends on #2589 
* Depends on #2587
2024-01-30 15:46:10 +00:00
Łukasz Czajka
3aaa8229cc
Merge stack and temporary variable groups in JuvixReg (#2579)
After this change there are only two types of variables in JuvixReg:
arguments and local variables. The previous distinction between stack
and temporary variables was spurious and complicated things
unnecessarily.
2024-01-17 19:11:40 +01:00
Łukasz Czajka
8e5f45f16f
Require semicolon to separate case/if branches in JuvixAsm syntax (#2574)
This makes the syntax more uniform. It was especially confusing with
nested branching, where some closing braces had to and others couldn't
be followed by a semicolon. Now all have to be followed by a semicolon
(except function ending braces).
2024-01-11 09:19:36 +00:00
Dimitris Apostolou
d6d21a22e3
Fix typos (#2573) 2024-01-08 13:27:18 +01:00
Łukasz Czajka
758d1cd949
Implement the dynamic dispatch loop in JuvixAsm (#2556)
* Closes #2555 
* Depends on #2554
2023-12-15 19:08:40 +01:00
Paul Cadman
d8027fc843
runtime Makefile: Do not resolve variables when writing a dependency file (#2538)
This is an attempt to fix a confusing situation where a user:

1. Builds the runtime with clang that does not support wasm32-wasi
2. Attempts to rebuild the runtime by passing the CC parameter pointing
to another installation of clang that does support wasm32-wasi.

In step 1. the runtime Makefile generates dependencies files which
contain the resolved value of `$(CC)`. When the user passes the correct
`CC` variable to the Makefile in step 2., the dependencies files are not
regenerated, the old value of `CC` is used in the build and the build
continues to fail.

In this PR we change the dependency file generation so that the
variables like `$(CC)` are written into the dependency file verbatim.
They are resolved when they are run in step 2. using the new value of
the CC parameter.

* Closes https://github.com/anoma/juvix/issues/2537
2023-11-27 10:14:20 +01:00
Jonathan Cubides
7b7f06f81a
Update clang formatting (#2465)
Using

```
clang-format --version
Homebrew clang-format version 17.0.2
```
2023-10-23 19:12:56 +02:00
Łukasz Czajka
1260853583
Improve closure calls in the runtime (#2396)
* Closes #1653 

The running time improvement is 20-30% on benchmarks dominated by
closure calls.
2023-09-29 14:20:00 +02:00
Łukasz Czajka
1710615fb0
VampIR range checks and error handling (#2344)
* Adds range checks and proper error handling to the Juvix-to-VampIR
pipeline.
* Adds the `--unsafe` option which disables the checks.
2023-09-12 19:56:28 +02:00
Łukasz Czajka
8095e52c21
Check for incomparable precedences (#2289)
Give an error when there are two operators with incomparable precedences
instead of linearising the ordering arbitrarily.
2023-08-16 16:33:29 +02:00
Łukasz Czajka
eebe961321
User-friendly operator declaration syntax (#2270)
* Closes #1964 

Adds the possibility to define operator fixities. They live in a
separate namespace. Standard library defines a few in
`Stdlib.Data.Fixity`:
```

syntax fixity rapp {arity: binary, assoc: right};
syntax fixity lapp {arity: binary, assoc: left, same: rapp};
syntax fixity seq {arity: binary, assoc: left, above: [lapp]};

syntax fixity functor {arity: binary, assoc: right};

syntax fixity logical {arity: binary, assoc: right, above: [seq]};
syntax fixity comparison {arity: binary, assoc: none, above: [logical]};

syntax fixity pair {arity: binary, assoc: right};
syntax fixity cons {arity: binary, assoc: right, above: [pair]};

syntax fixity step {arity: binary, assoc: right};
syntax fixity range {arity: binary, assoc: right, above: [step]};

syntax fixity additive {arity: binary, assoc: left, above: [comparison, range, cons]};
syntax fixity multiplicative {arity: binary, assoc: left, above: [additive]};

syntax fixity composition {arity: binary, assoc: right, above: [multiplicative]};
```

The fixities are identifiers in a separate namespace (different from
symbol and module namespaces). They can be exported/imported and then
used in operator declarations:
```
import Stdlib.Data.Fixity open;

syntax operator && logical;
syntax operator || logical;
syntax operator + additive;
syntax operator * multiplicative;
```
2023-08-09 18:15:51 +02:00
Jan Mas Rovira
f7382caeac
Record updates (#2263)
- Closes #1642.

This pr introduces syntax for convenient record updates.
Example:
```
type Triple (A B C : Type) :=
  | mkTriple {
      fst : A;
      snd : B;
      thd : C;
    };

main : Triple Nat Nat Nat;
main :=
  let
    p : Triple Nat Nat Nat := mkTriple 2 2 2;
    p' :
      Triple Nat Nat Nat :=
        p @Triple{
          fst := fst + 1;
          snd := snd * 3
        };
    f : Triple Nat Nat Nat -> Triple Nat Nat Nat := (@Triple{fst := fst * 10});
  in f p';
```
We write `@InductiveType{..}` to update the contents of a record. The
`@` is used for parsing. The `InductiveType` symbol indicates the type
of the record update. Inside the braces we have a list of `fieldName :=
newValue` items separated by semicolon. The `fieldName` is bound in
`newValue` with the old value of the field. Thus, we can write something
like `p @Triple{fst := fst + 1;}`.

Record updates `X@{..}` are parsed as postfix operators with higher
priority than application, so `f x y @X{q := 1}` is equivalent to `f x
(y @X{q := 1})`.

It is possible the use a record update with no argument by wrapping the
update in parentheses. See `f` in the above example.
2023-08-07 12:35:36 +02:00
Łukasz Czajka
70dea79181
Minor refactor and script update (#2261)
* Updates the `cntlines.sh` script to take into account the removal of
Abstract.
* Moves the VampIR runtime to `runtime/src/vampir`.
2023-07-27 17:57:20 +02:00
Łukasz Czajka
afbee7f14d
Fix isNegative in the VampIR runtime (#2212)
* Closes #2206
2023-06-20 15:34:50 +02:00
Łukasz Czajka
4c2aa499af
Use stderr for errors in the runtime (#2184)
* Closes #1902
2023-06-07 14:38:28 +02:00
Łukasz Czajka
4c0e0b13c8
Respect fixity in runtime printer (#2182)
* Closes #2180
2023-06-07 11:44:41 +02:00
Łukasz Czajka
9765686b97
Fix spurious messages from Makefile.generic (#2125)
* Closes #2124
2023-05-24 12:50:02 +02:00
Łukasz Czajka
63a2c5144d
Print quoted strings in the runtime (#1969)
* Closes #1968
2023-04-04 10:29:02 +01:00
Łukasz Czajka
6eae6c405c
Fix memory count for string operations (#1924)
* Closes #1919
2023-03-23 19:47:51 +00:00
Łukasz Czajka
2a8585ede0
Fix bug in IO runtime (#1906)
Co-authored-by: Paul Cadman <git@paulcadman.dev>
2023-03-21 14:34:46 +00:00
Łukasz Czajka
4be4d58d30
String builtins (#1784)
- Progress for #1742 
* Adds builtin primitives for operations on strings and removes the
corresponding foreign & compile blocks.
2023-01-31 18:31:04 +01:00
Łukasz Czajka
f2298bd674
JuvixCore to JuvixAsm translation (#1665)
An implementation of the translation from JuvixCore to JuvixAsm. After
merging this PR, the only remaining step to complete the basic
compilation pipeline (#1556) is the compilation of complex pattern
matching (#1531).

* Fixes several bugs in lambda-lifting.
* Fixes several bugs in the RemoveTypeArgs transformation.
* Fixes several bugs in the TopEtaExpand transformation.
* Adds the ConvertBuiltinTypes transformation which converts the builtin
bool inductive type to Core primitive bool.
* Adds the `juvix dev core strip` command.
* Adds the `juvix dev core asm` command.
* Adds the `juvix dev core compile` command.
* Adds two groups of tests: 
- JuvixCore to JuvixAsm translation: translate JuvixCore tests to
JuvixAsm and run the results with the JuvixAsm interpreter,
- JuvixCore compilation: compile JuvixCore tests to native code and WASM
and execute the results.
* Closes #1520 
* Closes #1549
2023-01-09 18:21:30 +01:00
Łukasz Czajka
f5de5faaef
Translation from JuvixAsm to C (#1619) 2022-12-06 11:33:20 +01:00
Łukasz Czajka
d1937ddfa5
Compute maximum runtime stack height in JuvixReg (#1613)
compute max stack height
2022-11-09 18:49:24 +00:00
Łukasz Czajka
9d4f843262
Precompute maximum heap allocation (#1608) 2022-11-08 13:42:40 +01:00
Łukasz Czajka
6adf5ed20a
Fix discrepancy between Juvix and WASM pages (#1605) 2022-11-04 13:32:29 +01:00
Łukasz Czajka
74bfe592f5
Juvix C runtime (#1580) 2022-11-03 09:38:09 +01:00