Skip to content

Commit

Permalink
adding support for binary constants (without the C2X ' separators)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmemarian committed Feb 2, 2024
1 parent bc739b9 commit 5e91d83
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions frontend/model/ail/ailSyntax.lem
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type basis =
| Octal
| Decimal
| Hexadecimal
| Binary

type integerConstant [name = "ic*"] =
| IConstant of integer * basis * maybe integerSuffix
Expand Down
11 changes: 9 additions & 2 deletions ocaml_frontend/decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ let decode_integer_constant str =
let (str, basisN, basis) =
if str.[0] = '0' then
let l = String.length str in
if String.length str > 1 && (str.[1] = 'x' || str.[1] = 'X') then
(String.sub str 2 (l-2), 16, AilSyntax.Hexadecimal)
if String.length str > 1 then
begin match str.[1] with
| 'x' | 'X' ->
(String.sub str 2 (l-2), 16, AilSyntax.Hexadecimal)
| 'b' | 'B' ->
(String.sub str 2 (l-2), 2, AilSyntax.Binary)
| _ ->
(String.sub str 1 (l-1), 8, AilSyntax.Octal)
end
else
(String.sub str 1 (l-1), 8, AilSyntax.Octal)
else
Expand Down
1 change: 1 addition & 0 deletions ocaml_frontend/pprinters/pp_ail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ let pp_integerConstant = function
| Octal -> String_nat_big_num.string_of_octal n
| Decimal -> String_nat_big_num.string_of_decimal n
| Hexadecimal -> String_nat_big_num.string_of_hexadecimal n
| Binary -> String_nat_big_num.string_of_binary n
) ^^ (P.optional pp_integerSuffix suff_opt)
| IConstantMax ity ->
pp_const (macro_string_of_integerType ity ^ "_MAX")
Expand Down
16 changes: 9 additions & 7 deletions ocaml_frontend/pprinters/pp_defacto_memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@ let pp_pretty_integer_value format (IV (_, ival_)) =
!^ "UNSPEC"
| IVconcrete n ->
!^ begin
let b = match format.Boot_printf.basis with
| Some AilSyntax.Octal ->
8
| Some AilSyntax.Decimal | None ->
10
| Some AilSyntax.Hexadecimal ->
16 in
let b = match format.Boot_printf.basis with
| Some AilSyntax.Octal ->
8
| Some AilSyntax.Decimal | None ->
10
| Some AilSyntax.Hexadecimal ->
16
| Some AilSyntax.Binary ->
2 in
let chars = String_nat_big_num.chars_of_num_with_basis b format.Boot_printf.use_upper n in
let bts = Bytes.create (List.length chars) in
List.iteri (Bytes.set bts) chars;
Expand Down
10 changes: 10 additions & 0 deletions ocaml_frontend/pprinters/string_nat_big_num.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ let string_of_hexadecimal_pad length n =
if has_length < length
then "0x" ^ String.make (length - has_length) '0' ^ Bytes.to_string ret
else "0x" ^ Bytes.to_string ret

let string_of_binary n =
let l = chars_of_num_with_basis 2 false n in
let ret = Bytes.create (List.length l + 2) in
Bytes.set ret 0 '0';
Bytes.set ret 1 'b';
List.iteri (fun i c ->
Bytes.set ret (i+2) c
) l;
Bytes.to_string ret
1 change: 1 addition & 0 deletions ocaml_frontend/pprinters/string_nat_big_num.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ val string_of_octal: num -> string
val string_of_decimal: num -> string
val string_of_hexadecimal: num -> string
val string_of_hexadecimal_pad: int -> num -> string
val string_of_binary: num -> string
14 changes: 14 additions & 0 deletions parsers/c/c_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,19 @@ let hexadecimal_prefix = "0x" | "0X"
let hexadecimal_constant =
hexadecimal_prefix hexadecimal_digit+

(* since C2x *)
(* TODO: we need a mechanism to signal that a C2x feature has been used *)
let binary_prefix = "0b" | "0B"

let binary_digit = "0" | "1"

let binary_constant =
(* TODO: removing the separator for now. We need to update the functions in
Decode.ml for things to work *)
(* binary_prefix (binary_digit "'"?)+ *)
binary_prefix (binary_digit)+


let octal_constant = '0' octal_digit*

let decimal_constant = nonzero_digit digit*
Expand All @@ -276,6 +289,7 @@ let integer_constant =
decimal_constant
| octal_constant
| hexadecimal_constant
| binary_constant (* since C2x *)


(* STD §6.4.3#1 *)
Expand Down

0 comments on commit 5e91d83

Please sign in to comment.