Skip to content

Commit 2d2995f

Browse files
committed
Add support for 32-bit architectures, JSOO in particular
Extend the value representation to accomodate 32-bit architectures. The additional values will not bit used on a 64-bit architecture. Signed-off-by: Christian Lindig <[email protected]>
1 parent 27b48f2 commit 2d2995f

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

bin/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(executable
22
(name main)
33
(public_name fit)
4+
(modes native)
45
(modules build main)
56
(libraries yojson cmdliner fit))
67

fit.opam.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22
x-maintenance-intent: ["(latest)"]
3-
available: [os-family != "windows" & arch != "arm32" & arch != "x86_32"]
3+
available: [os-family != "windows" ]

lib/fit.ml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
open Angstrom
55

66
let defer finally = Fun.protect ~finally
7+
let arch32 = match Sys.word_size with 32 -> true | _ -> false
78

89
(** redefine & as bitwise operation, && is still logical and *)
910
let ( & ) = Int.logand
@@ -138,6 +139,8 @@ type value =
138139
| Enum of int
139140
| String of string
140141
| Int of int
142+
| Int32 of Int32.t
143+
| Int64 of Int64.t
141144
| Float of float
142145
| Unknown
143146

@@ -146,6 +149,8 @@ let to_string value =
146149
| Enum d -> sprintf "enum(%d)" d
147150
| String s -> sprintf "string(%s)" s
148151
| Int i -> sprintf "int(%d)" i
152+
| Int32 i -> sprintf "int(%ld)" i
153+
| Int64 i -> sprintf "int(%Ld)" i
149154
| Float f -> sprintf "float(%f)" f
150155
| Unknown -> "unknown"
151156

@@ -163,6 +168,7 @@ type t = { header : header; records : record list }
163168
[arch] is known as well *)
164169
let base arch ty =
165170
let ff = -1 in
171+
let ff' = Int32.minus_one in
166172
let float = function
167173
| x when Float.is_nan x -> return Unknown
168174
| x when x = Float.infinity -> return Unknown
@@ -179,12 +185,16 @@ let base arch ty =
179185
return (if x = unk then Unknown else Int x)
180186
in
181187
let int32 ukn x =
182-
let x = Int32.to_int x in
183-
return (if x = ukn then Unknown else Int x)
188+
match arch32 with
189+
| _ when x = ukn -> return Unknown
190+
| true -> return (Int32 x)
191+
| false -> return (Int (Int32.to_int x))
184192
in
185193
let uint32 unk x =
186-
let x = Int32.to_int x land 0xffff_ffff in
187-
return (if x = unk then Unknown else Int x)
194+
match arch32 with
195+
| _ when x = unk -> return Unknown
196+
| true -> return (Int64 Int64.(of_int32 x |> logand 0xFFFF_FFFF_L))
197+
| false -> return (Int Int32.(to_int x land 0xffff_ffff))
188198
in
189199
let value =
190200
match (arch, ty.Type.ty) with
@@ -194,18 +204,18 @@ let base arch ty =
194204
| __, Type.Int (Signed, 8, FF) -> any_int8 >>= int ff
195205
| BE, Type.Int (Signed, 16, FF) -> BE.any_int16 >>= int ff
196206
| LE, Type.Int (Signed, 16, FF) -> LE.any_int16 >>= int ff
197-
| BE, Type.Int (Signed, 32, FF) -> BE.any_int32 >>= int32 ff
198-
| LE, Type.Int (Signed, 32, FF) -> LE.any_int32 >>= int32 ff
207+
| BE, Type.Int (Signed, 32, FF) -> BE.any_int32 >>= int32 ff'
208+
| LE, Type.Int (Signed, 32, FF) -> LE.any_int32 >>= int32 ff'
199209
| __, Type.Int (Unsigned, 8, ZZ) -> any_uint8 >>= uint8 0
200210
| LE, Type.Int (Unsigned, 16, ZZ) -> LE.any_uint16 >>= uint16 0
201211
| BE, Type.Int (Unsigned, 16, ZZ) -> BE.any_uint16 >>= uint16 0
202-
| LE, Type.Int (Unsigned, 32, ZZ) -> LE.any_int32 >>= uint32 0
203-
| BE, Type.Int (Unsigned, 32, ZZ) -> BE.any_int32 >>= uint32 0
212+
| LE, Type.Int (Unsigned, 32, ZZ) -> LE.any_int32 >>= uint32 0l
213+
| BE, Type.Int (Unsigned, 32, ZZ) -> BE.any_int32 >>= uint32 0l
204214
| __, Type.Int (Unsigned, 8, FF) -> any_uint8 >>= uint8 0xff
205215
| LE, Type.Int (Unsigned, 16, FF) -> LE.any_uint16 >>= uint16 0xffff
206216
| BE, Type.Int (Unsigned, 16, FF) -> BE.any_uint16 >>= uint16 0xffff
207-
| LE, Type.Int (Unsigned, 32, FF) -> LE.any_int32 >>= uint32 0xffff_ffff
208-
| BE, Type.Int (Unsigned, 32, FF) -> BE.any_int32 >>= uint32 0xffff_ffff
217+
| LE, Type.Int (Unsigned, 32, FF) -> LE.any_int32 >>= uint32 0xffff_ffff_l
218+
| BE, Type.Int (Unsigned, 32, FF) -> BE.any_int32 >>= uint32 0xffff_ffff_l
209219
| BE, Type.Float 32 -> BE.any_float >>= float
210220
| LE, Type.Float 32 -> LE.any_float >>= float
211221
| BE, Type.Float 64 -> BE.any_double >>= float
@@ -426,6 +436,8 @@ module JSON = struct
426436
| _, _, Enum n -> (string_of_int pos, `Int n)
427437
| _, _, String s -> (string_of_int pos, `String s)
428438
| _, _, Int i -> (string_of_int pos, `Int i)
439+
| _, _, Int32 i -> (string_of_int pos, `Float (Int32.to_float i))
440+
| _, _, Int64 i -> (string_of_int pos, `Float (Int64.to_float i))
429441
| _, _, Float f when Float.is_nan f -> (string_of_int pos, `Null)
430442
| _, _, Float f -> (string_of_int pos, `Float f)
431443
| _, _, Unknown -> (string_of_int pos, `Null)

lib/fit.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type value =
1313
| Enum of int
1414
| String of string
1515
| Int of int
16+
| Int32 of Int32.t
17+
| Int64 of Int64.t
1618
| Float of float
1719
| Unknown
1820

0 commit comments

Comments
 (0)