Skip to content

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

@RyanGlScott

Description

@RyanGlScott

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions