Skip to content

Commit 6fe4d17

Browse files
committedMar 12, 2025·
Add support for reading a slice of an integer
Signed-off-by: Alastair Reid <[email protected]>
1 parent b4e8bf0 commit 6fe4d17

8 files changed

+45
-5
lines changed
 

‎bin/asl2c.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@
7373
// e.g., "x[7:0]" --> "x[0 +: 8]"
7474
:xform_lower
7575
76-
// Eliminate slices of integers by first converting the integer to a bitvector.
77-
// e.g., if "x : integer", then "x[1 +: 8]" to "cvt_int_bits(x, 9)[1 +: 8]"
78-
:xform_int_bitslices
76+
{xform_int_bitslices}
7977
8078
// Convert use of getter/setter syntax to function calls.
8179
// e.g., "Mem[a, sz] = x" --> "Mem_set(a, sz, x)"
@@ -329,10 +327,17 @@ def mk_script(args, output_directory):
329327
'output_dir': output_directory,
330328
'split_state': "",
331329
'suppress_bitslice_xform': "",
330+
'xform_int_bitslices': "",
332331
'track_valid': "",
333332
'wrap_variables': "",
334333
}
335334
if args.instrument_unknown: substitutions['track_valid'] = ":xform_valid track-valid"
335+
if args.transform_int_slices:
336+
substitutions["xform_int_bitslices"] = textwrap.dedent("""\
337+
// Eliminate slices of integers by first converting the integer to a bitvector.
338+
// e.g., if "x : integer", then "x[1 +: 8]" to "cvt_int_bits(x, 9)[1 +: 8]"
339+
:xform_int_bitslices
340+
""")
336341
if args.wrap_variables: substitutions['wrap_variables'] = ":xform_wrap"
337342
if not args.auto_case_split:
338343
substitutions['auto_case_split'] = '--no-auto-case-split'
@@ -492,6 +497,7 @@ def main() -> int:
492497
parser.add_argument("--new-ffi", help="use the new FFI", action="store_true", default=False)
493498
parser.add_argument("--runtime-checks", help="perform runtime checks (array bounds, etc.)", action="store_true", default=False)
494499
parser.add_argument("--split-state", help="split state into multiple structs", action=argparse.BooleanOptionalAction)
500+
parser.add_argument("--transform-int-slices", help="convert integer slices to bit slices", action=argparse.BooleanOptionalAction, default=False)
495501
parser.add_argument("--instrument-unknown", help="instrument assignments of UNKNOWN", action=argparse.BooleanOptionalAction)
496502
parser.add_argument("--wrap-variables", help="wrap global variables into functions", action=argparse.BooleanOptionalAction)
497503
parser.add_argument("-O0", help="perform minimal set of transformations", action=argparse.BooleanOptionalAction)

‎libASL/backend_c_new.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ and expr (loc : Loc.t) (fmt : PP.formatter) (x : AST.expr) : unit =
629629
| Expr_Slices (Type_Bits (n,_), e, [Slice_LoWd (lo, wd)]) ->
630630
let module Runtime = (val (!runtime) : RuntimeLib) in
631631
Runtime.get_slice fmt (const_int_expr loc n) (const_int_expr loc wd) (mk_expr loc e) (fun fmt -> index_expr loc fmt lo)
632-
| Expr_Slices (Type_Integer _, e, [Slice_LoWd (lo, wd)]) when lo = Asl_utils.zero ->
632+
| Expr_Slices (Type_Integer _, e, [Slice_LoWd (lo, wd)]) ->
633633
let module Runtime = (val (!runtime) : RuntimeLib) in
634-
Runtime.cvt_int_bits fmt (const_int_expr loc wd) (mk_expr loc e)
634+
Runtime.get_slice_int fmt (const_int_expr loc wd) (mk_expr loc e) (mk_expr loc lo)
635635
| Expr_TApply (f, tes, es, throws) ->
636636
if throws <> NoThrow then
637637
rethrow_expr fmt (fun _ -> funcall loc fmt f tes es)

‎libASL/runtime.ml

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module type RuntimeLib = sig
6262
val pow2_int : PP.formatter -> rt_expr -> unit
6363
val align_int : PP.formatter -> rt_expr -> rt_expr -> unit
6464
val mod_pow2_int : PP.formatter -> rt_expr -> rt_expr -> unit
65+
val get_slice_int : PP.formatter -> int -> rt_expr -> rt_expr -> unit
6566
val set_slice_int : PP.formatter -> int -> rt_expr -> rt_expr -> rt_expr -> unit
6667
val print_int_dec : PP.formatter -> rt_expr -> unit
6768
val print_int_hex : PP.formatter -> rt_expr -> unit

‎libASL/runtime.mli

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module type RuntimeLib = sig
6161
val pow2_int : PP.formatter -> rt_expr -> unit
6262
val align_int : PP.formatter -> rt_expr -> rt_expr -> unit
6363
val mod_pow2_int : PP.formatter -> rt_expr -> rt_expr -> unit
64+
val get_slice_int : PP.formatter -> int -> rt_expr -> rt_expr -> unit
6465
val set_slice_int : PP.formatter -> int -> rt_expr -> rt_expr -> rt_expr -> unit
6566
val print_int_dec : PP.formatter -> rt_expr -> unit
6667
val print_int_hex : PP.formatter -> rt_expr -> unit

‎libASL/runtime_ac.ml

+8
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ module Runtime : RT.RuntimeLib = struct
367367
let cvt_bits_sint (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = cvt_bits_sint_aux fmt n int_width x
368368
let cvt_bits_uint (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = cvt_bits_uint_aux fmt n int_width x
369369

370+
let get_slice_int (fmt : PP.formatter) (w : int) (x : RT.rt_expr) (i : RT.rt_expr) : unit =
371+
let mask = Z.sub (Z.shift_left Z.one w) Z.one in
372+
PP.fprintf fmt "((%a)((%a >> %a) & %a))"
373+
ty_uint w
374+
RT.pp_expr x
375+
RT.pp_expr i
376+
(intN_literal int_width) mask
377+
370378
let set_slice_int (fmt : PP.formatter) (w : int) (l : RT.rt_expr) (i : RT.rt_expr) (r : RT.rt_expr) : unit =
371379
let mask = Z.sub (Z.shift_left Z.one w) Z.one in
372380
PP.fprintf fmt "%a = ({ int __index = %a; %a __mask = %a << __index; (%a & ~__mask) | (((%a)%a) << __index); });"

‎libASL/runtime_c23.ml

+8
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ module Runtime : RT.RuntimeLib = struct
385385
let cvt_bits_sint (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = cvt_bits_sint_aux fmt n int_width x
386386
let cvt_bits_uint (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = cvt_bits_uint_aux fmt n int_width x
387387

388+
let get_slice_int (fmt : PP.formatter) (w : int) (x : RT.rt_expr) (i : RT.rt_expr) : unit =
389+
let mask = Z.sub (Z.shift_left Z.one w) Z.one in
390+
PP.fprintf fmt "((%a)((%a >> %a) & %a))"
391+
ty_uint w
392+
RT.pp_expr x
393+
RT.pp_expr i
394+
(intN_literal int_width) mask
395+
388396
let set_slice_int (fmt : PP.formatter) (w : int) (l : RT.rt_expr) (i : RT.rt_expr) (r : RT.rt_expr) : unit =
389397
let mask = Z.sub (Z.shift_left Z.one w) Z.one in
390398
PP.fprintf fmt "%a = ({ int __index = %a; %a __mask = %a << __index; (%a & ~__mask) | (((%a)%a) << __index); });"

‎libASL/runtime_fallback.ml

+8
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ module Runtime : RT.RuntimeLib = struct
235235
let cvt_bits_uint (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = apply_bits_1_1 fmt "cvt_bits_uint" n x
236236
let cvt_int_bits (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = apply_bits_1_1 fmt "cvt_int_bits" n x
237237

238+
let get_slice_int (fmt : PP.formatter) (w : int) (x : RT.rt_expr) (i : RT.rt_expr) : unit =
239+
let slice fmt =
240+
PP.fprintf fmt "(%a >> %a)"
241+
RT.pp_expr x
242+
RT.pp_expr i
243+
in
244+
cvt_int_bits fmt w slice
245+
238246
let set_slice_int (fmt : PP.formatter) (w : int) (l : RT.rt_expr) (i : RT.rt_expr) (r : RT.rt_expr) : unit =
239247
let mask = Z.sub (Z.shift_left Z.one w) Z.one in
240248
PP.fprintf fmt "%a = ({ int __index = %a; %a __mask = %a << __index; (%a & ~__mask) | (((%a)%a) << __index); });"

‎libASL/runtime_sc.ml

+8
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,14 @@ module Runtime : RT.RuntimeLib = struct
397397
let cvt_bits_sint (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = cvt_bits_sint_aux fmt n int_width x
398398
let cvt_bits_uint (fmt : PP.formatter) (n : int) (x : RT.rt_expr) : unit = cvt_bits_uint_aux fmt n int_width x
399399

400+
let get_slice_int (fmt : PP.formatter) (w : int) (x : RT.rt_expr) (i : RT.rt_expr) : unit =
401+
let mask = Z.sub (Z.shift_left Z.one w) Z.one in
402+
PP.fprintf fmt "((%a)((%a >> %a) & %a))"
403+
ty_uint w
404+
RT.pp_expr x
405+
RT.pp_expr i
406+
(intN_literal int_width) mask
407+
400408
let set_slice_int (fmt : PP.formatter) (w : int) (l : RT.rt_expr) (i : RT.rt_expr) (r : RT.rt_expr) : unit =
401409
let mask = Z.sub (Z.shift_left Z.one w) Z.one in
402410
PP.fprintf fmt "%a = ({ int __index = %a; %a __mask = %a << __index; (%a & ~__mask) | (((%a)%a) << __index); });"

0 commit comments

Comments
 (0)
Please sign in to comment.