From d36370329e64df702f5a26c143b7f6dd83ac8f1f Mon Sep 17 00:00:00 2001 From: Samuel Hammersberg Date: Wed, 22 Mar 2023 10:24:00 +0100 Subject: [PATCH] Realized that getelementptr might be doing to right thing, and that the uninitialized data comes from padding. --- src/Codegen/Codegen.hs | 23 +++++++++++++---------- src/Codegen/LlvmIr.hs | 7 +++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Codegen/Codegen.hs b/src/Codegen/Codegen.hs index 750ae5d..2a0299e 100644 --- a/src/Codegen/Codegen.hs +++ b/src/Codegen/Codegen.hs @@ -151,13 +151,14 @@ compileScs [] = do -- set the first byte to the index of the constructor emit $ SetVariable ptr $ - GetElementPtrInbounds t' (Ref t') - (VIdent top I8) I32 (VInteger 0) I32 (VInteger 0) + GetElementPtr t' (Ref t') (VIdent top I8) + I64 (VInteger 0) + I32 (VInteger 0) emit $ Store I8 (VInteger $ numCI ci ) (Ref I8) ptr -- get a pointer of the correct type ptr' <- GA.Ident . show <$> getNewVar - emit $ SetVariable ptr' (Bitcast (Ref t') ptr (Ref $ CustomType id)) + emit $ SetVariable ptr' (Bitcast (Ref t') top (Ref $ CustomType id)) --emit $ UnsafeRaw "\n" @@ -166,9 +167,10 @@ compileScs [] = do emit $ Comment (show arg_t' <>" "<> arg_n <> " " <> show i ) elemPtr <- GA.Ident . show <$> getNewVar emit $ SetVariable elemPtr ( - GetElementPtrInbounds (CustomType id) (Ref (CustomType id)) - (VIdent ptr' Ptr) I32 - (VInteger 0) I32 (VInteger i)) + GetElementPtr (CustomType id) (Ref (CustomType id)) + (VIdent ptr' Ptr) + I64 (VInteger 0) + I32 (VInteger i)) emit $ Store arg_t' (VIdent (GA.Ident arg_n) arg_t') Ptr elemPtr -- %2 = getelementptr inbounds %Foo_AInteger, %Foo_AInteger* %1, i32 0, i32 1 -- store i32 42, i32* %2 @@ -178,6 +180,7 @@ compileScs [] = do --emit $ UnsafeRaw "\n" -- load and return the constructed value + emit $ Comment "Return the newly constructed value" load <- GA.Ident . show <$> getNewVar emit $ SetVariable load (Load t' Ptr top) emit $ Ret t' (VIdent load t') @@ -213,9 +216,9 @@ mainContent var = [ UnsafeRaw $ "%2 = alloca %Craig\n" <> " store %Craig %1, ptr %2\n" <> - " %3 = bitcast %Craig* %2 to i64*\n" <> - " %4 = load i64, ptr %3\n" <> - " call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %4)\n" + " %3 = bitcast %Craig* %2 to i72*\n" <> + " %4 = load i72, ptr %3\n" <> + " call i32 (ptr, ...) @printf(ptr noundef @.str, i72 noundef %4)\n" -- "call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef " <> show var <> ")\n" , -- , SetVariable (GA.Ident "p") (Icmp LLEq I64 (VInteger 2) (VInteger 2)) -- , BrCond (VIdent (GA.Ident "p")) (GA.Ident "b_1") (GA.Ident "b_2") @@ -234,7 +237,7 @@ mainContent var = defaultStart :: [LLVMIr] defaultStart = [ UnsafeRaw "target triple = \"x86_64-pc-linux-gnu\"\n" , UnsafeRaw "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" - , UnsafeRaw "@.str = private unnamed_addr constant [3 x i8] c\"%i\n\", align 1\n" + , UnsafeRaw "@.str = private unnamed_addr constant [3 x i8] c\"%x\n\", align 1\n" , UnsafeRaw "declare i32 @printf(ptr noalias nocapture, ...)\n" ] diff --git a/src/Codegen/LlvmIr.hs b/src/Codegen/LlvmIr.hs index ab2ed90..7a0cf82 100644 --- a/src/Codegen/LlvmIr.hs +++ b/src/Codegen/LlvmIr.hs @@ -106,6 +106,7 @@ data LLVMIr | Declare LLVMType Ident Params | SetVariable Ident LLVMIr | Variable Ident + | GetElementPtr LLVMType LLVMType LLVMValue LLVMType LLVMValue LLVMType LLVMValue | GetElementPtrInbounds LLVMType LLVMType LLVMValue LLVMType LLVMValue LLVMType LLVMValue | Add LLVMType LLVMValue LLVMValue | Sub LLVMType LLVMValue LLVMValue @@ -146,6 +147,12 @@ llvmIrToString = go 0 insToString :: Int -> LLVMIr -> String insToString i l = replicate i '\t' <> case l of + (GetElementPtr t1 t2 p t3 v1 t4 v2) -> do + -- getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0 + concat + [ "getelementptr ", show t1, ", " , show t2 + , " ", show p, ", ", show t3, " ", show v1, + ", ", show t4, " ", show v2, "\n" ] (GetElementPtrInbounds t1 t2 p t3 v1 t4 v2) -> do -- getelementptr inbounds %Foo, %Foo* %x, i32 0, i32 0 concat