| 1 |
(* c-types.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 1999 Bell Labs, Lucent Technologies |
| 4 |
* |
| 5 |
* A representation of C Types for specifying the arguments and results |
| 6 |
* of C function calls. |
| 7 |
*) |
| 8 |
|
| 9 |
|
| 10 |
structure CTypes = |
| 11 |
struct |
| 12 |
|
| 13 |
datatype c_type |
| 14 |
= C_void |
| 15 |
| C_float |
| 16 |
| C_double |
| 17 |
| C_long_double |
| 18 |
| C_unsigned of c_int |
| 19 |
| C_signed of c_int |
| 20 |
| C_PTR |
| 21 |
| C_ARRAY of (c_type * int) |
| 22 |
| C_STRUCT of c_type list |
| 23 |
| C_UNION of c_type list |
| 24 |
|
| 25 |
and c_int |
| 26 |
= I_char |
| 27 |
| I_short |
| 28 |
| I_int |
| 29 |
| I_long |
| 30 |
| I_long_long |
| 31 |
|
| 32 |
(* multiple calling conventions on a single architecture *) |
| 33 |
type calling_convention = string |
| 34 |
|
| 35 |
(* prototype describing C function *) |
| 36 |
type c_proto = { |
| 37 |
conv : calling_convention, |
| 38 |
retTy : c_type, |
| 39 |
paramTys : c_type list |
| 40 |
} |
| 41 |
|
| 42 |
(* eliminate aggregates in a C type *) |
| 43 |
fun flattenCTy cTy = (case cTy |
| 44 |
of (C_STRUCT cTys | |
| 45 |
C_UNION cTys ) => List.concat (List.map flattenCTy cTys) |
| 46 |
| C_ARRAY (cTy, n) => List.tabulate (n, fn _ => cTy) |
| 47 |
| cTy => [cTy]) |
| 48 |
|
| 49 |
(* conversions to strings *) |
| 50 |
local |
| 51 |
|
| 52 |
fun ci I_char = "char" |
| 53 |
| ci I_short = "short" |
| 54 |
| ci I_int = "int" |
| 55 |
| ci I_long = "long" |
| 56 |
| ci I_long_long = "long long" |
| 57 |
|
| 58 |
fun ct C_void = "void" |
| 59 |
| ct C_float = "float" |
| 60 |
| ct C_double = "double" |
| 61 |
| ct C_long_double = "long double" |
| 62 |
| ct (C_unsigned i) = "unsigned " ^ ci i |
| 63 |
| ct (C_signed i) = ci i |
| 64 |
| ct C_PTR = "T*" |
| 65 |
| ct (C_ARRAY(t,i)) = concat [ct t, "[", Int.toString i, "]"] |
| 66 |
| ct (C_STRUCT fl) = |
| 67 |
concat ("s{" :: foldr (fn (f, l) => ct f :: ";" :: l) ["}"] fl) |
| 68 |
| ct (C_UNION fl) = |
| 69 |
concat ("u{" :: foldr (fn (f, l) => ct f :: ";" :: l) ["}"] fl) |
| 70 |
in |
| 71 |
val toString = ct |
| 72 |
|
| 73 |
fun protoToString { conv, retTy, paramTys = a1 :: an } = |
| 74 |
concat (ct retTy :: "(*)(" :: ct a1 :: |
| 75 |
foldr (fn (a, l) => "," :: ct a :: l) [")"] an) |
| 76 |
| protoToString { conv, retTy, paramTys = [] } = ct retTy ^ "(*)(void)" |
| 77 |
end (* local *) |
| 78 |
|
| 79 |
end |