packages feed

cao-0.1: src/Main/Flags.hs

{-# LANGUAGE DeriveDataTypeable #-}
{-# OPTIONS_GHC -fno-cse #-}
{-
Module      :  $Header$
Description :  Compilation flags
Copyright   :  (c) SMART Team / HASLab
License     :  GPL

Maintainer  :  Paulo Silva <paufil@di.uminho.pt>
Stability   :  experimental
Portability :  non-portable

-}

module Main.Flags
    ( Options(..)
    , RunMode(..)
    , getOpts
    , printHelp
    ) where

import System.Console.CmdArgs
import System.Environment

data RunMode
    = CAO
    | CAO_Strict
    | CALF
    | CALF_Strict
    deriving (Show, Data, Typeable)

data Options
    = Comp  { 
          input        :: FilePath
        , dependent    :: Bool
        , verbose      :: Bool
        , output       :: Maybe FilePath
        , config       :: FilePath
        , runmode      :: RunMode
  
        , ddump_tc     :: Bool
        , ddump_simpl  :: Bool
        , ddump_eval   :: Bool
        , ddump_expand :: Bool
        , ddump_opt    :: Bool
        , ddump_target :: Bool
        , ddump_prec   :: Bool
        , dgen_cfg     :: Maybe String
        , dgen_ssa     :: Maybe String
        , dcheck       :: Bool
 
        , fexpand      :: Bool
        , optimize     :: Bool
        , findist_fun  :: Maybe (String, String)
        }
    | Help
    deriving (Show, Data, Typeable)

comp  :: Options
comp  = Comp { 
      input        = def &= args &= typ "FILE"
    , dependent    = def &= help "Turns on the dependent type checking"
    , runmode      = enum [ CALF        &= help "Normal mode"
                          , CALF_Strict &= help "Access checking"
                          , CAO         &= help "Type checking without symbolic constants"
                          , CAO_Strict  &= help "Type checking without symbolic constants (with access checking)"]
                         &= help "Running mode"
    , verbose      = def &= help "Give verbose output"
    , output       = def &= typ "FILE" &= help "Output file"
    , config       = "default.plat" &= typ "FILE" &= help "Config file"
    , ddump_tc     = def &= help "Dump type-checked code"
                         &= groupname "Debugging"
    , ddump_simpl  = def &= help "Dump simplified code"
                         &= groupname "Debugging"
    , ddump_eval   = def &= help "Dump code with evaluated expressions"
                         &= groupname "Debugging"
    , ddump_expand = def &= help "Dump code after seq expansion"
                         &= groupname "Debugging"
    , ddump_opt    = def &= help "Dump code after optimization passes"
                         &= groupname "Debugging"
    , ddump_target = def &= help "Dump code after targeting it to a given platform"
                         &= groupname "Debugging"
    , ddump_prec   = def &= help "Dump code after preprocessing to a C translation"
                         &= groupname "Debugging"
    , dgen_cfg     = def &= opt "pdf"
                         &= typ "FORMAT"
                         &= help "Generate Cao CFG in the specified\
                                      \ output format (Must be supported\
                                          \ by graphviz 'dot'!. Default = pdf)"
                         &= groupname "Debugging"
    , dgen_ssa     = def &= opt "pdf"
                         &= typ "FORMAT"
                         &= help "Generate Cao CFG in SSA in the\
                                      \ output format (Must be supported\
                                          \ by graphviz 'dot'!. Default = pdf)"
                         &= groupname "Debugging"
    , dcheck       = def &= help "Type check dumped code"
                         &= groupname "Debugging"

    , fexpand      = def &= explicit
                         &= name "fexpand"
                         &= help "Expand seqs"
                         &= groupname "Optimization options"
    , optimize     = def &= explicit
                         &= name "O"
                         &= help "Run optimization passes"
                         &= groupname "Optimization options"
    , findist_fun  = def &= explicit
                         &= name "findist-fun"
                         &= typ "FUN,FUN"
                         &= help "Apply side channel countermeasures\
                                     \ to two function definitions."
                         &= groupname "Optimization options"
    }
    &= help "CAO to C compiler"

chelp :: Options
chelp = Help
     &= help "Display help about CAO modes"

mode  :: Mode (CmdArgs Options)
mode  = cmdArgsMode $
           modes [comp, chelp]
        &= help "CAO compiler infraestructure"
        &= summary "cao v0.1 \n\
                   \(C) SMART Team 2011 - DI/HasLab - Univ. Minho,\
                   \ Braga, Portugal"

printHelp :: IO ()
printHelp = withArgs ["--help"] $ cmdArgsRun mode >> return ()

getOpts :: IO Options
getOpts = getArgs >>= doGetOpts
    where 
    doGetOpts as
        | null as   = withArgs ["help"] $ cmdArgsRun mode
        | otherwise = cmdArgsRun mode