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

Share more code between copilot-bluespec and copilot-c99 #3

Open
RyanGlScott opened this issue Feb 8, 2024 · 0 comments
Open

Share more code between copilot-bluespec and copilot-c99 #3

RyanGlScott opened this issue Feb 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@RyanGlScott
Copy link
Collaborator

RyanGlScott commented Feb 8, 2024

Currently, copilot-bluespec has a fair bit of code that is copy-pasted from copilot-c99:

  • Almost all of the code in Copilot.Compile.Bluespec.External is taken directly from Copilot.Compile.C99.External.

  • This code in Copilot.Compile.Bluespec.Name:

    -- | Turn a stream id into a suitable Bluespec variable name.
    streamName :: Id -> String
    streamName sId = "s" ++ show sId
    -- | Turn a stream id into the global varname for indices.
    indexName :: Id -> String
    indexName sId = streamName sId ++ "_idx"
    -- | Turn a stream id into the name of its accessor function
    streamAccessorName :: Id -> String
    streamAccessorName sId = streamName sId ++ "_get"
    -- | Turn stream id into name of its generator function.
    generatorName :: Id -> String
    generatorName sId = streamName sId ++ "_gen"
    -- | Turn the name of a trigger into a guard generator.
    guardName :: String -> String
    guardName name = lowercaseName name ++ "_guard"
    -- | Turn a trigger name into a an trigger argument name.
    argName :: String -> Int -> String
    argName name n = lowercaseName name ++ "_arg" ++ show n
    -- | Enumerate all argument names based on trigger name.
    argNames :: String -> [String]
    argNames base = map (argName base) [0..]

    Is taken from Copilot.Compile.C99.Name.

  • This code in Copilot.Compile.Bluespec.Compile:

    -- ** Obtain information from Copilot Core Exprs and Types.
    -- | List all types of an expression, returns items uniquely.
    exprTypes :: Typeable a => Expr a -> [UType]
    exprTypes e = case e of
    Const ty _ -> typeTypes ty
    Local ty1 ty2 _ e1 e2 -> typeTypes ty1 `union` typeTypes ty2
    `union` exprTypes e1 `union` exprTypes e2
    Var ty _ -> typeTypes ty
    Drop ty _ _ -> typeTypes ty
    ExternVar ty _ _ -> typeTypes ty
    Op1 _ e1 -> exprTypes e1
    Op2 _ e1 e2 -> exprTypes e1 `union` exprTypes e2
    Op3 _ e1 e2 e3 -> exprTypes e1 `union` exprTypes e2
    `union` exprTypes e3
    Label ty _ _ -> typeTypes ty
    -- | List all types of a type, returns items uniquely.
    typeTypes :: Typeable a => Type a -> [UType]
    typeTypes ty = case ty of
    Array ty' -> typeTypes ty' `union` [UType ty]
    Struct x -> concatMap (\(Value ty' _) -> typeTypes ty') (toValues x)
    `union` [UType ty]
    _ -> [UType ty]
    -- | Collect all expression of a list of streams and triggers and wrap them
    -- into an UEXpr.
    gatherExprs :: [Stream] -> [Trigger] -> [UExpr]
    gatherExprs streams triggers = map streamUExpr streams
    ++ concatMap triggerUExpr triggers
    where
    streamUExpr (Stream _ _ expr ty) = UExpr ty expr
    triggerUExpr (Trigger _ guard args) = UExpr Bool guard : args

    Is taken from Copilot.Compile.C99.Compile.

  • Much of the infrastructure in the unit tests in each package.

Ideally, we could find some way to share this code in between the two libraries to avoid code duplication. One complication is that most of this code lives in modules that aren't exported. Perhaps it would make sense to create a new copilot-backend library that factors out the code shared in common?

@RyanGlScott RyanGlScott added the enhancement New feature or request label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant