Skip to content

Commit

Permalink
Add namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
vbgl committed Feb 23, 2024
1 parent 879a857 commit 9c8c04b
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 65 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## New features

- Namespaces can be used to structure source code and require the same file
more than one (in different contexts)
([PR #734](https://github.com/jasmin-lang/jasmin/pull/734)).

- The type systems for constant time and speculative constant time now ensure
that division and modulo operators may only be used with public arguments.
This ensures that problems like KyberSlash (https://kyberslash.cr.yp.to/) do
Expand Down
11 changes: 10 additions & 1 deletion compiler/src/latex_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ let pp_global fmt { pgd_type ; pgd_name ; pgd_val } =
let pp_path fmt s =
F.fprintf fmt "%S " (L.unloc s)

let pp_pitem fmt pi =
let rec pp_pitem fmt pi =
match L.unloc pi with
| PFundef f -> pp_fundef fmt f
| PParam p -> pp_param fmt p
Expand All @@ -393,6 +393,15 @@ let pp_pitem fmt pi =
F.fprintf fmt "%a%a " pp_from from kw "require";
List.iter (pp_path fmt) s;
F.fprintf fmt eol
| PNamespace (ns, pis) ->
(* TODO: ident within namespaces? *)
F.fprintf fmt "%a %s " kw "namespace" (L.unloc ns);
openbrace fmt ();
F.fprintf fmt eol;
List.iter (pp_pitem fmt) pis;
F.fprintf fmt eol;
closebrace fmt ();
F.fprintf fmt eol

let pp_prog fmt =
List.iter (F.fprintf fmt "%a" pp_pitem)
2 changes: 2 additions & 0 deletions compiler/src/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"if" , IF ;
"inline", INLINE ;
"mut" , MUTABLE;
"namespace", NAMESPACE;
"param" , PARAM ;
"ptr" , POINTER;
"reg" , REG ;
Expand Down Expand Up @@ -156,6 +157,7 @@ rule main = parse
| ";" { SEMICOLON }
| "?" { QUESTIONMARK }
| ":" { COLON }
| "::" { COLONCOLON }

| ">>r" { ROR }
| "<<r" { ROL }
Expand Down
9 changes: 8 additions & 1 deletion compiler/src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
%token BANG
%token BANGEQ
%token COLON
%token COLONCOLON
%token COMMA
%token CONSTANT
%token DOT
Expand All @@ -52,6 +53,7 @@
%token LTLT
%token MINUS
%token MUTABLE
%token NAMESPACE
%token PARAM
%token PERCENT
%token PIPE
Expand Down Expand Up @@ -99,8 +101,11 @@

%%

%inline qident:
| x = separated_nonempty_list(COLONCOLON, NID) { String.concat "::" x }

%inline ident:
| x=loc(NID) { x }
| x=loc(qident) { x }

var:
| x=ident { x }
Expand Down Expand Up @@ -486,6 +491,8 @@ top:
| x=pglobal { Syntax.PGlobal x }
| x=pexec { Syntax.Pexec x }
| x=prequire { Syntax.Prequire x}
| NAMESPACE name = ident LBRACE pfs = loc(top)* RBRACE
{ Syntax.PNamespace (name, pfs) }
(* -------------------------------------------------------------------- *)
module_:
| pfs=loc(top)* EOF
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/pp_arm_m4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Immediate values (denoted <imm>) are always nonnegative integers.

open Arch_decl
open Utils
open PrintCommon
open Prog
open Var0
open Arm_decl
Expand Down Expand Up @@ -67,7 +68,8 @@ let print_asm_lines fmt lns =
(* -------------------------------------------------------------------- *)
(* TODO_ARM: This is architecture-independent. *)

let string_of_label name p = Printf.sprintf "L%s$%d" name (Conv.int_of_pos p)
let string_of_label name p =
Format.asprintf "L%s$%d" (escape name) (Conv.int_of_pos p)

let pp_label n lbl = string_of_label n lbl

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/ppasm.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(* -------------------------------------------------------------------- *)
open Utils
open PrintCommon
open Prog
open Arch_decl
open Label
Expand Down Expand Up @@ -33,7 +34,7 @@ let pp_gens (fmt : Format.formatter) xs =

(* -------------------------------------------------------------------- *)
let string_of_label name (p : label) =
Format.sprintf "L%s$%d" name (Conv.int_of_pos p)
Format.asprintf "L%s$%d" (escape name) (Conv.int_of_pos p)

(* -------------------------------------------------------------------- *)
type lreg =
Expand Down
Loading

0 comments on commit 9c8c04b

Please sign in to comment.