Skip to content

Commit 64b53a0

Browse files
committed
int -> int64 for lospec literals
1 parent 1bacdf9 commit 64b53a0

8 files changed

Lines changed: 30 additions & 20 deletions

File tree

libs/lospecs/ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type mulk = [`U of hld | `S of hld | `US] [@@deriving yojson]
5050
(* -------------------------------------------------------------------- *)
5151
type aexpr_ =
5252
| EVar of ident
53-
| EInt of int
53+
| EInt of int64
5454
| ESlice of aexpr * (aexpr * int * int)
5555
| EAssign of aexpr * (aexpr * int * int) * aexpr
5656
| EApp of ident * aexpr list

libs/lospecs/circuit.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ let of_int ~(size : int) (v : int) : reg =
131131
Array.init size (fun i -> constant (bit ~position:i v))
132132

133133
(* -------------------------------------------------------------------- *)
134-
let of_int32 (v : int32) : reg =
135-
Array.init 32 (fun i -> constant (bit32 ~position:i v))
134+
let of_int32 ?(size = 32) (v : int32) : reg =
135+
Array.init size (fun i -> constant (bit32 ~position:i v))
136136

137137
(* -------------------------------------------------------------------- *)
138-
let of_int64 (v : int64) : reg =
139-
Array.init 64 (fun i -> constant (bit64 ~position:i v))
138+
let of_int64 ?(size = 64) (v : int64) : reg =
139+
Array.init size (fun i -> constant (bit64 ~position:i v))
140140

141141
(* -------------------------------------------------------------------- *)
142142
let of_int32s (vs : int32 array) : reg =

libs/lospecs/circuit.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ val bool_list_of_reg : reg -> bool list
2525
(* ==================================================================== *)
2626
val of_int : size:int -> int -> reg
2727

28+
val of_int32 : ?size:int -> int32 -> reg
29+
30+
val of_int64 : ?size:int -> int64 -> reg
31+
2832
val of_bigint : size:int -> Z.t -> reg
2933

3034
val of_int32s : int32 array -> reg

libs/lospecs/circuit_spec.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ let circuit_of_specification (rs : reg list) (p : adef) : reg =
158158

159159
| ESlice (e, ({ node = EInt offset }, size, scale)) ->
160160
let e = of_expr env e in
161-
let offset = offset * scale in
161+
let offset = (Int64.to_int offset) * scale in
162162
let size = size * scale in
163163
Array.sub e offset size
164164

@@ -177,7 +177,7 @@ let circuit_of_specification (rs : reg list) (p : adef) : reg =
177177
| EAssign (e, ({ node = EInt offset }, size, scale), v) ->
178178
let e = of_expr env e in
179179
let v = of_expr env v in
180-
let offset = offset * scale in
180+
let offset = (Int64.to_int offset) * scale in
181181
let size = size * scale in
182182
let pre, e = split_at_arr offset e in
183183
let e, post = split_at_arr size e in
@@ -251,7 +251,7 @@ let circuit_of_specification (rs : reg list) (p : adef) : reg =
251251

252252
| EInt i -> begin
253253
match e.type_ with
254-
| `W n -> Circuit.of_int ~size:n i
254+
| `W n -> Circuit.of_int64 ~size:n i
255255
| _ -> raise (CircuitSpecError (Format.asprintf "Expected int got %a" pp_atype e.type_))
256256
end
257257

libs/lospecs/lexer.mll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ rule main = parse
4848
{ Hashtbl.find_default keywords x (IDENT x) }
4949

5050
| decnum as d
51-
{ NUMBER (int_of_string d) }
51+
{ NUMBER (Int64.of_string d) }
5252

5353
| hexnum as d
54-
{ NUMBER (int_of_string d) }
54+
{ NUMBER (Int64.of_string d) }
5555

5656
| whitespace+
5757
{ main lexbuf }

libs/lospecs/parser.mly

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
%token RPAREN
2929

3030
%token<string> IDENT
31-
%token<int> NUMBER
31+
%token<int64> NUMBER
3232

3333
%type <Ptree.pprogram> program
3434

@@ -50,7 +50,7 @@
5050

5151
%inline wtype_:
5252
| AT x=NUMBER
53-
{ `W x }
53+
{ `W (Int64.to_int x) }
5454

5555
%inline wtype:
5656
| w=loc(wtype_) { w }
@@ -60,7 +60,7 @@ fname_:
6060
{ (f, None) }
6161

6262
| f=loc(IDENT) p=angled(list0(loc(NUMBER), COMMA))
63-
{ (f, Some (List.map (Lc.map (fun x -> `W x)) p)) }
63+
{ (f, Some (List.map (Lc.map (fun x -> `W (Int64.to_int x))) p)) }
6464

6565
%inline fname:
6666
| f=loc(fname_) { f }

libs/lospecs/ptree.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type pfname = (psymbol * pword list option) loced [@@deriving yojson]
8888
type pexpr_ =
8989
| PEParens of pexpr
9090
| PEFName of pfname
91-
| PEInt of int * pword option
91+
| PEInt of int64 * pword option
9292
| PECond of pexpr * (pexpr * pexpr)
9393
| PEFun of pargs * pexpr
9494
| PELet of (psymbol * pargs option * pexpr) * pexpr

libs/lospecs/typing.ml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,18 @@ let check_plain_arg (_ : env) (arg : pexpr option loced) =
109109
arg
110110

111111
(* -------------------------------------------------------------------- *)
112-
let as_int_constant (e : pexpr) : int =
112+
let as_int_constant (e : pexpr) : int64 =
113113
match e.data with
114114
| PEInt (i, None) -> i
115115
| _ -> tyerror e.range "integer constant expected"
116116

117117
(* -------------------------------------------------------------------- *)
118+
let as_nativeint_constant (e : pexpr) : int =
119+
match e.data with
120+
| PEInt (i, None) -> Int64.to_int i
121+
| _ -> tyerror e.range "integer constant expected"
122+
123+
(* -------------------------------------------------------------------- *)
118124
type sig_ = {
119125
s_name : string;
120126
s_ntyparams : int;
@@ -468,17 +474,17 @@ let rec tt_expr_ (env : env) (e : pexpr) : aargs option * aexpr =
468474
| PESlice (ev, (start, len, scale)) ->
469475
let ev = tt_expr env ev in
470476
let start = tt_expr env start in
471-
let len = Option.default 1 (Option.map as_int_constant len) in
472-
let scale = Option.default 1 (Option.map as_int_constant scale) in
477+
let len = Option.default 1 (Option.map as_nativeint_constant len) in
478+
let scale = Option.default 1 (Option.map as_nativeint_constant scale) in
473479
let node = ESlice (ev, (start, len, scale))
474480
and type_ = `W (len * scale) in
475481
(None, { node; type_; })
476482
477483
| PEAssign (ev, (start, len, scale), v) ->
478484
let ev = tt_expr env ev in
479485
let start = tt_expr env start in
480-
let len = Option.default 1 (Option.map as_int_constant len) in
481-
let scale = Option.default 1 (Option.map as_int_constant scale) in
486+
let len = Option.default 1 (Option.map as_nativeint_constant len) in
487+
let scale = Option.default 1 (Option.map as_nativeint_constant scale) in
482488
let v = tt_expr env ~check:(`W (len * scale)) v in
483489
let node = EAssign (ev, (start, len, scale), v) in
484490
(None, { node; type_ = ev.type_; })
@@ -526,7 +532,7 @@ let rec tt_expr_ (env : env) (e : pexpr) : aargs option * aexpr =
526532
let (`W w) = as_seq1 (tt_type_parameters env fn.range f ~expected:1 w) in
527533
let args = List.map (check_plain_arg env) args in
528534
let e, n = as_seq2 (check_arguments_count e.range ~expected:2 args) in
529-
let n = as_int_constant n in
535+
let n = as_nativeint_constant n in
530536
let ne = tt_expr env ~check:(`W w) e in
531537
(None, { node = ERepeat (`W (w * n), (ne, n)); type_ = `W (w * n); })
532538

0 commit comments

Comments
 (0)