Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for RISC-V 32 I M #781

Open
wants to merge 113 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
0be66c6
Add dependencies to build vscoq-language-server (temp fix).
rtetley Jan 24, 2024
edd3b75
Provides RISCV initial architecture description and first instruction…
clebreto Feb 1, 2024
bfafb60
Adds SUB, AND, MV
eponier Feb 23, 2024
881bfc8
Adds LOAD and STORE
eponier Feb 23, 2024
121aad6
Refactor cond
eponier Feb 21, 2024
65371a0
Add XOR and OR to compiler/riscv_instr_decl.v (#747)
zeri42 Mar 8, 2024
35faf94
Adds the very minimal bits for dummy compilation.
clebreto Mar 4, 2024
602bee6
WIP : bug-499.
clebreto Mar 11, 2024
8055c6d
Adds li to implement more functionalities.
clebreto Mar 11, 2024
254fd06
Merge pull request #754 from jasmin-lang/riscv-compiler-skeleton
eponier Mar 19, 2024
218dc38
Merge branch 'main' into risc-v
eponier Mar 19, 2024
b0b4453
Adds XOR lowering.
clebreto Mar 27, 2024
3a80c10
Fix adressing assembly syntax.
clebreto Mar 28, 2024
f9fc5db
Intrepret "subi" as ADDI.
clebreto Mar 28, 2024
9778be6
Factorize instructions by encodings.
clebreto Mar 28, 2024
7a00b90
Adds swap and subi as extra ops.
clebreto Mar 28, 2024
84b5d1b
Condition lowering implementation.
clebreto Apr 8, 2024
12e88bc
Provides JAL instruction to match calling convention
clebreto Apr 8, 2024
9309743
Provides tests for branching conditions.
clebreto Apr 9, 2024
2f9c8c8
Hack to compare with imm.
clebreto Apr 9, 2024
ff7977e
X1 -> X2
eponier Apr 9, 2024
33f5b14
X1 -> X2 again
eponier Apr 9, 2024
50e48b8
Push and pop ok
eponier Apr 9, 2024
6f1cc0e
assemble_cond: correctly detects 0
eponier Apr 9, 2024
6d84586
Fixes program crash
eponier Apr 9, 2024
8e2aa91
Allows to execute at least once each test.
clebreto Apr 9, 2024
922c98c
Merge branch 'main' into risc-v
eponier Apr 10, 2024
ff82fa2
Check execution of tests.
clebreto Apr 10, 2024
7de90ac
Adds MUL MULH instructions declaration.
clebreto Apr 10, 2024
51d9f0b
Fix typo.
clebreto Apr 10, 2024
1ccb0de
Continues to add multiplication ops.
clebreto Apr 10, 2024
dd28d82
Lower multiplication.
clebreto Apr 10, 2024
4e65069
Polishing
eponier Apr 10, 2024
9d82cc5
The cheatsheet is lying!
eponier Apr 10, 2024
542c92c
Proofs skeleton with Admitted.
rtetley Apr 11, 2024
e39523e
Change condt_not signature for proofs.
clebreto Apr 11, 2024
5fd0df7
Finalize previous commit.
clebreto Apr 11, 2024
263e6ce
WIP on params_proof
rtetley Apr 11, 2024
16fed13
Correctly zeroing the 2 lest significant bits.
clebreto Apr 12, 2024
b23ec40
Adapt printing of functions enclosed in namespaces.
clebreto Apr 12, 2024
19104e8
Merge branch 'main' into risc-v
eponier Apr 12, 2024
d53c71c
Escape globals and push (really needed ?).
clebreto Apr 12, 2024
fdeadb3
WIP params_proof
rtetley Apr 12, 2024
e5702f8
WIP riscv_params_proof
rtetley Apr 12, 2024
548ebde
Provides SLL and SLI.
clebreto Apr 12, 2024
3b5d937
Prove assemble_extra_op.
rtetley Apr 15, 2024
aff3b23
Adds NOT and NEG pseudo ops.
clebreto Apr 15, 2024
14428bb
Proove params_core_proof
rtetley Apr 15, 2024
37df6af
Proove params common proof
rtetley Apr 15, 2024
7f1799d
Adds support for signed / unsigned load.
clebreto Apr 15, 2024
5763849
Add srl, srli, and a first compiling risc-v version of gimli
sopmacF Apr 16, 2024
4f07a3d
WIP lowering proofs.
clebreto Apr 16, 2024
2ed57bc
WIP Proof lowering.
rtetley Apr 16, 2024
ec1f95d
Renames commonly used registers.
clebreto Apr 17, 2024
54ccb4f
Provides stack zeroization.
clebreto Apr 17, 2024
86f3f92
Advance on stackzero proof.
clebreto Apr 17, 2024
7f75d53
Advance again on stackzero.
clebreto Apr 17, 2024
a8d17b5
WIP proof lowering
rtetley Apr 18, 2024
729e5a7
Don't use callee saved register.
clebreto Apr 18, 2024
6b6561d
Admit remaining stack zero proofs.
clebreto Apr 18, 2024
ed2bf10
Fix shift declaration and proof.
clebreto Apr 18, 2024
fabe78d
Lowering proof done.
rtetley Apr 19, 2024
b5de339
Stack zeroization proof.
rtetley Apr 19, 2024
a06b9d9
Cleaning
eponier Apr 19, 2024
e249081
Gimli: fix and move to examples
eponier Apr 19, 2024
505b4c2
Lowering: factorize reasoning on op2
eponier Apr 24, 2024
538c494
Lowering: accept more left shifts
eponier Apr 25, 2024
846b1a5
assemble_cond: cleaning & more compact proof
eponier Apr 25, 2024
b466cd5
Merge branch 'main' into risc-v
eponier May 17, 2024
9464d24
Merge remote-tracking branch 'upstream/main' into risc-v
eponier Jun 3, 2024
8df3761
Merge remote-tracking branch 'upstream/main' into risc-v
eponier Jun 7, 2024
3371cb3
Merge remote-tracking branch 'upstream/main' into risc-v
eponier Jun 18, 2024
59b3b1d
Merge branch 'main' into risc-v
clebreto Jul 2, 2024
c11b63d
doit hack
clebreto Jul 2, 2024
7ecbdeb
Merge branch 'main' into risc-v
eponier Jul 29, 2024
79879e7
Merge branch 'main' into risc-v
eponier Sep 16, 2024
5017d5f
Proper asm "call" for syscalls
eponier Sep 16, 2024
3a85822
Adds some tests.
clebreto May 24, 2024
082668b
Fix sub test ...
clebreto May 24, 2024
427a733
Prove decide_op_reg_imm.
clebreto May 27, 2024
6dbfd66
Use is_arith_small_neg for sub
clebreto May 27, 2024
8e87601
Fixing and polishing
eponier May 30, 2024
28da830
Removes unused variable.
clebreto May 30, 2024
44640b4
Adds some Omul and lower_mulu tests.
clebreto May 30, 2024
e2b99d0
Fix load printing.
clebreto May 31, 2024
eee54f8
Better error message
clebreto Jun 3, 2024
df758a9
WIP large stack
clebreto Jun 3, 2024
02174a6
printing
eponier Jun 3, 2024
cca80c3
multiple fixes
eponier Jun 3, 2024
f0667d0
Adds load and fix test.
clebreto Jun 4, 2024
6313772
Adds sra instruction and associated proof.
clebreto Jul 18, 2024
c20b1a9
FIX typo.
clebreto Jul 18, 2024
4545b4a
Adds shift basic tests.
clebreto Jul 18, 2024
914d965
Template test files must be .jinc (again)
eponier Jul 29, 2024
7ce49f6
Use an env var for assembly checker on risc-v
eponier Jul 29, 2024
26fe70b
Prove riscv_lower_addressing
eponier Jun 17, 2024
03dab30
Fix macos / linux assembly checker option handling
clebreto Aug 4, 2024
11d9268
Use jasmin shift semantic properly.
clebreto Sep 3, 2024
013919d
Introduce smart_addi to stack alloc as done
clebreto Sep 3, 2024
a2473d9
Moves subi from ext op to lowering.
clebreto Sep 16, 2024
df36861
End of the proofs
eponier Sep 16, 2024
31cd7b2
Merge pull request #868 from jasmin-lang/789-risc-v-test-every-instru…
clebreto Sep 16, 2024
72d4742
Clear out unused Lemma.
clebreto Sep 30, 2024
7212a26
FIXME -> TODO.
clebreto Sep 30, 2024
1b6f96f
x28 x29 are FP temporaries -> OK
clebreto Sep 30, 2024
2296f15
FIXME -> Comment
clebreto Sep 30, 2024
9ddd343
Fix typo.
clebreto Sep 30, 2024
9008aad
Remove comment
clebreto Sep 30, 2024
eba00c4
move write_lval_eq to psem_facts.
clebreto Sep 30, 2024
582b3ca
Call pass LowerAddressing also in OCaml
eponier Oct 1, 2024
7a14e76
Add RA to the list of callee_saved registers.
clebreto Oct 8, 2024
b74ebeb
Merge branch 'main' into risc-v
bgregoir Oct 17, 2024
f286134
adapte 4fc6626e82abf81afe315e1f7344889678f48a00 to risc-v
bgregoir Oct 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ CHECKCATS ?= \
x86-64-stack-zero-loop \
x86-64-stack-zero-unrolled \
arm-m4-stack-zero-loop \
arm-m4-stack-zero-unrolled
arm-m4-stack-zero-unrolled \
risc-v

# --------------------------------------------------------------------
DESTDIR ?=
Expand Down
5 changes: 5 additions & 0 deletions compiler/config/tests.config
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@ args = -stack-zero=unrolled
okdirs = examples/**/arm-m4 tests/success/**/arm-m4
kodirs = tests/fail/**/arm-m4
exclude = tests/success/arm-m4/large_stack

[test-risc-v]
bin = ./scripts/check-risc-v
okdirs = examples/**/risc-v tests/success/**/risc-v tests/success/**/common
kodirs = tests/fail/**/risc-v
2 changes: 1 addition & 1 deletion compiler/entry/jasmin2ec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let parse_and_extract arch call_conv =
Format.eprintf "%a@." pp_hierror e;
exit 1

let model =
let model =
let alts = [ ("normal", Normal) ; ("CT", ConstantTime) ] in
let doc =
Format.asprintf "Extraction model (determines added annotations (e.g. leakage) (%s)."
Expand Down
83 changes: 83 additions & 0 deletions compiler/examples/gimli/risc-v/gimli.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
param int N_ROUND = 24;
param int N_COLUMN = 4;
param int ROUND_CONSTANT = 0x9e377900;

inline
fn swap(reg ptr u32[12] state, inline int i, inline int j) -> reg ptr u32[12] {
reg u32 x y;

x = state[i];
y = state[j];
state[i] = y;
state[j] = x;

return state;
}

inline fn ror32(reg u32 in, inline int cnt) -> reg u32 {
reg u32 u l ret;
u = in << cnt;
l = in >> (32 - cnt);
ret = u | l;
return ret;
}

export
fn gimli(reg ptr u32[12] state) -> reg ptr u32[12] {
inline int round, column;
reg u32 x, y, z;
reg u32 a, b;
reg u32 rc;
reg u32 tmp;

rc = ROUND_CONSTANT;

for round = N_ROUND downto 0 {
for column = 0 to N_COLUMN {
x = state[0 + column];
/* x <<r= 24; */
x = ror32(x,24);
y = state[4 + column];
/* y <<r= 9; */
y = ror32(y,9);
z = state[8 + column];

tmp = z << 1;
a = x ^ tmp;
b = y & z;
tmp = b << 2;
a = a ^ tmp;
state[8 + column] = a;

a = y ^ x;
b = x | z;
tmp = b << 1;
a = a ^ tmp;
state[4 + column] = a;

a = z ^ y;
b = x & y;
tmp = b << 3;
a = a ^ tmp;
state[0 + column] = a;
}

if (round % 4 == 0) {
state = swap(state, 0, 1);
state = swap(state, 2, 3);
}

if (round % 4 == 2) {
state = swap(state, 0, 2);
state = swap(state, 1, 3);
}

if (round % 4 == 0) {
a = state[0];
b = rc + round;
a ^= b;
state[0] = a;
}
}
return state;
}
36 changes: 36 additions & 0 deletions compiler/scripts/check-risc-v
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

set -e

DIR=$(mktemp -d jasminXXXXXX)
ASM=${DIR}/jasmin.s
OBJ=${DIR}/jasmin.o

trap "rm -r ${DIR}" EXIT

set -x

$(dirname $0)/../jasminc -g -arch risc-v -o ${ASM} "$@"

# Set default ASSEMBLY_CHECKER
ASSEMBLY_CHECKER=${ASSEMBLY_CHECKER:-llvm-mc}
ASSEMBLY_CHECKER_OPTIONS=${ASSEMBLY_CHECKER_OPTIONS:---triple=riscv32 --mcpu=generic-rv32 -mattr=+m --filetype=obj}
# Detect operating system
UNAME_S=$(uname -s)

# Conditionally set ASSEMBLY_CHECKER for Darwin (macOS)
if [ "$UNAME_S" == "Darwin" ]; then
ASSEMBLY_CHECKER="riscv64-unknown-elf-as"
ASSEMBLY_CHECKER_OPTIONS=""
elif [ "$UNAME_S" == "Linux" ]; then
ASSEMBLY_CHECKER="llvm-mc"
ASSEMBLY_CHECKER_OPTIONS="--triple=riscv32 --mcpu=generic-rv32 -mattr=+m --filetype=obj"
fi

# Check if the assembly checker binary exists and is executable
if ! command -v "$ASSEMBLY_CHECKER" &> /dev/null; then
echo "Error: $ASSEMBLY_CHECKER is not installed or not found in PATH."
exit 1
fi

${ASSEMBLY_CHECKER} ${ASSEMBLY_CHECKER_OPTIONS} -o ${OBJ} ${ASM}
4 changes: 4 additions & 0 deletions compiler/src/CLI_errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ let check_options () =
then warning Experimental Location.i_dummy
"support of the ARMv7 architecture is experimental";

if !target_arch = RISCV
then warning Experimental Location.i_dummy
"support of the RISC-V architecture is really experimental";

if !ec_list <> []
|| !ecfile <> ""
|| !ec_array_path <> Filename.current_dir_name
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/coreArchFactory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module Core_arch_ARM : Arch_full.Core_arch = Arm_arch_full.Arm (struct
let call_conv = Arm_decl.arm_linux_call_conv
end)

module Core_arch_RISCV : Arch_full.Core_arch = Riscv_arch_full.Riscv (struct
let call_conv = Riscv_decl.riscv_linux_call_conv
end)

let core_arch_x86 ~use_lea ~use_set0 call_conv :
(module Arch_full.Core_arch
with type reg = register
Expand Down
1 change: 1 addition & 0 deletions compiler/src/coreArchFactory.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Core_arch_ARM : Arch_full.Core_arch
module Core_arch_RISCV : Arch_full.Core_arch
open X86_decl

val core_arch_x86 :
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/glob_options.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ let set_stack_zero_size s = stack_zero_size := Some (Annot.ws_of_string s)
type architecture =
| X86_64
| ARM_M4
| RISCV
let target_arch = ref X86_64

let set_target_arch a =
let a' =
match a with
| "x86-64" -> X86_64
| "arm-m4" -> ARM_M4
| "risc-v" -> RISCV
| _ -> assert false
in target_arch := a'

Expand Down Expand Up @@ -141,6 +143,7 @@ let print_strings = function
| Compiler.LowerInstruction -> "lowering" , "lowering of instructions"
| Compiler.PropagateInline -> "propagate", "propagate inline variables"
| Compiler.SLHLowering -> "slhlowering" , "lowering of selective load hardening instructions"
| Compiler.LowerAddressing -> "loweraddr", "lowering of complex addressing modes (RISC-V only)"
| Compiler.StackAllocation -> "stkalloc" , "stack allocation"
| Compiler.RemoveReturn -> "rmreturn" , "remove unused returned values"
| Compiler.RegAllocation -> "ralloc" , "register allocation"
Expand Down Expand Up @@ -210,7 +213,7 @@ let options = [
"-intel", Arg.Unit (set_syntax `Intel), " Use intel syntax (default is AT&T)";
"-ATT", Arg.Unit (set_syntax `ATT), " Use AT&T syntax (default is AT&T)";
"-call-conv", Arg.Symbol (["windows"; "linux"], set_cc), " Select calling convention (default depends on host architecture)";
"-arch", Arg.Symbol (["x86-64"; "arm-m4"], set_target_arch), " Select target arch (default is x86-64)";
"-arch", Arg.Symbol (["x86-64"; "arm-m4"; "risc-v"], set_target_arch), " Select target arch (default is x86-64)";
"-stack-zero",
Arg.Symbol (List.map fst stack_zero_strategies, set_stack_zero_strategy),
" Select stack zeroization strategy for export functions";
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/help.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let show_intrinsics asmOp fmt =
begin match sfx with
| [] -> 0
| PVp _ :: _ -> 1
| PVx _ :: _ -> 2
| (PVs _ | PVx _) :: _ -> 2
| (PVv _ | PVsv _) :: _ -> 3
| PVvv _ :: _ -> 4
end
Expand All @@ -17,7 +17,7 @@ let show_intrinsics asmOp fmt =
let headers = [|
"no size suffix";
"one optional size suffix, e.g., “_64”";
"a zero/sign extend suffix, e.g., “_u32u16”";
"a zero/sign extend suffix, e.g., “_s16” or “_u32u16”";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is surprising: does it mean that it is now possible to write MOVSX_s16?

"one vector description suffix, e.g., “_4u64”";
"two vector description suffixes, e.g., “_2u16_2u64”";
"a flag setting suffix (i.e. “S”) and a condition suffix (i.e. “cc”)"
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/main_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ let main () =
module C = CoreArchFactory.Core_arch_ARM
let analyze _ _ _ _ _ = failwith "TODO_ARM: analyze"
end)
| RISCV ->
(module struct
module C = CoreArchFactory.Core_arch_RISCV
let analyze _ _ _ _ _ = failwith "TODO_RISCV: analyze"
end)
in
let module Arch = Arch_full.Arch_from_Core_arch (P.C) in

Expand Down
38 changes: 19 additions & 19 deletions compiler/src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
open Syntax
open Annotations

let setsign c s =
let setsign c s =
match c with
| None -> Some (Location.mk_loc (Location.loc s) (CSS(None, Location.unloc s)))
| _ -> c
Expand All @@ -20,7 +20,7 @@
%token RPAREN

%token T_BOOL
%token T_U8 T_U16 T_U32 T_U64 T_U128 T_U256 T_INT
%token T_U8 T_U16 T_U32 T_U64 T_U128 T_U256 T_INT

%token SHARP
%token ALIGNED
Expand Down Expand Up @@ -95,7 +95,7 @@
%left LTLT GTGT ROR ROL
%left PLUS MINUS
%left STAR SLASH PERCENT
%nonassoc BANG
%nonassoc BANG

%type <Syntax.pprogram> module_

Expand Down Expand Up @@ -126,9 +126,9 @@ annotationlabel:
| id=loc(keyword) { id }
| s=loc(STRING) { s }

int:
int:
| i=INT { Syntax.parse_int i }
| MINUS i=INT { Z.neg (Syntax.parse_int i ) }
| MINUS i=INT { Z.neg (Syntax.parse_int i ) }

simple_attribute:
| i=int { Aint i }
Expand All @@ -146,14 +146,14 @@ annotation:

struct_annot:
| a=separated_list(COMMA, annotation) { a }

top_annotation:
| SHARP a=annotation { [a] }
| SHARP LBRACKET a=struct_annot RBRACKET { a }

annotations:
| l=list(top_annotation) { List.concat l }


(* ** Type expressions
* -------------------------------------------------------------------- *)
Expand Down Expand Up @@ -195,7 +195,7 @@ castop1:
castop:
| c=loc(castop1)? { c }

cast:
cast:
| T_INT { `ToInt }
| s=swsize { `ToWord s }

Expand All @@ -216,7 +216,7 @@ cast:
| AMP c=castop { `BAnd c}
| PIPE c=castop { `BOr c}
| HAT c=castop { `BXOr c}
| LTLT c=castop { `ShL c}
| LTLT c=castop { `ShL c}
| s=loc(GTGT) c=castop { `ShR (setsign c s)}
| ROR c=castop { `ROR c}
| ROL c=castop { `ROL c}
Expand All @@ -241,8 +241,8 @@ prim:
%inline mem_access:
| ct=parens(utype)? LBRACKET al=unaligned? v=var e=mem_ofs? RBRACKET
{ al, ct, v, e }
arr_access_len:

arr_access_len:
| COLON e=pexpr { e }

arr_access_i:
Expand Down Expand Up @@ -367,7 +367,7 @@ pinstr_r:
| WHILE is1=pblock? LPAREN b=pexpr RPAREN is2=pblock?
{ PIWhile (is1, b, is2) }

| vd=postfix(pvardecl(COMMA?), SEMICOLON)
| vd=postfix(pvardecl(COMMA?), SEMICOLON)
{ PIdecl vd }

pif:
Expand Down Expand Up @@ -404,17 +404,17 @@ annot_stor_type:

writable:
| CONSTANT {`Constant }
| MUTABLE {`Writable }
| MUTABLE {`Writable }

pointer:
| o=writable? POINTER { o }

ptr:
| o=pointer? {
match o with
| o=pointer? {
match o with
| Some w -> `Pointer w
| None -> `Direct
}
| None -> `Direct
}

storage:
| REG ptr=ptr { `Reg ptr }
Expand All @@ -430,7 +430,7 @@ storage:
%inline pvardecl(S):
| ty=stor_type vs=separated_nonempty_list(S, loc(decl)) { (ty, vs) }

pparamdecl(S):
pparamdecl(S):
ty=stor_type vs=separated_nonempty_list(S, var) { (ty, vs) }

annot_pparamdecl:
Expand Down Expand Up @@ -472,7 +472,7 @@ pparam:
(* -------------------------------------------------------------------- *)
pgexpr:
| e=pexpr { GEword e }
| LBRACE es = rtuple1(pexpr) RBRACE { GEarray es }
| LBRACE es = rtuple1(pexpr) RBRACE { GEarray es }
| e=loc(STRING) { GEstring e }

pglobal:
Expand Down
Loading