103 lines
4 KiB
Haskell
103 lines
4 KiB
Haskell
module Backend.Compiler (Compiler (Compiler), Optimization (Zero, One, Two, Three, Size, SizeSize)) where
|
|
|
|
import Backend
|
|
import Data.Bifoldable (bimapM_)
|
|
import Data.List (singleton)
|
|
import qualified IR
|
|
import System.IO (Handle, hClose, hFlush, hPutStr, openTempFile, stdout)
|
|
import System.Posix (getProcessID, rename)
|
|
import System.Process (callProcess)
|
|
|
|
type File = String
|
|
type Compile = Bool
|
|
data Optimization = Zero | One | Two | Three | Size | SizeSize deriving (Eq, Show)
|
|
|
|
data Step = Opt | Llc | LdLLd
|
|
|
|
getFlag :: Step -> Optimization -> String
|
|
getFlag step = case (step) of
|
|
Opt -> getFlagOpt
|
|
Llc -> getFlagLlc
|
|
LdLLd -> getFlagLdLLd
|
|
where
|
|
getFlagOpt level = case (level) of
|
|
Zero -> "--O0"
|
|
One -> "--O1"
|
|
Two -> "--O2"
|
|
Three -> "--O3"
|
|
Size -> "--Os"
|
|
SizeSize -> "--Oz"
|
|
getFlagLlc level = case (level) of
|
|
Zero -> "-O0"
|
|
One -> "-O1"
|
|
Two -> "-O2"
|
|
Three -> "-O3"
|
|
Size -> "-O2"
|
|
SizeSize -> "-O2"
|
|
getFlagLdLLd level = case (level) of
|
|
Zero -> "-O0"
|
|
One -> "-O1"
|
|
Two -> "-O2"
|
|
Three -> "-O2"
|
|
Size -> "-O2"
|
|
SizeSize -> "-O2"
|
|
|
|
data Compiler = Compiler File Compile Optimization
|
|
|
|
instance BFExecutor Compiler where
|
|
exec c = execStr c . singleton
|
|
execStr (Compiler out comp optLevel) code =
|
|
print out
|
|
>> ( openTempFile "./" (out ++ ".ll")
|
|
>>> (\(_, f) -> return f >>> append IR.setup >>> append IR.main_start >>> compile code >>> append IR.main_end >>> hFlush)
|
|
>>= bimapM_
|
|
( if (comp)
|
|
then (\f -> opt f >>= llc >>= ldLld >>= flip rename out)
|
|
else flip rename out
|
|
)
|
|
hClose
|
|
)
|
|
>> return (Compiler out comp optLevel)
|
|
where
|
|
opt unopt = file >>= (\r -> callProcess "opt" [getFlag Opt optLevel, "-o", r, "-S", unopt]) >> file
|
|
where
|
|
file = getProcessID >>= (\r -> return $ "./" ++ out ++ (show r) ++ ".opt")
|
|
llc uncomp = file >>= (\r -> callProcess "llc" [getFlag Llc optLevel, "-o", r, uncomp, "--filetype=obj"]) >> file
|
|
where
|
|
file = getProcessID >>= (\r -> return $ "./" ++ out ++ (show r) ++ ".o")
|
|
ldLld unlinked = file >>= (\r -> callProcess "ld.lld" [getFlag Llc optLevel, "-o", r, unlinked]) >> file
|
|
where
|
|
file = getProcessID >>= (\r -> return $ "./" ++ out ++ (show r))
|
|
|
|
compile :: String -> Handle -> IO ()
|
|
compile s h = putStr s >> (() <$ _compile s (1, []) h)
|
|
|
|
(>>>) :: (Monad m) => m a -> (a -> m b) -> m a
|
|
v >>> f = v >>= \x -> f x >> return x
|
|
|
|
-- +[-]
|
|
-- append val_inc
|
|
-- append paren_opening 1
|
|
-- append val_dec
|
|
-- append paren_close 1
|
|
_compile :: String -> (Int, [Int]) -> Handle -> IO String
|
|
_compile (x : xs) loop_counter file =
|
|
putStr [x]
|
|
>> case (x) of
|
|
'>' -> use file >>> append IR.index_inc >>= _compile xs loop_counter
|
|
'<' -> use file >>> append IR.index_dec >>= _compile xs loop_counter
|
|
'+' -> use file >>> append IR.val_inc >>= _compile xs loop_counter
|
|
'-' -> use file >>> append IR.val_dec >>= _compile xs loop_counter
|
|
'.' -> use file >>> append IR.point >>= _compile xs loop_counter
|
|
',' -> use file >>> append IR.comma >>= _compile xs loop_counter
|
|
'[' -> use file >>> append (IR.paren_opening (fst loop_counter)) >>= _compile xs (fst loop_counter + 1, [fst loop_counter] ++ snd loop_counter)
|
|
']' -> use file >>> append (IR.paren_close (_head $ snd loop_counter)) >>= _compile xs (fst loop_counter, _drop 1 $ snd loop_counter)
|
|
_ -> _compile xs loop_counter file
|
|
where
|
|
use = return
|
|
_head a = if (length a > 0) then a !! 0 else error "unmatched closed parenthesis"
|
|
_drop n a = if (length a > 0) then drop n a else error "unmatched closed parenthesis"
|
|
_compile [] _ _ = return ""
|
|
|
|
append :: String -> Handle -> IO ()
|
|
append = flip hPutStr
|