Added a variable lookup.
This commit is contained in:
parent
5a70286802
commit
cca2f853ea
2 changed files with 33 additions and 33 deletions
|
|
@ -13,6 +13,8 @@ import Data.Coerce (coerce)
|
||||||
import Data.Map (Map)
|
import Data.Map (Map)
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Maybe (fromJust, fromMaybe)
|
import Data.Maybe (fromJust, fromMaybe)
|
||||||
|
import Data.Set (Set)
|
||||||
|
import qualified Data.Set as Set
|
||||||
import Data.Tuple.Extra (dupe, first, second)
|
import Data.Tuple.Extra (dupe, first, second)
|
||||||
import Grammar.ErrM (Err)
|
import Grammar.ErrM (Err)
|
||||||
import Monomorphizer.MonomorphizerIr as MIR
|
import Monomorphizer.MonomorphizerIr as MIR
|
||||||
|
|
@ -22,6 +24,7 @@ import qualified TypeChecker.TypeCheckerIr as TIR
|
||||||
data CodeGenerator = CodeGenerator
|
data CodeGenerator = CodeGenerator
|
||||||
{ instructions :: [LLVMIr]
|
{ instructions :: [LLVMIr]
|
||||||
, functions :: Map MIR.Id FunctionInfo
|
, functions :: Map MIR.Id FunctionInfo
|
||||||
|
, customTypes :: Set LLVMType
|
||||||
, constructors :: Map TIR.Ident ConstructorInfo
|
, constructors :: Map TIR.Ident ConstructorInfo
|
||||||
, variableCount :: Integer
|
, variableCount :: Integer
|
||||||
, labelCount :: Integer
|
, labelCount :: Integer
|
||||||
|
|
@ -88,28 +91,24 @@ createArgs xs = fst $ foldl (\(acc, l) t -> (acc ++ [(TIR.Ident ("arg_" <> show
|
||||||
getConstructors :: [MIR.Def] -> Map TIR.Ident ConstructorInfo
|
getConstructors :: [MIR.Def] -> Map TIR.Ident ConstructorInfo
|
||||||
getConstructors bs = Map.fromList $ go bs
|
getConstructors bs = Map.fromList $ go bs
|
||||||
where
|
where
|
||||||
go [] = []
|
go [] = []
|
||||||
go (MIR.DData (MIR.Data t cons) : xs) =
|
go (MIR.DData (MIR.Data t cons) : xs) = fst
|
||||||
fst
|
(foldl (\(acc, i) (Inj id xs) ->
|
||||||
( foldl
|
(( id, ConstructorInfo
|
||||||
( \(acc, i) (Inj id xs) ->
|
|
||||||
( ( id
|
|
||||||
, ConstructorInfo
|
|
||||||
{ numArgsCI = length (init . flattenType $ xs)
|
{ numArgsCI = length (init . flattenType $ xs)
|
||||||
, argumentsCI = createArgs (init . flattenType $ xs)
|
, argumentsCI = createArgs (init . flattenType $ xs)
|
||||||
, numCI = i
|
, numCI = i
|
||||||
, returnTypeCI = t --last . flattenType $ xs
|
, returnTypeCI = t --last . flattenType $ xs
|
||||||
}
|
}
|
||||||
)
|
) : acc, i + 1)) ([], 0) cons) <> go xs
|
||||||
: acc
|
go (_ : xs) = go xs
|
||||||
, i + 1
|
|
||||||
)
|
getTypes :: [MIR.Def] -> Set LLVMType
|
||||||
)
|
getTypes bs = Set.fromList $ go bs
|
||||||
([], 0)
|
where
|
||||||
cons
|
go [] = []
|
||||||
)
|
go (MIR.DData (MIR.Data t _) : xs) = type2LlvmType t : go xs
|
||||||
<> go xs
|
go (_:xs) = go xs
|
||||||
go (_ : xs) = go xs
|
|
||||||
|
|
||||||
initCodeGenerator :: [MIR.Def] -> CodeGenerator
|
initCodeGenerator :: [MIR.Def] -> CodeGenerator
|
||||||
initCodeGenerator scs =
|
initCodeGenerator scs =
|
||||||
|
|
@ -117,6 +116,7 @@ initCodeGenerator scs =
|
||||||
{ instructions = defaultStart
|
{ instructions = defaultStart
|
||||||
, functions = getFunctions scs
|
, functions = getFunctions scs
|
||||||
, constructors = getConstructors scs
|
, constructors = getConstructors scs
|
||||||
|
, customTypes = getTypes scs
|
||||||
, variableCount = 0
|
, variableCount = 0
|
||||||
, labelCount = 0
|
, labelCount = 0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,15 @@ module Codegen.LlvmIr (
|
||||||
ToIr (..),
|
ToIr (..),
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.List (intercalate)
|
import Data.List (intercalate)
|
||||||
import TypeChecker.TypeCheckerIr (Ident (..))
|
import TypeChecker.TypeCheckerIr (Ident (..))
|
||||||
|
|
||||||
data CallingConvention = TailCC | FastCC | CCC | ColdCC deriving (Show)
|
data CallingConvention = TailCC | FastCC | CCC | ColdCC deriving (Show, Eq, Ord)
|
||||||
instance ToIr CallingConvention where
|
instance ToIr CallingConvention where
|
||||||
toIr :: CallingConvention -> String
|
toIr :: CallingConvention -> String
|
||||||
toIr TailCC = "tailcc"
|
toIr TailCC = "tailcc"
|
||||||
toIr FastCC = "fastcc"
|
toIr FastCC = "fastcc"
|
||||||
toIr CCC = "ccc"
|
toIr CCC = "ccc"
|
||||||
toIr ColdCC = "coldcc"
|
toIr ColdCC = "coldcc"
|
||||||
|
|
||||||
-- | A datatype which represents some basic LLVM types
|
-- | A datatype which represents some basic LLVM types
|
||||||
|
|
@ -33,7 +33,7 @@ data LLVMType
|
||||||
| Function LLVMType [LLVMType]
|
| Function LLVMType [LLVMType]
|
||||||
| Array Integer LLVMType
|
| Array Integer LLVMType
|
||||||
| CustomType Ident
|
| CustomType Ident
|
||||||
deriving (Show)
|
deriving (Show, Eq, Ord)
|
||||||
|
|
||||||
class ToIr a where
|
class ToIr a where
|
||||||
toIr :: a -> String
|
toIr :: a -> String
|
||||||
|
|
@ -62,7 +62,7 @@ data LLVMComp
|
||||||
| LLSge
|
| LLSge
|
||||||
| LLSlt
|
| LLSlt
|
||||||
| LLSle
|
| LLSle
|
||||||
deriving (Show)
|
deriving (Show, Eq, Ord)
|
||||||
instance ToIr LLVMComp where
|
instance ToIr LLVMComp where
|
||||||
toIr :: LLVMComp -> String
|
toIr :: LLVMComp -> String
|
||||||
toIr = \case
|
toIr = \case
|
||||||
|
|
@ -77,10 +77,10 @@ instance ToIr LLVMComp where
|
||||||
LLSlt -> "slt"
|
LLSlt -> "slt"
|
||||||
LLSle -> "sle"
|
LLSle -> "sle"
|
||||||
|
|
||||||
data Visibility = Local | Global deriving (Show)
|
data Visibility = Local | Global deriving (Show, Eq, Ord)
|
||||||
instance ToIr Visibility where
|
instance ToIr Visibility where
|
||||||
toIr :: Visibility -> String
|
toIr :: Visibility -> String
|
||||||
toIr Local = "%"
|
toIr Local = "%"
|
||||||
toIr Global = "@"
|
toIr Global = "@"
|
||||||
|
|
||||||
{- | Represents a LLVM "value", as in an integer, a register variable,
|
{- | Represents a LLVM "value", as in an integer, a register variable,
|
||||||
|
|
@ -92,16 +92,16 @@ data LLVMValue
|
||||||
| VIdent Ident LLVMType
|
| VIdent Ident LLVMType
|
||||||
| VConstant String
|
| VConstant String
|
||||||
| VFunction Ident Visibility LLVMType
|
| VFunction Ident Visibility LLVMType
|
||||||
deriving (Show)
|
deriving (Show, Eq, Ord)
|
||||||
|
|
||||||
instance ToIr LLVMValue where
|
instance ToIr LLVMValue where
|
||||||
toIr :: LLVMValue -> String
|
toIr :: LLVMValue -> String
|
||||||
toIr v = case v of
|
toIr v = case v of
|
||||||
VInteger i -> show i
|
VInteger i -> show i
|
||||||
VChar i -> show i
|
VChar i -> show i
|
||||||
VIdent (Ident n) _ -> "%" <> n
|
VIdent (Ident n) _ -> "%" <> n
|
||||||
VFunction (Ident n) vis _ -> toIr vis <> n
|
VFunction (Ident n) vis _ -> toIr vis <> n
|
||||||
VConstant s -> "c" <> show s
|
VConstant s -> "c" <> show s
|
||||||
|
|
||||||
type Params = [(Ident, LLVMType)]
|
type Params = [(Ident, LLVMType)]
|
||||||
type Args = [(LLVMType, LLVMValue)]
|
type Args = [(LLVMType, LLVMValue)]
|
||||||
|
|
@ -136,7 +136,7 @@ data LLVMIr
|
||||||
| Comment String
|
| Comment String
|
||||||
| UnsafeRaw String -- This should generally be avoided, and proper
|
| UnsafeRaw String -- This should generally be avoided, and proper
|
||||||
-- instructions should be used in its place
|
-- instructions should be used in its place
|
||||||
deriving (Show)
|
deriving (Show, Eq, Ord)
|
||||||
|
|
||||||
-- | Converts a list of LLVMIr instructions to a string
|
-- | Converts a list of LLVMIr instructions to a string
|
||||||
llvmIrToString :: [LLVMIr] -> String
|
llvmIrToString :: [LLVMIr] -> String
|
||||||
|
|
@ -146,9 +146,9 @@ llvmIrToString = go 0
|
||||||
go _ [] = mempty
|
go _ [] = mempty
|
||||||
go i (x : xs) = do
|
go i (x : xs) = do
|
||||||
let (i', n) = case x of
|
let (i', n) = case x of
|
||||||
Define{} -> (i + 1, 0)
|
Define{} -> (i + 1, 0)
|
||||||
DefineEnd -> (i - 1, 0)
|
DefineEnd -> (i - 1, 0)
|
||||||
_ -> (i, i)
|
_ -> (i, i)
|
||||||
insToString n x <> go i' xs
|
insToString n x <> go i' xs
|
||||||
|
|
||||||
-- \| Converts a LLVM inststruction to a String, allowing for printing etc.
|
-- \| Converts a LLVM inststruction to a String, allowing for printing etc.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue