Added a Malloc instruction.

This commit is contained in:
Samuel Hammersberg 2023-03-28 17:47:43 +02:00
parent 230a205965
commit c77139dfa8
3 changed files with 27 additions and 19 deletions

View file

@ -114,8 +114,7 @@ data LLVMIr
| Declare LLVMType Ident Params | Declare LLVMType Ident Params
| SetVariable Ident LLVMIr | SetVariable Ident LLVMIr
| Variable Ident | Variable Ident
| -- extractvalue <aggregate type> <val>, <idx>{, <idx>}* | ExtractValue LLVMType LLVMValue Integer
ExtractValue LLVMType LLVMValue Integer
| GetElementPtr LLVMType LLVMType LLVMValue LLVMType LLVMValue LLVMType LLVMValue | GetElementPtr LLVMType LLVMType LLVMValue LLVMType LLVMValue LLVMType LLVMValue
| GetElementPtrInbounds LLVMType LLVMType LLVMValue LLVMType LLVMValue LLVMType LLVMValue | GetElementPtrInbounds LLVMType LLVMType LLVMValue LLVMType LLVMValue LLVMType LLVMValue
| Add LLVMType LLVMValue LLVMValue | Add LLVMType LLVMValue LLVMValue
@ -134,6 +133,7 @@ data LLVMIr
| Bitcast LLVMType LLVMValue LLVMType | Bitcast LLVMType LLVMValue LLVMType
| Ret LLVMType LLVMValue | Ret LLVMType LLVMValue
| Comment String | Comment String
| Malloca Integer
| 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, Eq, Ord) deriving (Show, Eq, Ord)
@ -223,6 +223,9 @@ llvmIrToString = go 0
, ")\n" , ")\n"
] ]
(Alloca t) -> unwords ["alloca", toIr t, "\n"] (Alloca t) -> unwords ["alloca", toIr t, "\n"]
(Malloca t) ->
concat
[ "call ptr @malloc(i32 ", show t, ")"]
(Store t1 val t2 (Ident id2)) -> (Store t1 val t2 (Ident id2)) ->
concat concat
[ "store ", toIr t1, " ", toIr val [ "store ", toIr t1, " ", toIr val

View file

@ -1,6 +0,0 @@
main : _Int ;
main = double 3 ;
double : _Int -> _Int ;
double x = x + x ;

View file

@ -1,2 +1,13 @@
main = const 1 2 ; id x = x;
const x y = x ; const x y = x ;
data Maybe () where {
Just : Int -> Maybe ()
Nothing : Maybe ()
};
main = case (Just 5) of {
Just a => 10 ;
Nothing => 0 ;
}; --const (id 0) (id 'a') ;