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

Handle cases like int table[1 << HASHBITS][WIDTH*HEIGHT][MAXSIZE + 1] #171

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
31 changes: 28 additions & 3 deletions src/Language/Rust/Corrode/C.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ applyRenames ident = case identToString ident of
"main" -> "_c_main"
"match" -> "match_"
"mod" -> "mod_"
"move" -> "move_"
"proc" -> "proc_"
"type" -> "type_"
"where" -> "where_"
Expand Down Expand Up @@ -371,13 +372,20 @@ getSymbolIdent ident = do
, result = Rust.Deref (Rust.Lit (Rust.LitByteStr (name' ++ "\NUL")))
}
builtinSymbols =
[ ("__builtin_bswap" ++ show w, Result
[ (c, Result
{ resultType = IsFunc (IsInt Unsigned (BitWidth w))
[(Nothing, IsInt Unsigned (BitWidth w))] False
, resultMutable = Rust.Immutable
, result = Rust.Path (Rust.PathSegments ["u" ++ show w, "swap_bytes"])
, result = Rust.Path (Rust.PathSegments ["u" ++ show w, rust])
})
| w <- [16, 32, 64]
| (c, rust, w) <- [
("__builtin_bswap16", "swap_bytes", 16),
("__builtin_bswap32", "swap_bytes", 32),
("__builtin_bswap64", "swap_bytes", 64),
("__builtin_clzll", "leading_zeros", 64),
("__builtin_ctzll", "trailing_zeros", 64),
("__builtin_popcountll", "count_ones", 64)
]
]
++
[ ("__FILE__", Result
Expand Down Expand Up @@ -2358,6 +2366,11 @@ on whether the pointer was to a mutable value.
, resultMutable = mut'
, result = Rust.Deref (result expr')
}
IsArray mut' _ ty' -> return Result
{ resultType = ty'
, resultMutable = mut'
, result = Rust.Index (result expr') 0
}
IsFunc{} -> return expr'
_ -> badSource node "dereference of non-pointer"
```
Expand Down Expand Up @@ -2960,6 +2973,18 @@ better because it preserves more of the programmer's intent.
```haskell
interpretConstExpr :: CExpr -> EnvMonad s Integer
interpretConstExpr (CConst (CIntConst (CInteger v _ _) _)) = return v
interpretConstExpr (CBinary CAddOp lhs rhs _) = do
lhs' <- interpretConstExpr lhs
rhs' <- interpretConstExpr rhs
return (lhs' + rhs')
interpretConstExpr (CBinary CMulOp lhs rhs _) = do
lhs' <- interpretConstExpr lhs
rhs' <- interpretConstExpr rhs
return (lhs' * rhs')
interpretConstExpr (CBinary CShlOp lhs rhs _) = do
lhs' <- interpretConstExpr lhs
rhs' <- interpretConstExpr rhs
return (lhs' * 2^rhs')
interpretConstExpr expr = unimplemented expr
```

Expand Down