How to use LLVMBuildStore API












-1















long i=3;
long *j;
j=&i;


I want to use LLVM C API to transform above code especially last line into llvm code .Now I search correct function in the LLVM C API ,I think should use "LLVMBuildStore",but there is "&" operator,How can I translate it? LLVMBuildStore' second parameter is LLVMValueRef type value which refer to "i" in this case,how to get the address from this value?










share|improve this question



























    -1















    long i=3;
    long *j;
    j=&i;


    I want to use LLVM C API to transform above code especially last line into llvm code .Now I search correct function in the LLVM C API ,I think should use "LLVMBuildStore",but there is "&" operator,How can I translate it? LLVMBuildStore' second parameter is LLVMValueRef type value which refer to "i" in this case,how to get the address from this value?










    share|improve this question

























      -1












      -1








      -1


      0






      long i=3;
      long *j;
      j=&i;


      I want to use LLVM C API to transform above code especially last line into llvm code .Now I search correct function in the LLVM C API ,I think should use "LLVMBuildStore",but there is "&" operator,How can I translate it? LLVMBuildStore' second parameter is LLVMValueRef type value which refer to "i" in this case,how to get the address from this value?










      share|improve this question














      long i=3;
      long *j;
      j=&i;


      I want to use LLVM C API to transform above code especially last line into llvm code .Now I search correct function in the LLVM C API ,I think should use "LLVMBuildStore",but there is "&" operator,How can I translate it? LLVMBuildStore' second parameter is LLVMValueRef type value which refer to "i" in this case,how to get the address from this value?







      c api llvm






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 2:44









      freyonefreyone

      735




      735
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Given that you have provided the address assignment expression to j, I suppose that the variables are not globals, but rather allocated inside a function.



          So, to translate this C snippet into LLVM IR, you first need to allocate space in the stack for variable i and pointer j, and then store constant 3 to the address of i and the address of i to the address of pointer j.



          In LLVM IR:



          %i = alloca i64, align 8  ;allocation for i. %i is a pointer i64* to variable i
          %j = alloca i64*, align 8 ;respectively, the type of %j is i64** (pointer to i64*)
          store i64 3, i64* %i, align 8 ; i=3
          store i64* %i, i64** %j, align 8 ; store %i (the address of var i) to the address of pointer j


          I do not know how to generate these instructions using the LLVM C API, because I have never used it. However, hopefully there might be a relevance between the C and C++ API and maybe providing the code that I would write using the C++ API might help you get an idea of the type of arguments you need to use. I hope it helps.



          AllocaInst *alloc_i = new AllocaInst(Type::getInt64Ty(M.getContext()), //Type i64
          0, //AddressSpace
          nullptr, //Arraysize
          8, //Alignment
          "i", //name of result in IR. Leave "" for default
          I); //Add alloca instruction before Instruction I

          AllocaInst *alloc_j = new AllocaInst(Type::getInt64PtrTy(M.getContext()), //Type i64*
          0, //AddressSpace
          nullptr, //Arraysize
          8, //Alignment
          "i", //name of result in IR. Leave "" for default
          I); //Add alloca instruction before Instruction I

          StoreInst *store_i = new StoreInst(ConstantInt::get(Type::getInt64Ty(M.getContext()), 3), //get constant 3 of type i64
          alloc_i, //store to the result Value* of alloc_i
          false,
          8, //Alignment
          I); //Insert before instr I

          StoreInst *store_j = new StoreInst(alloc_i, //i64* pointer to i
          alloc_j, //store to the address of pointer j
          false,
          8, //Alignment
          I); //Insert before instr I


          Finally, consider using the C++ API instead.






          share|improve this answer


























          • thanks your example.now i knew the allocated value is already the point value,so do nothing for "&" operator,only when get the content ,need to "load" the content from the pointer.

            – freyone
            Nov 22 '18 at 2:00



















          0














          Considering the following C function



          int main() {
          long i=3;
          long *j;
          j=&i;
          return 0;
          }


          Clang 3.8 will generate:



          define i32 @main() {
          %1 = alloca i32, align 4
          %2 = alloca i64, align 8
          %3 = alloca i64*, align 8
          store i32 0, i32* %1, align 4
          store i64 3, i64* %2, align 8
          store i64* %2, i64** %3, align 8
          ret i32 0
          }


          What we can see from the generated code is that clang generates a single basic block.
          In this basic block we generate three alloca instructions and three store instructions and one ret instruction which terminates the basic block.



          For store instructions, you can use LLVMBuildStore().
          LLVMBuildStore takes three arguments, the first being your builder. The second parameter is what you would like to store the third is where to store it.
          For the first two store instructions, you can use just use the LLVM value that represents your integers.



          For the third parameter use the LLVMBuildStore instruction directly without loading the variable. Where the second parameter is the LLVMValueRef you received from your previous LLVMBuildAlloca() which you got when creating :



          %2 = alloca i64*, align 8





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404596%2fhow-to-use-llvmbuildstore-api%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Given that you have provided the address assignment expression to j, I suppose that the variables are not globals, but rather allocated inside a function.



            So, to translate this C snippet into LLVM IR, you first need to allocate space in the stack for variable i and pointer j, and then store constant 3 to the address of i and the address of i to the address of pointer j.



            In LLVM IR:



            %i = alloca i64, align 8  ;allocation for i. %i is a pointer i64* to variable i
            %j = alloca i64*, align 8 ;respectively, the type of %j is i64** (pointer to i64*)
            store i64 3, i64* %i, align 8 ; i=3
            store i64* %i, i64** %j, align 8 ; store %i (the address of var i) to the address of pointer j


            I do not know how to generate these instructions using the LLVM C API, because I have never used it. However, hopefully there might be a relevance between the C and C++ API and maybe providing the code that I would write using the C++ API might help you get an idea of the type of arguments you need to use. I hope it helps.



            AllocaInst *alloc_i = new AllocaInst(Type::getInt64Ty(M.getContext()), //Type i64
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            AllocaInst *alloc_j = new AllocaInst(Type::getInt64PtrTy(M.getContext()), //Type i64*
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            StoreInst *store_i = new StoreInst(ConstantInt::get(Type::getInt64Ty(M.getContext()), 3), //get constant 3 of type i64
            alloc_i, //store to the result Value* of alloc_i
            false,
            8, //Alignment
            I); //Insert before instr I

            StoreInst *store_j = new StoreInst(alloc_i, //i64* pointer to i
            alloc_j, //store to the address of pointer j
            false,
            8, //Alignment
            I); //Insert before instr I


            Finally, consider using the C++ API instead.






            share|improve this answer


























            • thanks your example.now i knew the allocated value is already the point value,so do nothing for "&" operator,only when get the content ,need to "load" the content from the pointer.

              – freyone
              Nov 22 '18 at 2:00
















            1














            Given that you have provided the address assignment expression to j, I suppose that the variables are not globals, but rather allocated inside a function.



            So, to translate this C snippet into LLVM IR, you first need to allocate space in the stack for variable i and pointer j, and then store constant 3 to the address of i and the address of i to the address of pointer j.



            In LLVM IR:



            %i = alloca i64, align 8  ;allocation for i. %i is a pointer i64* to variable i
            %j = alloca i64*, align 8 ;respectively, the type of %j is i64** (pointer to i64*)
            store i64 3, i64* %i, align 8 ; i=3
            store i64* %i, i64** %j, align 8 ; store %i (the address of var i) to the address of pointer j


            I do not know how to generate these instructions using the LLVM C API, because I have never used it. However, hopefully there might be a relevance between the C and C++ API and maybe providing the code that I would write using the C++ API might help you get an idea of the type of arguments you need to use. I hope it helps.



            AllocaInst *alloc_i = new AllocaInst(Type::getInt64Ty(M.getContext()), //Type i64
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            AllocaInst *alloc_j = new AllocaInst(Type::getInt64PtrTy(M.getContext()), //Type i64*
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            StoreInst *store_i = new StoreInst(ConstantInt::get(Type::getInt64Ty(M.getContext()), 3), //get constant 3 of type i64
            alloc_i, //store to the result Value* of alloc_i
            false,
            8, //Alignment
            I); //Insert before instr I

            StoreInst *store_j = new StoreInst(alloc_i, //i64* pointer to i
            alloc_j, //store to the address of pointer j
            false,
            8, //Alignment
            I); //Insert before instr I


            Finally, consider using the C++ API instead.






            share|improve this answer


























            • thanks your example.now i knew the allocated value is already the point value,so do nothing for "&" operator,only when get the content ,need to "load" the content from the pointer.

              – freyone
              Nov 22 '18 at 2:00














            1












            1








            1







            Given that you have provided the address assignment expression to j, I suppose that the variables are not globals, but rather allocated inside a function.



            So, to translate this C snippet into LLVM IR, you first need to allocate space in the stack for variable i and pointer j, and then store constant 3 to the address of i and the address of i to the address of pointer j.



            In LLVM IR:



            %i = alloca i64, align 8  ;allocation for i. %i is a pointer i64* to variable i
            %j = alloca i64*, align 8 ;respectively, the type of %j is i64** (pointer to i64*)
            store i64 3, i64* %i, align 8 ; i=3
            store i64* %i, i64** %j, align 8 ; store %i (the address of var i) to the address of pointer j


            I do not know how to generate these instructions using the LLVM C API, because I have never used it. However, hopefully there might be a relevance between the C and C++ API and maybe providing the code that I would write using the C++ API might help you get an idea of the type of arguments you need to use. I hope it helps.



            AllocaInst *alloc_i = new AllocaInst(Type::getInt64Ty(M.getContext()), //Type i64
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            AllocaInst *alloc_j = new AllocaInst(Type::getInt64PtrTy(M.getContext()), //Type i64*
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            StoreInst *store_i = new StoreInst(ConstantInt::get(Type::getInt64Ty(M.getContext()), 3), //get constant 3 of type i64
            alloc_i, //store to the result Value* of alloc_i
            false,
            8, //Alignment
            I); //Insert before instr I

            StoreInst *store_j = new StoreInst(alloc_i, //i64* pointer to i
            alloc_j, //store to the address of pointer j
            false,
            8, //Alignment
            I); //Insert before instr I


            Finally, consider using the C++ API instead.






            share|improve this answer















            Given that you have provided the address assignment expression to j, I suppose that the variables are not globals, but rather allocated inside a function.



            So, to translate this C snippet into LLVM IR, you first need to allocate space in the stack for variable i and pointer j, and then store constant 3 to the address of i and the address of i to the address of pointer j.



            In LLVM IR:



            %i = alloca i64, align 8  ;allocation for i. %i is a pointer i64* to variable i
            %j = alloca i64*, align 8 ;respectively, the type of %j is i64** (pointer to i64*)
            store i64 3, i64* %i, align 8 ; i=3
            store i64* %i, i64** %j, align 8 ; store %i (the address of var i) to the address of pointer j


            I do not know how to generate these instructions using the LLVM C API, because I have never used it. However, hopefully there might be a relevance between the C and C++ API and maybe providing the code that I would write using the C++ API might help you get an idea of the type of arguments you need to use. I hope it helps.



            AllocaInst *alloc_i = new AllocaInst(Type::getInt64Ty(M.getContext()), //Type i64
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            AllocaInst *alloc_j = new AllocaInst(Type::getInt64PtrTy(M.getContext()), //Type i64*
            0, //AddressSpace
            nullptr, //Arraysize
            8, //Alignment
            "i", //name of result in IR. Leave "" for default
            I); //Add alloca instruction before Instruction I

            StoreInst *store_i = new StoreInst(ConstantInt::get(Type::getInt64Ty(M.getContext()), 3), //get constant 3 of type i64
            alloc_i, //store to the result Value* of alloc_i
            false,
            8, //Alignment
            I); //Insert before instr I

            StoreInst *store_j = new StoreInst(alloc_i, //i64* pointer to i
            alloc_j, //store to the address of pointer j
            false,
            8, //Alignment
            I); //Insert before instr I


            Finally, consider using the C++ API instead.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 0:05

























            answered Nov 21 '18 at 23:51









            Foivos TsFoivos Ts

            112




            112













            • thanks your example.now i knew the allocated value is already the point value,so do nothing for "&" operator,only when get the content ,need to "load" the content from the pointer.

              – freyone
              Nov 22 '18 at 2:00



















            • thanks your example.now i knew the allocated value is already the point value,so do nothing for "&" operator,only when get the content ,need to "load" the content from the pointer.

              – freyone
              Nov 22 '18 at 2:00

















            thanks your example.now i knew the allocated value is already the point value,so do nothing for "&" operator,only when get the content ,need to "load" the content from the pointer.

            – freyone
            Nov 22 '18 at 2:00





            thanks your example.now i knew the allocated value is already the point value,so do nothing for "&" operator,only when get the content ,need to "load" the content from the pointer.

            – freyone
            Nov 22 '18 at 2:00













            0














            Considering the following C function



            int main() {
            long i=3;
            long *j;
            j=&i;
            return 0;
            }


            Clang 3.8 will generate:



            define i32 @main() {
            %1 = alloca i32, align 4
            %2 = alloca i64, align 8
            %3 = alloca i64*, align 8
            store i32 0, i32* %1, align 4
            store i64 3, i64* %2, align 8
            store i64* %2, i64** %3, align 8
            ret i32 0
            }


            What we can see from the generated code is that clang generates a single basic block.
            In this basic block we generate three alloca instructions and three store instructions and one ret instruction which terminates the basic block.



            For store instructions, you can use LLVMBuildStore().
            LLVMBuildStore takes three arguments, the first being your builder. The second parameter is what you would like to store the third is where to store it.
            For the first two store instructions, you can use just use the LLVM value that represents your integers.



            For the third parameter use the LLVMBuildStore instruction directly without loading the variable. Where the second parameter is the LLVMValueRef you received from your previous LLVMBuildAlloca() which you got when creating :



            %2 = alloca i64*, align 8





            share|improve this answer




























              0














              Considering the following C function



              int main() {
              long i=3;
              long *j;
              j=&i;
              return 0;
              }


              Clang 3.8 will generate:



              define i32 @main() {
              %1 = alloca i32, align 4
              %2 = alloca i64, align 8
              %3 = alloca i64*, align 8
              store i32 0, i32* %1, align 4
              store i64 3, i64* %2, align 8
              store i64* %2, i64** %3, align 8
              ret i32 0
              }


              What we can see from the generated code is that clang generates a single basic block.
              In this basic block we generate three alloca instructions and three store instructions and one ret instruction which terminates the basic block.



              For store instructions, you can use LLVMBuildStore().
              LLVMBuildStore takes three arguments, the first being your builder. The second parameter is what you would like to store the third is where to store it.
              For the first two store instructions, you can use just use the LLVM value that represents your integers.



              For the third parameter use the LLVMBuildStore instruction directly without loading the variable. Where the second parameter is the LLVMValueRef you received from your previous LLVMBuildAlloca() which you got when creating :



              %2 = alloca i64*, align 8





              share|improve this answer


























                0












                0








                0







                Considering the following C function



                int main() {
                long i=3;
                long *j;
                j=&i;
                return 0;
                }


                Clang 3.8 will generate:



                define i32 @main() {
                %1 = alloca i32, align 4
                %2 = alloca i64, align 8
                %3 = alloca i64*, align 8
                store i32 0, i32* %1, align 4
                store i64 3, i64* %2, align 8
                store i64* %2, i64** %3, align 8
                ret i32 0
                }


                What we can see from the generated code is that clang generates a single basic block.
                In this basic block we generate three alloca instructions and three store instructions and one ret instruction which terminates the basic block.



                For store instructions, you can use LLVMBuildStore().
                LLVMBuildStore takes three arguments, the first being your builder. The second parameter is what you would like to store the third is where to store it.
                For the first two store instructions, you can use just use the LLVM value that represents your integers.



                For the third parameter use the LLVMBuildStore instruction directly without loading the variable. Where the second parameter is the LLVMValueRef you received from your previous LLVMBuildAlloca() which you got when creating :



                %2 = alloca i64*, align 8





                share|improve this answer













                Considering the following C function



                int main() {
                long i=3;
                long *j;
                j=&i;
                return 0;
                }


                Clang 3.8 will generate:



                define i32 @main() {
                %1 = alloca i32, align 4
                %2 = alloca i64, align 8
                %3 = alloca i64*, align 8
                store i32 0, i32* %1, align 4
                store i64 3, i64* %2, align 8
                store i64* %2, i64** %3, align 8
                ret i32 0
                }


                What we can see from the generated code is that clang generates a single basic block.
                In this basic block we generate three alloca instructions and three store instructions and one ret instruction which terminates the basic block.



                For store instructions, you can use LLVMBuildStore().
                LLVMBuildStore takes three arguments, the first being your builder. The second parameter is what you would like to store the third is where to store it.
                For the first two store instructions, you can use just use the LLVM value that represents your integers.



                For the third parameter use the LLVMBuildStore instruction directly without loading the variable. Where the second parameter is the LLVMValueRef you received from your previous LLVMBuildAlloca() which you got when creating :



                %2 = alloca i64*, align 8






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 22:08









                JKRTJKRT

                431517




                431517






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404596%2fhow-to-use-llvmbuildstore-api%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    WPF add header to Image with URL pettitions [duplicate]