Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #122 by using operators that are consistent with the type of the operands #130

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions lib/AstOfLlbc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,12 @@ let expression_of_place (env : env) (p : C.place) : K.expr =
L.log "AstOfLlbc" "expression of place: %s" (C.show_place p);
expression_of_place env p

(* We produce bit-wise operators first, then when the type is of booleans, we
change into the non-B variants (Rust does not distinguish between bitwise and
boolean operators) *)
let op_of_unop (op : C.unop) : Krml.Constant.op =
match op with
| C.Not -> Not
| C.Not -> BNot
| C.Neg -> Neg
| _ -> assert false

Expand Down Expand Up @@ -684,8 +687,13 @@ let mk_op_app (op : K.op) (first : K.expr) (rest : K.expr list) : K.expr =
| t -> fail "Not an operator type: %a" ptyp t
in
let op =
if op = Not && first.typ <> K.TBool then
Krml.Constant.BNot
if first.typ = K.TBool then
match op with
| BNot -> Krml.Constant.Not
| BAnd -> And
| BOr -> Or
| BXor -> Xor
| op -> op
else
op
in
Expand Down Expand Up @@ -1764,7 +1772,7 @@ let decl_of_id (env : env) (id : C.any_decl_id) : K.decl option =
(fun ({ C.variant_name; discriminant; _ } : C.variant) ->
let v =
if has_custom_constants then
Some (Z.to_int discriminant.value)
Some discriminant.value
else
None
in
Expand Down
41 changes: 21 additions & 20 deletions lib/Bundles.ml
Original file line number Diff line number Diff line change
Expand Up @@ -458,27 +458,28 @@ let reassign_monomorphizations (files : Krml.Ast.file list) (config : config) =
Krml.MonomorphizationState.generated_lids;
(* Review the type monomorphization state. *)
Hashtbl.iter
(fun (generic_lid, ts, _) (_, monomorphized_lid) ->
(fun (generic_lid, ts, _) (_, monomorphized_lid, normalized) ->
(* Krml.KPrint.bprintf "generic=%a, monomorphized=%a\n" plid generic_lid plid monomorphized_lid; *)
match
List.find_map
(fun {
name;
inline_static;
monomorphizations_of;
monomorphizations_using;
monomorphizations_exact;
_;
} ->
find_map (matches monomorphized_lid) monomorphizations_exact
||| List.find_map (uses monomorphizations_using) ts
||| find_map (matches generic_lid) monomorphizations_of
|> Option.map (fun vis -> name, inline_static, vis))
config
with
| Some name -> Hashtbl.add target_of_lid monomorphized_lid name
| None -> ())
Krml.Monomorphization.state;
if not normalized then
match
List.find_map
(fun {
name;
inline_static;
monomorphizations_of;
monomorphizations_using;
monomorphizations_exact;
_;
} ->
find_map (matches monomorphized_lid) monomorphizations_exact
||| List.find_map (uses monomorphizations_using) ts
||| find_map (matches generic_lid) monomorphizations_of
|> Option.map (fun vis -> name, inline_static, vis))
config
with
| Some name -> Hashtbl.add target_of_lid monomorphized_lid name
| None -> ())
Krml.MonomorphizationState.state;
(* Debug *)
Hashtbl.iter
(fun lid (target, _inline_static, vis) ->
Expand Down
7 changes: 7 additions & 0 deletions test/issue_123/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/issue_123/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "issue_123"
version = "0.1.0"
edition = "2021"

[dependencies]
39 changes: 39 additions & 0 deletions test/issue_123/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
enum Gamma2 {
V95_232 = 95_232,
V261_888 = 261_888,
}

enum E1 {
C1 = 0xffffffff,
C2 = -0xffffffff,
C3 = 0x0fffffff
}

#[derive(PartialEq)]
enum E2 {
C1 = 0xff,
C2 = -1,
}

enum E3 {
C1 = 0xff
}

enum E4 {
C1 = 0x7f,
C2 = -0x7e
}

enum E {
One = 1,
Five = 5,
}

fn fun(e: E) -> i32 {
e as i32
}

fn main() {
assert_eq!(E2::C1 as isize, -1);
assert_eq!(fun(E::One), 1);
}