Added a variable lookup.

This commit is contained in:
Samuel Hammersberg 2023-03-28 15:36:08 +02:00
parent 5a70286802
commit cca2f853ea
2 changed files with 33 additions and 33 deletions

View file

@ -11,15 +11,15 @@ module Codegen.LlvmIr (
ToIr (..),
) where
import Data.List (intercalate)
import TypeChecker.TypeCheckerIr (Ident (..))
import Data.List (intercalate)
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
toIr :: CallingConvention -> String
toIr TailCC = "tailcc"
toIr FastCC = "fastcc"
toIr CCC = "ccc"
toIr CCC = "ccc"
toIr ColdCC = "coldcc"
-- | A datatype which represents some basic LLVM types
@ -33,7 +33,7 @@ data LLVMType
| Function LLVMType [LLVMType]
| Array Integer LLVMType
| CustomType Ident
deriving (Show)
deriving (Show, Eq, Ord)
class ToIr a where
toIr :: a -> String
@ -62,7 +62,7 @@ data LLVMComp
| LLSge
| LLSlt
| LLSle
deriving (Show)
deriving (Show, Eq, Ord)
instance ToIr LLVMComp where
toIr :: LLVMComp -> String
toIr = \case
@ -77,10 +77,10 @@ instance ToIr LLVMComp where
LLSlt -> "slt"
LLSle -> "sle"
data Visibility = Local | Global deriving (Show)
data Visibility = Local | Global deriving (Show, Eq, Ord)
instance ToIr Visibility where
toIr :: Visibility -> String
toIr Local = "%"
toIr Local = "%"
toIr Global = "@"
{- | Represents a LLVM "value", as in an integer, a register variable,
@ -92,16 +92,16 @@ data LLVMValue
| VIdent Ident LLVMType
| VConstant String
| VFunction Ident Visibility LLVMType
deriving (Show)
deriving (Show, Eq, Ord)
instance ToIr LLVMValue where
toIr :: LLVMValue -> String
toIr v = case v of
VInteger i -> show i
VChar i -> show i
VIdent (Ident n) _ -> "%" <> n
VInteger i -> show i
VChar i -> show i
VIdent (Ident n) _ -> "%" <> n
VFunction (Ident n) vis _ -> toIr vis <> n
VConstant s -> "c" <> show s
VConstant s -> "c" <> show s
type Params = [(Ident, LLVMType)]
type Args = [(LLVMType, LLVMValue)]
@ -136,7 +136,7 @@ data LLVMIr
| Comment String
| UnsafeRaw String -- This should generally be avoided, and proper
-- instructions should be used in its place
deriving (Show)
deriving (Show, Eq, Ord)
-- | Converts a list of LLVMIr instructions to a string
llvmIrToString :: [LLVMIr] -> String
@ -146,9 +146,9 @@ llvmIrToString = go 0
go _ [] = mempty
go i (x : xs) = do
let (i', n) = case x of
Define{} -> (i + 1, 0)
Define{} -> (i + 1, 0)
DefineEnd -> (i - 1, 0)
_ -> (i, i)
_ -> (i, i)
insToString n x <> go i' xs
-- \| Converts a LLVM inststruction to a String, allowing for printing etc.