@@ -103,7 +103,6 @@ module type CONFIG = sig
103103 val no_packed : bool
104104 val no_assertions : bool
105105 val never_pack_unions : bool
106- val union_padding : bool
107106 val no_unions : bool
108107 val unreachable : string list
109108 val no_write_flush : bool
@@ -184,14 +183,19 @@ module Make (Config : CONFIG) = struct
184183 let rec bit_width ctx = function
185184 | CT_unit | CT_bool -> Some 1
186185 | CT_fbits len -> Some len
187- | CT_lbits -> Some Config. max_unknown_bitvector_width
186+ | CT_lbits ->
187+ let w = Config. max_unknown_bitvector_width in
188+ Some (required_width (Big_int. of_int (w - 1 )) + 1 + w)
188189 | CT_enum enum_id ->
189190 let members = Jib_compile. enum_members Parse_ast. Unknown ctx enum_id in
190191 Some (required_width (Big_int. of_int (IdSet. cardinal members - 1 )))
191192 | CT_constant c -> Some (required_width c)
192193 | CT_variant _ as ctyp ->
194+ let open Util.Option_monad in
193195 let ctors = Jib_compile. variant_constructor_bindings Parse_ast. Unknown ctx ctyp |> snd |> Bindings. bindings in
194- List. map (fun (_ , ctyp ) -> bit_width ctx ctyp) ctors |> Util. option_all |> Option. map (List. fold_left max 1 )
196+ let * ctor_widths = List. map (fun (_ , ctyp ) -> bit_width ctx ctyp) ctors |> Util. option_all in
197+ let max_width = List. fold_left max 1 ctor_widths in
198+ Some (max_width + required_width (Big_int. of_int (List. length ctors - 1 )))
195199 | CT_struct _ as ctyp ->
196200 let fields = Jib_compile. struct_field_bindings Parse_ast. Unknown ctx ctyp |> snd |> Bindings. bindings in
197201 List. map (fun (_ , ctyp ) -> bit_width ctx ctyp) fields |> Util. option_all |> Option. map (List. fold_left ( + ) 0 )
@@ -319,7 +323,9 @@ module Make (Config : CONFIG) = struct
319323 Reporting. unreachable (id_loc id) __POS__ " Abstract types not supported for SystemVerilog target"
320324 | CTD_abbrev _ -> empty
321325 | CTD_enum (id , ids ) ->
322- string " typedef" ^^ space ^^ string " enum" ^^ space
326+ let width = required_width (Big_int. of_int (List. length ids - 1 )) in
327+ let width_doc = lbracket ^^ string (string_of_int (width - 1 )) ^^ colon ^^ char '0' ^^ rbracket in
328+ string " typedef" ^^ space ^^ string " enum" ^^ space ^^ width_doc ^^ space
323329 ^^ group (lbrace ^^ nest 4 (hardline ^^ separate_map (comma ^^ hardline) pp_id ids) ^^ hardline ^^ rbrace)
324330 ^^ space ^^ sv_type_id id ^^ semi
325331 | CTD_struct (id , _ , fields ) ->
@@ -339,11 +345,13 @@ module Make (Config : CONFIG) = struct
339345 let sv_ctor (id , ctyp ) = wrap_type ctyp (pp_id id) in
340346 let tag_type = string (" sailtag_" ^ pp_id_string id) in
341347 let value_type = string (" sailunion_" ^ pp_id_string id) in
348+ let tag_width = required_width (Big_int. of_int (List. length ctors - 1 )) in
342349 let kind_enum =
343350 separate space
344351 [
345352 string " typedef" ;
346353 string " enum" ;
354+ lbracket ^^ string (string_of_int (tag_width - 1 )) ^^ colon ^^ char '0' ^^ rbracket;
347355 group (lbrace ^^ nest 4 (hardline ^^ separate_map (comma ^^ hardline) kind_id ctors) ^^ hardline ^^ rbrace);
348356 tag_type ^^ semi;
349357 ]
@@ -393,84 +401,54 @@ module Make (Config : CONFIG) = struct
393401 ctors
394402 in
395403 let constructors =
396- if Config. union_padding then
397- List. map
398- (fun (_ , (ctor_id , ctyp , padding_type , required_padding )) ->
399- separate space [string " function" ; string " automatic" ; sv_type_id id; pp_id ctor_id]
400- ^^ parens (wrap_type ctyp (char 'v' ))
401- ^^ semi
402- ^^ nest 4
403- (hardline ^^ sv_type_id id ^^ space ^^ char 'r' ^^ semi ^^ hardline
404- ^^ string (" sailunion_" ^ pp_id_string id)
405- ^^ space ^^ char 'u' ^^ semi ^^ hardline ^^ padding_type ^^ space ^^ char 'p' ^^ semi ^^ hardline
406- ^^ separate space
407- [
408- string " r.tag" ;
409- equals;
410- string_of_id ctor_id |> Util. zencode_string |> String. uppercase_ascii |> string ;
411- ]
412- ^^ semi ^^ hardline
413- ^^ separate space [char 'p' ^^ dot ^^ pp_id ctor_id; equals; char 'v' ]
414- ^^ semi ^^ hardline
415- ^^ ( if required_padding > 0 then
416- separate space
417- [
418- char 'p' ^^ dot ^^ string " padding" ;
419- equals;
420- ksprintf string " %d'b%s" required_padding (String. make required_padding '0' );
421- ]
422- ^^ semi ^^ hardline
423- else empty
424- )
425- ^^ separate space [char 'u' ^^ dot ^^ pp_id ctor_id; equals; char 'p' ]
426- ^^ semi ^^ hardline
427- ^^ separate space [string " r.value" ; equals; char 'u' ]
428- ^^ semi ^^ hardline ^^ string " return" ^^ space ^^ char 'r' ^^ semi
429- )
430- ^^ hardline ^^ string " endfunction"
431- )
432- padding_structs
433- else
434- List. map
435- (fun (ctor_id , ctyp ) ->
436- separate space [string " function" ; string " automatic" ; sv_type_id id; pp_id ctor_id]
437- ^^ parens (wrap_type ctyp (char 'v' ))
438- ^^ semi
439- ^^ nest 4
440- (hardline ^^ sv_type_id id ^^ space ^^ char 'r' ^^ semi ^^ hardline
441- ^^ string (" sailunion_" ^ pp_id_string id)
442- ^^ space ^^ char 'u' ^^ semi ^^ hardline
443- ^^ separate space
444- [
445- string " r.tag" ;
446- equals;
447- string_of_id ctor_id |> Util. zencode_string |> String. uppercase_ascii |> string ;
448- ]
449- ^^ semi ^^ hardline
450- ^^ separate space [char 'u' ^^ dot ^^ pp_id ctor_id; equals; char 'v' ]
451- ^^ semi ^^ hardline
452- ^^ separate space [string " r.value" ; equals; char 'u' ]
453- ^^ semi ^^ hardline ^^ string " return" ^^ space ^^ char 'r' ^^ semi
454- )
455- ^^ hardline ^^ string " endfunction"
456- )
457- ctors
404+ List. map
405+ (fun (_ , (ctor_id , ctyp , padding_type , required_padding )) ->
406+ separate space [string " function" ; string " automatic" ; sv_type_id id; pp_id ctor_id]
407+ ^^ parens (wrap_type ctyp (char 'v' ))
408+ ^^ semi
409+ ^^ nest 4
410+ (hardline ^^ sv_type_id id ^^ space ^^ char 'r' ^^ semi ^^ hardline
411+ ^^ string (" sailunion_" ^ pp_id_string id)
412+ ^^ space ^^ char 'u' ^^ semi ^^ hardline ^^ padding_type ^^ space ^^ char 'p' ^^ semi ^^ hardline
413+ ^^ separate space
414+ [
415+ string " r.tag" ;
416+ equals;
417+ string_of_id ctor_id |> Util. zencode_string |> String. uppercase_ascii |> string ;
418+ ]
419+ ^^ semi ^^ hardline
420+ ^^ separate space [char 'p' ^^ dot ^^ pp_id ctor_id; equals; char 'v' ]
421+ ^^ semi ^^ hardline
422+ ^^ ( if required_padding > 0 then
423+ separate space
424+ [
425+ char 'p' ^^ dot ^^ string " padding" ;
426+ equals;
427+ ksprintf string " %d'b%s" required_padding (String. make required_padding '0' );
428+ ]
429+ ^^ semi ^^ hardline
430+ else empty
431+ )
432+ ^^ separate space [char 'u' ^^ dot ^^ pp_id ctor_id; equals; char 'p' ]
433+ ^^ semi ^^ hardline
434+ ^^ separate space [string " r.value" ; equals; char 'u' ]
435+ ^^ semi ^^ hardline ^^ string " return" ^^ space ^^ char 'r' ^^ semi
436+ )
437+ ^^ hardline ^^ string " endfunction"
438+ )
439+ padding_structs
458440 in
459441 let sv_padded_ctor (_ , (ctor_id , _ , padding_type , _ )) = padding_type ^^ space ^^ pp_id ctor_id in
460- (if Config. union_padding then separate_map (twice hardline) fst padding_structs ^^ twice hardline else empty)
442+ separate_map (twice hardline) fst padding_structs
443+ ^^ twice hardline
461444 ^^ separate space
462445 [
463446 string " typedef" ;
464447 (if Config. no_unions then string " struct" else string " union" );
465448 string " packed" ;
466449 group
467450 (lbrace
468- ^^ nest 4
469- (hardline
470- ^^
471- if Config. union_padding then separate_map (semi ^^ hardline) sv_padded_ctor padding_structs
472- else separate_map (semi ^^ hardline) sv_ctor ctors
473- )
451+ ^^ nest 4 (hardline ^^ separate_map (semi ^^ hardline) sv_padded_ctor padding_structs)
474452 ^^ semi ^^ hardline ^^ rbrace
475453 );
476454 value_type ^^ semi;
@@ -666,7 +644,7 @@ module Make (Config : CONFIG) = struct
666644 [pp_smt v ^^ dot ^^ string " tag" ; string " ==" ; string (ctor |> zencode_id |> String. uppercase_ascii)]
667645 )
668646 | Unwrap (ctor , packed , v ) ->
669- let packed_ctor = if Config. union_padding then pp_id ctor ^^ dot ^^ pp_id ctor else pp_id ctor in
647+ let packed_ctor = pp_id ctor ^^ dot ^^ pp_id ctor in
670648 if packed then pp_smt v ^^ dot ^^ string " value" ^^ dot ^^ packed_ctor else pp_smt v ^^ dot ^^ pp_id ctor
671649 | Field (_ , field , v ) -> pp_smt v ^^ dot ^^ pp_id field
672650 | Ite (cond , then_exp , else_exp ) ->
0 commit comments