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
|
||||||
|
|
@ -89,26 +92,22 @@ 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
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ module Codegen.LlvmIr (
|
||||||
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"
|
||||||
|
|
@ -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,7 +77,7 @@ 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 = "%"
|
||||||
|
|
@ -92,7 +92,7 @@ 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
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue