Skip to content

Commit

Permalink
Merge pull request #55 from AndrasKovacs/raehik/fix-endianness-cpp
Browse files Browse the repository at this point in the history
Fix usage of `WORDS_BIGENDIAN` CPP macros
  • Loading branch information
AndrasKovacs authored Mar 15, 2024
2 parents d18401e + 086d7f6 commit d16e501
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 62 deletions.
37 changes: 20 additions & 17 deletions flake.lock

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

55 changes: 38 additions & 17 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
{
inputs = {
#nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
#flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
haskell-flake.url = "github:srid/haskell-flake";
};
outputs = inputs@{ nixpkgs, flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = nixpkgs.lib.systems.flakeExposed;
outputs = inputs:
let
# simple devshell for non-dev compilers: really just want `cabal repl`
nondevDevShell = compiler: {
mkShellArgs.name = "${compiler}-flatparse";
hoogle = false;
tools = _: {
hlint = null;
haskell-language-server = null;
ghcid = null;
};
};
in
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = inputs.nixpkgs.lib.systems.flakeExposed;
imports = [ inputs.haskell-flake.flakeModule ];
perSystem = { self', pkgs, ... }: {
haskellProjects.default = {
haskellPackages = pkgs.haskell.packages.ghc944;
#haskellPackages = pkgs.haskell.packages.ghc925;
#haskellPackages = pkgs.haskell.packages.ghc810;
packages = {
flatparse.root = ./.;
perSystem = { self', pkgs, config, ... }: {
packages.default = self'.packages.ghc96-flatparse;
devShells.default = self'.devShells.ghc96;
haskellProjects.ghc98 = {
# shouldn't work, pkgs aren't up to date and mine aren't 9.8 ready
basePackages = pkgs.haskell.packages.ghc98;
devShell = nondevDevShell "ghc98";
};
haskellProjects.ghc96 = {
basePackages = pkgs.haskell.packages.ghc96;
devShell.mkShellArgs.name = "ghc96-flatparse";
devShell.tools = _: {
haskell-language-server = null; # 2024-03-06: broken
};
# 2023-01-12 raehik: ghcid seems broken on nixpkgs GHC 9.4.{3,4}
buildTools = hp: { ghcid = null; hls = null; };
# overrides = self: super: { }
# hlintCheck.enable = true;
# hlsCheck.enable = true;
};
haskellProjects.ghc94 = {
basePackages = pkgs.haskell.packages.ghc94;
devShell = nondevDevShell "ghc94";
};
haskellProjects.ghc92 = {
basePackages = pkgs.haskell.packages.ghc92;
devShell = nondevDevShell "ghc92";
};
};
};
Expand Down
7 changes: 5 additions & 2 deletions src/FlatParse/Basic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ module FlatParse.Basic (

) where

-- for WORDS_BIGENDIAN
#include "MachDeps.h"

import FlatParse.Basic.Parser
import FlatParse.Basic.Base
import FlatParse.Basic.Integers
Expand Down Expand Up @@ -494,7 +497,7 @@ isolateToNextNull (ParserT p) = ParserT \fp eob s st -> go fp eob s st s
-- reading could probably segfault
go8 fp eob s0 st s
_ -> -- >= 8 bytes of input: use efficient 8-byte scanning
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
-- big-endian ("L->R"): find leftmost null byte
let !x@(I# x#) = Common.zbytel'intermediate (I# (indexIntOffAddr# s 0#)) in
#else
Expand All @@ -504,7 +507,7 @@ isolateToNextNull (ParserT p) = ParserT \fp eob s st -> go fp eob s st s
case x# ==# 0# of
1# -> go fp eob s0 st sWord -- no 0x00 in next word
_ -> -- 0x00 somewhere in next word
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
let !(I# nullIdx#) = Common.zbytel'toIdx x in
#else
let !(I# nullIdx#) = Common.zbyter'toIdx x in
Expand Down
27 changes: 15 additions & 12 deletions src/FlatParse/Basic/Integers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module FlatParse.Basic.Integers
, sizedUnsafe#
) where

-- for WORDS_BIGENDIAN
#include "MachDeps.h"

import FlatParse.Basic.Parser
import FlatParse.Basic.Base ( withEnsure# )

Expand Down Expand Up @@ -204,7 +207,7 @@ is inlined as a single @BSWAP@ instruction.

-- | Parse any 'Word16' (little-endian).
anyWord16le :: ParserT st e Word16
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyWord16le = withAnyWord16 (pure . byteSwap16)
#else
anyWord16le = anyWord16
Expand All @@ -213,7 +216,7 @@ anyWord16le = anyWord16

-- | Parse any 'Word16' (big-endian).
anyWord16be :: ParserT st e Word16
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyWord16be = anyWord16
#else
anyWord16be = withAnyWord16 (pure . byteSwap16)
Expand All @@ -222,7 +225,7 @@ anyWord16be = withAnyWord16 (pure . byteSwap16)

-- | Parse any 'Word32' (little-endian).
anyWord32le :: ParserT st e Word32
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyWord32le = withAnyWord32 (pure . byteSwap32)
#else
anyWord32le = anyWord32
Expand All @@ -231,7 +234,7 @@ anyWord32le = anyWord32

-- | Parse any 'Word32' (big-endian).
anyWord32be :: ParserT st e Word32
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyWord32be = anyWord32
#else
anyWord32be = withAnyWord32 (pure . byteSwap32)
Expand All @@ -240,7 +243,7 @@ anyWord32be = withAnyWord32 (pure . byteSwap32)

-- | Parse any 'Word64' (little-endian).
anyWord64le :: ParserT st e Word64
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyWord64le = withAnyWord64 (pure . byteSwap64)
#else
anyWord64le = anyWord64
Expand All @@ -249,7 +252,7 @@ anyWord64le = anyWord64

-- | Parse any 'Word64' (big-endian).
anyWord64be :: ParserT st e Word64
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyWord64be = anyWord64
#else
anyWord64be = withAnyWord64 (pure . byteSwap64)
Expand All @@ -258,7 +261,7 @@ anyWord64be = withAnyWord64 (pure . byteSwap64)

-- | Parse any 'Int16' (little-endian).
anyInt16le :: ParserT st e Int16
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyInt16le = withAnyWord16 (pure . word16ToInt16 . byteSwap16)
#else
anyInt16le = anyInt16
Expand All @@ -267,7 +270,7 @@ anyInt16le = anyInt16

-- | Parse any 'Int16' (big-endian).
anyInt16be :: ParserT st e Int16
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyInt16be = anyInt16
#else
anyInt16be = withAnyWord16 (pure . word16ToInt16 . byteSwap16)
Expand All @@ -276,7 +279,7 @@ anyInt16be = withAnyWord16 (pure . word16ToInt16 . byteSwap16)

-- | Parse any 'Int32' (little-endian).
anyInt32le :: ParserT st e Int32
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyInt32le = withAnyWord32 (pure . word32ToInt32 . byteSwap32)
#else
anyInt32le = anyInt32
Expand All @@ -285,7 +288,7 @@ anyInt32le = anyInt32

-- | Parse any 'Int32' (big-endian).
anyInt32be :: ParserT st e Int32
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyInt32be = anyInt32
#else
anyInt32be = withAnyWord32 (pure . word32ToInt32 . byteSwap32)
Expand All @@ -294,7 +297,7 @@ anyInt32be = withAnyWord32 (pure . word32ToInt32 . byteSwap32)

-- | Parse any 'Int64' (little-endian).
anyInt64le :: ParserT st e Int64
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyInt64le = withAnyWord64 (pure . word64ToInt64 . byteSwap64)
#else
anyInt64le = anyInt64
Expand All @@ -303,7 +306,7 @@ anyInt64le = anyInt64

-- | Parse any 'Int64' (big-endian).
anyInt64be :: ParserT st e Int64
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
anyInt64be = anyInt64
#else
anyInt64be = withAnyWord64 (pure . word64ToInt64 . byteSwap64)
Expand Down
6 changes: 4 additions & 2 deletions src/FlatParse/Stateful.hs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ module FlatParse.Stateful (

) where

-- for WORDS_BIGENDIAN
#include "MachDeps.h"

import qualified FlatParse.Basic as Basic
import FlatParse.Stateful.Parser
Expand Down Expand Up @@ -472,7 +474,7 @@ isolateToNextNull (ParserT p) = ParserT \fp !r eob s n st -> go fp r n eob s st
-- reading could probably segfault
go8 fp r n eob s0 st s
_ -> -- >= 8 bytes of input: use efficient 8-byte scanning
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
-- big-endian ("L->R"): find leftmost null byte
let !x@(I# x#) = Common.zbytel'intermediate (I# (indexIntOffAddr# s 0#)) in
#else
Expand All @@ -482,7 +484,7 @@ isolateToNextNull (ParserT p) = ParserT \fp !r eob s n st -> go fp r n eob s st
case x# ==# 0# of
1# -> go fp r n eob s0 st sWord -- no 0x00 in next word
_ -> -- 0x00 somewhere in next word
#ifdef WORDS_BIGENDIAN
#if defined(WORDS_BIGENDIAN)
let !(I# nullIdx#) = Common.zbytel'toIdx x in
#else
let !(I# nullIdx#) = Common.zbyter'toIdx x in
Expand Down
Loading

0 comments on commit d16e501

Please sign in to comment.