├── .gitignore ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── bit-writer.rkt ├── builder.rkt ├── definer.rkt ├── error.rkt ├── examples ├── optimization.rkt ├── printf.rkt ├── show-function-cfg.rkt ├── sum.rkt └── write-bitcode.rkt ├── info.rkt ├── jit.rkt ├── main.rkt ├── module.rkt ├── pass-manager.rkt ├── ref.rkt ├── scribblings ├── .gitignore ├── builder.scrbl ├── jit.scrbl ├── llvm-structure.scrbl ├── module.scrbl └── racket-llvm.scrbl ├── types.rkt └── value.rkt /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | \#* 3 | .\#* 4 | .DS_Store 5 | compiled/ 6 | /doc/ 7 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Lîm Tsú-thuàn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | racket-llvm 2 | ============= 3 | 4 | > **Warning** 5 | > LLVM is a big project, that's quite normal to find some missing functions/definitions in this project. Thus, you can use `define-llvm` to get missing functions, and we will give you a big thanks if you would like to port them back to the project by pull requests! To get full definitions in LLVM, look at [LLVM C-API](https://llvm.org/doxygen/dir_db1e4f1ef1b4536ff54becd23c94e664.html) for reference. 6 | 7 | A racket to LLVM C-API bindings. 8 | 9 | ### Installation 10 | 11 | ```shell 12 | raco pkg install --auto racket-llvm 13 | ``` 14 | 15 | ### Usage 16 | 17 | Ensure you have `llvm-config` installed and can invoke it. You might need following command. 18 | 19 | ```shell 20 | # export homebrew installed llvm binaries 21 | export PATH="/opt/homebrew/opt/llvm/bin:$PATH" 22 | ``` 23 | 24 | ##### Dependencies 25 | 26 | Add `"racket-llvm"` into `info.rkt` 27 | 28 | ```racket 29 | (define deps '("base" "racket-llvm")) 30 | ``` 31 | 32 | ##### Example 33 | 34 | The following one shows how to use **Execution Engine** to run a LLVM function and view the result. 35 | 36 | ```racket 37 | ; create module, the basic unit in LLVM IR 38 | (define mod (llvm-module "test_mod")) 39 | ; create builder, all instructions will be created by this 40 | (define builder (llvm-builder-create)) 41 | 42 | ; add function into module 43 | (define sum (llvm-add-function mod "sum" (llvm-function-type (llvm-int32-type) (list (llvm-int32-type) (llvm-int32-type))))) 44 | ; create basic block for function 45 | (define entry (llvm-append-basic-block sum)) 46 | ; shift builder insertion point to the end of the basic block 47 | (llvm-builder-position-at-end builder entry) 48 | ; build `%3 = add i32 %0, %1` 49 | ; build `ret i32 %3` 50 | (llvm-build-ret builder (llvm-build-add builder (llvm-get-param sum 0) (llvm-get-param sum 1))) 51 | 52 | ; verify the module 53 | (llvm-module-verify mod) 54 | ; print and check the content of the module 55 | (displayln (llvm-module->string mod)) 56 | 57 | ; create execution engine based on module 58 | (define eng (llvm-create-execution-engine-for-module mod)) 59 | ; link to mcjit 60 | (llvm-link-in-mcjit) 61 | 62 | ; ask two integers 63 | (define (ask-integer) 64 | (printf "enter integer: ") 65 | (read)) 66 | (define-values (x y) (values (ask-integer) (ask-integer))) 67 | 68 | ; use engine to run the function(engine will search it in the module) 69 | (define res (llvm-run-function eng sum 70 | (list (llvm-create-generic-value-of-int (llvm-int32-type) x #f) 71 | (llvm-create-generic-value-of-int (llvm-int32-type) y #f)))) 72 | ; show result 73 | (printf "JIT result: ~a\n" (llvm-generic-value->int res #f)) 74 | ``` 75 | 76 | ### Develop 77 | 78 | Here is an example to show how can one define a new llvm function binding via `define-llvm`. 79 | 80 | ```racket 81 | ;; our standard is converting from CamlCase to kebab-case identifier 82 | (define-llvm llvm-build-struct-gep2 83 | ;; here is a complicated version of `_fun`, the first wrapped list are parameters 84 | (_fun (builder ty pointer index [name ""]) :: 85 | ;; the followings are type for parameters 86 | (builder : _LLVMBuilderRef) 87 | (ty : _LLVMTypeRef) 88 | (pointer : _LLVMValueRef) 89 | (index : _int) 90 | (name : _string) 91 | ;; final, the return type 92 | -> _LLVMValueRef) 93 | ;; `LLVMBuildStructGEP2` is the name of function in C side 94 | #:c-id LLVMBuildStructGEP2) 95 | ``` 96 | 97 | And you can get llvm functions list from [LLVM C-API](https://llvm.org/doxygen/dir_db1e4f1ef1b4536ff54becd23c94e664.html). To get more about the FFI in racket, reference to its [document](https://docs.racket-lang.org/foreign/index.html#%28tech._ffi%29). 98 | 99 | ### Reference 100 | 101 | - [llvm doxygen](https://llvm.org/doxygen/) 102 | - [How to get started with the LLVM C API](https://www.pauladamsmith.com/blog/2015/01/how-to-get-started-with-llvm-c-api.html) 103 | -------------------------------------------------------------------------------- /bit-writer.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (provide (all-defined-out)) 3 | (require ffi/unsafe 4 | "definer.rkt" 5 | "ref.rkt") 6 | 7 | (define-llvm llvm-write-bitcode-to-file (_fun (mod file-path) :: 8 | (mod : _LLVMModuleRef) 9 | (file-path : _string) 10 | ; coercion the integer failure to bool 11 | ; 0 should be #f, otherwise is #t 12 | -> (failure : _bool) 13 | -> (when failure (raise "write bitcode failed"))) 14 | #:c-id LLVMWriteBitcodeToFile) 15 | -------------------------------------------------------------------------------- /builder.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (provide (all-defined-out)) 3 | (require ffi/unsafe 4 | "definer.rkt" 5 | "types.rkt" 6 | "ref.rkt") 7 | 8 | (define-llvm llvm-append-basic-block (_fun (fn [name ""]) :: 9 | (fn : _LLVMValueRef) 10 | (name : _string) 11 | -> _LLVMBasicBlockRef) 12 | #:c-id LLVMAppendBasicBlock) 13 | 14 | (define-llvm llvm-builder-create (_fun -> _LLVMBuilderRef) #:c-id LLVMCreateBuilder) 15 | 16 | (define-llvm llvm-builder-position-at-end (_fun _LLVMBuilderRef 17 | _LLVMBasicBlockRef 18 | -> _void) 19 | #:c-id LLVMPositionBuilderAtEnd) 20 | (define-llvm llvm-get-param (_fun _LLVMValueRef _int -> _LLVMValueRef) #:c-id LLVMGetParam) 21 | 22 | (define-llvm llvm-build-ret (_fun _LLVMBuilderRef _LLVMValueRef -> _LLVMValueRef) 23 | #:c-id LLVMBuildRet) 24 | (define-llvm llvm-build-ret-void (_fun _LLVMBuilderRef -> _LLVMValueRef) 25 | #:c-id LLVMBuildRetVoid) 26 | (define-llvm llvm-build-br (_fun _LLVMBuilderRef 27 | _LLVMBasicBlockRef ; dest 28 | -> _LLVMValueRef) 29 | #:c-id LLVMBuildBr) 30 | (define-llvm llvm-build-cond-br (_fun _LLVMBuilderRef 31 | _LLVMValueRef ; if 32 | _LLVMBasicBlockRef ; then 33 | _LLVMBasicBlockRef ; else 34 | -> _LLVMValueRef) 35 | #:c-id LLVMBuildCondBr) 36 | (define-llvm llvm-build-switch (_fun _LLVMBuilderRef 37 | _LLVMValueRef ; V 38 | _LLVMBasicBlockRef ; Else 39 | _int ; NumCases 40 | -> _LLVMValueRef) 41 | #:c-id LLVMBuildSwitch) 42 | (define-llvm llvm-add-case (_fun _LLVMBuilderRef 43 | _LLVMValueRef ; switch 44 | _LLVMValueRef ; on value 45 | _LLVMBasicBlockRef ; dest 46 | -> _void) 47 | #:c-id LLVMAddCase) 48 | (define-llvm llvm-build-indirectbr (_fun _LLVMBuilderRef 49 | _LLVMValueRef ; Addr 50 | _int ; NumDests 51 | -> _LLVMValueRef) 52 | #:c-id LLVMBuildIndirectBr) 53 | (define-llvm llvm-add-destination (_fun _LLVMValueRef ; IndirectBr 54 | _LLVMBasicBlockRef ; Dest 55 | -> _void) 56 | #:c-id LLVMAddDestination) 57 | (define-llvm llvm-build-unreachable (_fun _LLVMBuilderRef -> _LLVMValueRef) #:c-id LLVMBuildUnreachable) 58 | 59 | (define (cmp-fun pred) (_fun (builder p lhs rhs [name ""]) :: 60 | (builder : _LLVMBuilderRef) 61 | (p : pred) 62 | (lhs : _LLVMValueRef) 63 | (rhs : _LLVMValueRef) 64 | (name : _string) 65 | -> _LLVMValueRef)) 66 | (define _LLVMIntPredicate 67 | (_enum '(int-eq = 32 68 | int-ne 69 | int-ugt int-uge int-ult int-ule 70 | int-sgt int-sge int-slt int-sle))) 71 | (define-llvm llvm-build-int-cmp (cmp-fun _LLVMIntPredicate) #:c-id LLVMBuildICmp) 72 | (define _LLVMRealPredicate 73 | (_enum '(predicate-false 74 | real-oeq real-ogt real-oge real-olt real-ole real-one real-ord 75 | real-uno real-ueq real-ugt real-uge real-ult real-ule real-une 76 | predicate-true))) 77 | (define-llvm llvm-build-float-cmp (cmp-fun _LLVMRealPredicate) #:c-id LLVMBuildFCmp) 78 | 79 | (define-llvm llvm-const-int (_fun (type value [sign-extend? #f]) :: 80 | (type : _LLVMTypeRef) 81 | (value : _llong) 82 | (sign-extend? : _bool) 83 | -> _LLVMValueRef) 84 | #:c-id LLVMConstInt) 85 | 86 | (define binary-fun (_fun (builder lhs rhs [name ""]) :: 87 | (builder : _LLVMBuilderRef) 88 | (lhs : _LLVMValueRef) 89 | (rhs : _LLVMValueRef) 90 | (name : _string) 91 | -> _LLVMValueRef)) 92 | (define-llvm llvm-build-add binary-fun #:c-id LLVMBuildAdd) 93 | (define-llvm llvm-build-nswadd binary-fun #:c-id LLVMBuildNSWAdd) 94 | (define-llvm llvm-build-nuwadd binary-fun #:c-id LLVMBuildNUWAdd) 95 | (define-llvm llvm-build-fadd binary-fun #:c-id LLVMBuildFAdd) 96 | (define-llvm llvm-build-sub binary-fun #:c-id LLVMBuildSub) 97 | (define-llvm llvm-build-nswsub binary-fun #:c-id LLVMBuildNSWSub) 98 | (define-llvm llvm-build-nuwsub binary-fun #:c-id LLVMBuildNUWSub) 99 | (define-llvm llvm-build-fsub binary-fun #:c-id LLVMBuildFSub) 100 | (define-llvm llvm-build-mul binary-fun #:c-id LLVMBuildMul) 101 | (define-llvm llvm-build-nswmul binary-fun #:c-id LLVMBuildNSWMul) 102 | (define-llvm llvm-build-nuwmul binary-fun #:c-id LLVMBuildNUWMul) 103 | (define-llvm llvm-build-fmul binary-fun #:c-id LLVMBuildFMul) 104 | (define-llvm llvm-build-udiv binary-fun #:c-id LLVMBuildUDiv) 105 | (define-llvm llvm-build-exactudiv binary-fun #:c-id LLVMBuildExactUDiv) 106 | (define-llvm llvm-build-sdiv binary-fun #:c-id LLVMBuildSDiv) 107 | (define-llvm llvm-build-exactsdiv binary-fun #:c-id LLVMBuildExactSDiv) 108 | (define-llvm llvm-build-fdiv binary-fun #:c-id LLVMBuildFDiv) 109 | (define-llvm llvm-build-urem binary-fun #:c-id LLVMBuildURem) 110 | (define-llvm llvm-build-srem binary-fun #:c-id LLVMBuildSRem) 111 | (define-llvm llvm-build-frem binary-fun #:c-id LLVMBuildFRem) 112 | (define-llvm llvm-build-shl binary-fun #:c-id LLVMBuildShl) 113 | (define-llvm llvm-build-lshr binary-fun #:c-id LLVMBuildLShr) 114 | (define-llvm llvm-build-ashr binary-fun #:c-id LLVMBuildAShr) 115 | (define-llvm llvm-build-and binary-fun #:c-id LLVMBuildAnd) 116 | (define-llvm llvm-build-or binary-fun #:c-id LLVMBuildOr) 117 | (define-llvm llvm-build-xor binary-fun #:c-id LLVMBuildXor) 118 | 119 | (define unary-fun (_fun (builder v [name ""]) :: 120 | (builder : _LLVMBuilderRef) 121 | (v : _LLVMValueRef) 122 | (name : _string) 123 | -> _LLVMValueRef)) 124 | (define-llvm llvm-build-neg unary-fun #:c-id LLVMBuildNeg) 125 | (define-llvm llvm-build-nswneg unary-fun #:c-id LLVMBuildNSWNeg) 126 | (define-llvm llvm-build-nuwneg unary-fun #:c-id LLVMBuildNUWNeg) 127 | (define-llvm llvm-build-fneg unary-fun #:c-id LLVMBuildFNeg) 128 | (define-llvm llvm-build-not unary-fun #:c-id LLVMBuildNot) 129 | 130 | (define allocation-fun (_fun (builder ty [name ""]) :: 131 | (builder : _LLVMBuilderRef) 132 | (ty : _LLVMTypeRef) 133 | (name : _string) 134 | -> _LLVMValueRef)) 135 | (define-llvm llvm-build-malloc allocation-fun #:c-id LLVMBuildMalloc) 136 | (define-llvm llvm-build-alloca allocation-fun #:c-id LLVMBuildAlloca) 137 | (define array-allocation-fun (_fun (builder ty v [name ""]) :: 138 | (builder : _LLVMBuilderRef) 139 | (ty : _LLVMTypeRef) 140 | (v : _LLVMValueRef) 141 | (name : _string) 142 | -> _LLVMValueRef)) 143 | (define-llvm llvm-build-array-malloc array-allocation-fun #:c-id LLVMBuildArrayMalloc) 144 | (define-llvm llvm-build-array-alloca array-allocation-fun #:c-id LLVMBuildArrayAlloca) 145 | 146 | (define-llvm llvm-build-free (_fun _LLVMBuilderRef _LLVMValueRef -> _LLVMValueRef) #:c-id LLVMBuildFree) 147 | 148 | (define-llvm llvm-build-load2 (_fun (builder ty pointer_val [name ""]) :: 149 | (builder : _LLVMBuilderRef) 150 | (ty : _LLVMTypeRef) 151 | (pointer_val : _LLVMValueRef) 152 | (name : _string) 153 | -> _LLVMValueRef) 154 | #:c-id LLVMBuildLoad2) 155 | (define-llvm llvm-build-store (_fun (builder val ptr) :: 156 | (builder : _LLVMBuilderRef) 157 | (val : _LLVMValueRef) 158 | (ptr : _LLVMValueRef) 159 | -> _LLVMValueRef) 160 | #:c-id LLVMBuildStore) 161 | (define-llvm llvm-build-insert-value (_fun (builder aggregate_val element_val index [name ""]) :: 162 | (builder : _LLVMBuilderRef) 163 | (aggregate_val : _LLVMValueRef) 164 | (element_val : _LLVMValueRef) 165 | (index : _int) 166 | (name : _string) 167 | -> _LLVMValueRef) 168 | #:c-id LLVMBuildInsertValue) 169 | (define-llvm llvm-build-extract-element (_fun (builder aggregate_val index_val [name ""]) :: 170 | (builder : _LLVMBuilderRef) 171 | (aggregate_val : _LLVMValueRef) 172 | (index_val : _LLVMValueRef) 173 | (name : _string) 174 | -> _LLVMValueRef) 175 | #:c-id LLVMBuildExtractElement) 176 | 177 | (define gep-fun (_fun (builder ty pointer indices [name ""]) :: 178 | (builder : _LLVMBuilderRef) 179 | (ty : _LLVMTypeRef) 180 | (pointer : _LLVMValueRef) 181 | (indices : (_list i _LLVMValueRef)) 182 | (_int = (length indices)) 183 | (name : _string) 184 | -> _LLVMValueRef)) 185 | (define-llvm llvm-build-gep2 gep-fun #:c-id LLVMBuildGEP2) 186 | (define-llvm llvm-build-in-bounds-gep2 gep-fun #:c-id LLVMBuildInBoundsGEP2) 187 | (define-llvm llvm-build-struct-gep2 (_fun (builder ty pointer index [name ""]) :: 188 | (builder : _LLVMBuilderRef) 189 | (ty : _LLVMTypeRef) 190 | (pointer : _LLVMValueRef) 191 | (index : _int) 192 | (name : _string) 193 | -> _LLVMValueRef) 194 | #:c-id LLVMBuildStructGEP2) 195 | 196 | (define build-string-fun (_fun (builder val [name ""]) :: 197 | (builder : _LLVMBuilderRef) 198 | (val : _string) 199 | (name : _string) 200 | -> _LLVMValueRef)) 201 | (define-llvm llvm-build-string build-string-fun #:c-id LLVMBuildGlobalString) 202 | (define-llvm llvm-build-string-ptr build-string-fun #:c-id LLVMBuildGlobalStringPtr) 203 | 204 | (define (get-fun ty) (_fun _LLVMValueRef -> ty)) 205 | (define (set-fun ty) (_fun _LLVMValueRef ty -> _void)) 206 | (define-llvm llvm-get-volatile (get-fun _bool) #:c-id LLVMGetVolatile) 207 | (define-llvm llvm-set-volatile (get-fun _bool) #:c-id LLVMSetVolatile) 208 | (define-llvm llvm-get-weak (get-fun _bool) #:c-id LLVMGetWeak) 209 | (define-llvm llvm-set-weak (get-fun _bool) #:c-id LLVMSetWeak) 210 | (define _LLVMAtomicOrdering 211 | (_enum '(not-atomic = 0 212 | unordered 213 | monotonic 214 | acquire = 4 215 | release 216 | acquire-release 217 | sequentially-consistent))) 218 | (define-llvm llvm-get-ordering (get-fun _LLVMAtomicOrdering) #:c-id LLVMGetOrdering) 219 | (define-llvm llvm-set-ordering (get-fun _LLVMAtomicOrdering) #:c-id LLVMSetOrdering) 220 | (define _LLVMAtomicRMWBinOp 221 | (_enum '(xchg add sub and nand or xor max min umax umin fadd fsub))) 222 | (define-llvm llvm-get-atomic-rmw-binop (get-fun _LLVMAtomicRMWBinOp) #:c-id LLVMGetAtomicRMWBinOp) 223 | (define-llvm llvm-set-atomic-rmw-binop (get-fun _LLVMAtomicRMWBinOp) #:c-id LLVMSetAtomicRMWBinOp) 224 | 225 | (define cast-fun (_fun (builder val dest-ty [name ""]) :: 226 | (builder : _LLVMBuilderRef) 227 | (val : _LLVMValueRef) 228 | (dest-ty : _LLVMTypeRef) 229 | (name : _string) 230 | -> _LLVMValueRef)) 231 | (define-llvm llvm-build-trunc cast-fun #:c-id LLVMBuildTrunc) 232 | (define-llvm llvm-build-zext cast-fun #:c-id LLVMBuildZExt) 233 | (define-llvm llvm-build-sext cast-fun #:c-id LLVMBuildSExt) 234 | (define-llvm llvm-build-fp->ui cast-fun #:c-id LLVMBuildFPToUI) 235 | (define-llvm llvm-build-fp->si cast-fun #:c-id LLVMBuildFPToSI) 236 | (define-llvm llvm-build-ui->fp cast-fun #:c-id LLVMBuildUIToFP) 237 | (define-llvm llvm-build-si->fp cast-fun #:c-id LLVMBuildSIToFP) 238 | (define-llvm llvm-build-fp-trunc cast-fun #:c-id LLVMBuildFPTrunc) 239 | (define-llvm llvm-build-fp-ext cast-fun #:c-id LLVMBuildFPExt) 240 | (define-llvm llvm-build-ptr->int cast-fun #:c-id LLVMBuildPtrToInt) 241 | (define-llvm llvm-build-int->ptr cast-fun #:c-id LLVMBuildIntToPtr) 242 | (define-llvm llvm-build-bitcast cast-fun #:c-id LLVMBuildBitCast) 243 | (define-llvm llvm-build-addrspace-cast cast-fun #:c-id LLVMBuildAddrSpaceCast) 244 | (define-llvm llvm-build-zext-or-bitcast cast-fun #:c-id LLVMBuildZExtOrBitCast) 245 | (define-llvm llvm-build-sext-or-bitcast cast-fun #:c-id LLVMBuildSExtOrBitCast) 246 | (define-llvm llvm-build-trunc-or-bitcast cast-fun #:c-id LLVMBuildTruncOrBitCast) 247 | (define-llvm llvm-build-pointer-cast cast-fun #:c-id LLVMBuildPointerCast) 248 | (define-llvm llvm-build-fp-cast cast-fun #:c-id LLVMBuildFPCast) 249 | (define-llvm llvm-build-int-cast cast-fun #:c-id LLVMBuildIntCast) 250 | 251 | (define-llvm llvm-build-phi (_fun (builder ty [name ""]) :: 252 | (builder : _LLVMBuilderRef) 253 | (ty : _LLVMTypeRef) 254 | (name : _string) 255 | -> _LLVMValueRef) 256 | #:c-id LLVMBuildPhi) 257 | 258 | (define-llvm llvm-build-call2 (_fun (builder fn-ty fn args [name ""]) :: 259 | (builder : _LLVMBuilderRef) 260 | (fn-ty : _LLVMTypeRef) 261 | (fn : _LLVMValueRef) 262 | (args : (_list i _LLVMValueRef)) 263 | (_int = (length args)) 264 | (name : _string) 265 | -> _LLVMValueRef) 266 | #:c-id LLVMBuildCall2) 267 | 268 | (define-llvm llvm-build-select (_fun (builder if then else [args (list)] [name ""]) :: 269 | (builder : _LLVMBuilderRef) 270 | (if : _LLVMValueRef) 271 | (then : _LLVMValueRef) 272 | (else : _LLVMValueRef) 273 | (name : _string) 274 | -> _LLVMValueRef) 275 | #:c-id LLVMBuildSelect) 276 | 277 | (module+ test 278 | (require rackunit 279 | "module.rkt" 280 | "jit.rkt" 281 | "types.rkt") 282 | (define mod (llvm-module "test")) 283 | 284 | (llvm-link-in-mcjit) 285 | 286 | (define eng (llvm-create-execution-engine-for-module mod)) 287 | 288 | (define (check-binary-inst make-inst 289 | make-type 290 | create-value 291 | get-value 292 | rhs 293 | lhs 294 | result) 295 | (define builder (llvm-builder-create)) 296 | (define func-type (llvm-function-type (make-type) (list (make-type) (make-type)))) 297 | (define func (llvm-add-function mod "" func-type)) 298 | 299 | (define entry (llvm-append-basic-block func)) 300 | (llvm-builder-position-at-end builder entry) 301 | (llvm-build-ret builder (make-inst builder (llvm-get-param func 0) (llvm-get-param func 1))) 302 | 303 | (check-equal? (get-value (llvm-run-function eng func (map create-value (list rhs lhs)))) 304 | result)) 305 | 306 | (define (check-binary-int32-inst make-inst rhs lhs result) 307 | (check-binary-inst make-inst 308 | llvm-int32-type 309 | (λ (x) (llvm-create-generic-value-of-int (llvm-int32-type) x #f)) 310 | (λ (x) (llvm-generic-value->int x #f)) 311 | rhs lhs result)) 312 | 313 | (define (check-binary-float-inst make-inst rhs lhs result) 314 | (check-binary-inst make-inst 315 | llvm-float-type 316 | (λ (x) (llvm-create-generic-value-of-float (llvm-float-type) x)) 317 | (λ (x) (llvm-generic-value->float (llvm-float-type) x)) 318 | rhs lhs result)) 319 | 320 | (define (check-unary-inst make-inst 321 | make-type 322 | create-value 323 | get-value 324 | v 325 | result) 326 | (define builder (llvm-builder-create)) 327 | (define func-type (llvm-function-type (make-type) (list (make-type)))) 328 | (define func (llvm-add-function mod "" func-type)) 329 | (define entry (llvm-append-basic-block func)) 330 | (llvm-builder-position-at-end builder entry) 331 | (llvm-build-ret builder (make-inst builder (llvm-get-param func 0))) 332 | (check-equal? (get-value (llvm-run-function eng func (map create-value (list v)))) 333 | result)) 334 | 335 | (define (check-unary-int-inst make-inst make-type v result) 336 | (check-unary-inst make-inst 337 | make-type 338 | (λ (x) (llvm-create-generic-value-of-int (make-type) x #t)) 339 | (λ (x) (llvm-generic-value->int x #t)) 340 | v result)) 341 | 342 | (define (check-unary-float-inst make-inst make-type v result) 343 | (check-unary-inst make-inst 344 | make-type 345 | (λ (x) (llvm-create-generic-value-of-float (make-type) x)) 346 | (λ (x) (llvm-generic-value->float (make-type) x)) 347 | v result))) 348 | (module+ test 349 | (define builder (llvm-builder-create)) 350 | 351 | ; if 42 > 16 then 1234 else 937 352 | (let* ([func (llvm-add-function mod "" (llvm-function-type (llvm-int32-type)))] 353 | [entry (llvm-append-basic-block func)]) 354 | (llvm-builder-position-at-end builder entry) 355 | (define then (llvm-append-basic-block func)) 356 | (define els (llvm-append-basic-block func)) 357 | (llvm-build-cond-br builder 358 | (llvm-build-int-cmp builder 'int-ugt 359 | (llvm-const-int (llvm-int32-type) 42) 360 | (llvm-const-int (llvm-int32-type) 16)) 361 | then 362 | els) 363 | (llvm-builder-position-at-end builder then) 364 | (llvm-build-ret builder (llvm-const-int (llvm-int32-type) 1234)) 365 | (llvm-builder-position-at-end builder els) 366 | (llvm-build-ret builder (llvm-const-int (llvm-int32-type) 937)) 367 | (check-equal? 1234 (llvm-generic-value->int (llvm-run-function eng func (list)) #f)))) 368 | (module+ test 369 | (check-binary-int32-inst llvm-build-add 20 22 42) 370 | (check-binary-int32-inst llvm-build-sub 4321 321 4000) 371 | (check-binary-int32-inst llvm-build-mul 17 123 2091) 372 | (check-binary-int32-inst llvm-build-udiv 8 2 4) 373 | (check-binary-int32-inst llvm-build-udiv 57 7 8) 374 | (check-binary-int32-inst llvm-build-lshr 4 2 1) 375 | (check-binary-int32-inst llvm-build-shl 4 2 16) 376 | (check-binary-float-inst llvm-build-fadd 2.5 3.82 6.319999694824219) 377 | (check-binary-float-inst llvm-build-fadd 2.5 -3.82 -1.3199999332427979) 378 | (check-binary-float-inst llvm-build-frem 2.1 3.9 2.0999999046325684) 379 | (check-unary-int-inst llvm-build-neg llvm-int32-type 1 -1) 380 | (check-unary-int-inst llvm-build-not llvm-int1-type 1 0) 381 | (check-unary-float-inst llvm-build-fneg llvm-float-type 1. -1.) 382 | (check-unary-float-inst llvm-build-fneg llvm-float-type 2.1 -2.0999999046325684)) 383 | -------------------------------------------------------------------------------- /definer.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | (provide define-llvm) 3 | (require ffi/unsafe 4 | ffi/unsafe/define 5 | racket/string 6 | racket/port 7 | racket/system) 8 | 9 | (define (get-llvm-lib-dir) 10 | (let ([path (find-executable-path "llvm-config")]) 11 | (list (string-trim 12 | (with-output-to-string 13 | (λ () (system* path "--libdir"))))))) 14 | 15 | (define-ffi-definer define-llvm (ffi-lib "libLLVM" '("15" "14" "13" "12" "11" #f) 16 | #:get-lib-dirs get-llvm-lib-dir)) 17 | -------------------------------------------------------------------------------- /error.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | (provide llvm-dispose-message) 3 | (require ffi/unsafe 4 | "definer.rkt") 5 | 6 | (define-llvm llvm-dispose-message (_fun (_ptr i _string) -> _void) 7 | #:c-id LLVMDisposeMessage) 8 | -------------------------------------------------------------------------------- /examples/optimization.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (require racket-llvm) 3 | 4 | ; let's create an if-else function 5 | 6 | (define mod (llvm-module "optimizeMe")) 7 | 8 | (define eng (llvm-create-execution-engine-for-module mod)) 9 | 10 | (llvm-link-in-mcjit) 11 | 12 | (define builder (llvm-builder-create)) 13 | 14 | (define if-func (llvm-add-function mod 15 | "if" 16 | (llvm-function-type (llvm-int32-type)))) 17 | 18 | (llvm-builder-position-at-end builder (llvm-append-basic-block if-func)) 19 | 20 | (define cmp (llvm-build-int-cmp builder 21 | 'int-eq 22 | (llvm-const-int (llvm-int32-type) 123) 23 | (llvm-const-int (llvm-int32-type) 321) 24 | "equal")) 25 | 26 | (define then (llvm-append-basic-block if-func)) 27 | (define els (llvm-append-basic-block if-func)) 28 | 29 | (define cond-br (llvm-build-cond-br builder cmp then els)) 30 | 31 | (llvm-builder-position-at-end builder then) 32 | (void (llvm-build-ret builder (llvm-const-int (llvm-int32-type) 111))) 33 | 34 | (llvm-builder-position-at-end builder els) 35 | (define sum (llvm-build-add builder 36 | (llvm-const-int (llvm-int32-type) 222) 37 | (llvm-const-int (llvm-int32-type) 93281) 38 | "sum")) 39 | (void (llvm-build-ret builder sum)) 40 | 41 | (llvm-module-verify mod) 42 | (llvm-function-verify if-func) 43 | 44 | (displayln "before:") 45 | (display (llvm-module->string mod)) 46 | 47 | ; let's do an optimization pass 48 | (define pass-manager (llvm-pass-manager-create)) 49 | (define pass-manager-builder (llvm-pass-manager-builder-create)) 50 | (llvm-pass-manager-builder-set-opt-level pass-manager-builder 3) 51 | (llvm-pass-manager-builder-populate-module-pass-manager pass-manager-builder pass-manager) 52 | 53 | (displayln "did the pass change anything?") 54 | (llvm-pass-manager-run pass-manager mod) 55 | 56 | (displayln "after:") 57 | (display (llvm-module->string mod)) 58 | -------------------------------------------------------------------------------- /examples/printf.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | (require racket-llvm) 3 | 4 | (define mod (llvm-module "printf example")) 5 | 6 | (define builder (llvm-builder-create)) 7 | 8 | (define printf-ty (llvm-function-type (llvm-int32-type) 9 | (list (llvm-pointer-type (llvm-int8-type))) 10 | #t)) 11 | (define printf-func (llvm-add-function mod "printf" printf-ty)) 12 | (define main-func (llvm-add-function mod "main" (llvm-function-type (llvm-int32-type)))) 13 | 14 | (define entry (llvm-append-basic-block main-func)) 15 | (llvm-builder-position-at-end builder entry) 16 | (define str (llvm-build-string-ptr builder "Hello, World!\n")) 17 | (llvm-build-call2 builder printf-ty printf-func (list str)) 18 | (void (llvm-build-ret builder (llvm-const-int (llvm-int32-type) 0))) 19 | 20 | (llvm-module-verify mod) 21 | (displayln (llvm-module->string mod)) 22 | 23 | (define engine (llvm-create-execution-engine-for-module mod)) 24 | (void (llvm-run-function engine main-func '())) 25 | -------------------------------------------------------------------------------- /examples/show-function-cfg.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | 3 | (require racket-llvm) 4 | 5 | ; let's create an if-else function 6 | 7 | (define mod (llvm-module "optimizeMe")) 8 | 9 | (define eng (llvm-create-execution-engine-for-module mod)) 10 | 11 | (llvm-link-in-mcjit) 12 | 13 | (define builder (llvm-builder-create)) 14 | 15 | (define if-func (llvm-add-function mod 16 | "if" 17 | (llvm-function-type (llvm-int32-type)))) 18 | 19 | (llvm-builder-position-at-end builder (llvm-append-basic-block if-func)) 20 | 21 | (define cmp (llvm-build-int-cmp builder 22 | 'int-eq 23 | (llvm-const-int (llvm-int32-type) 123) 24 | (llvm-const-int (llvm-int32-type) 321) 25 | "equal")) 26 | 27 | (define then (llvm-append-basic-block if-func)) 28 | (define els (llvm-append-basic-block if-func)) 29 | 30 | (llvm-build-cond-br builder cmp then els) 31 | 32 | (llvm-builder-position-at-end builder then) 33 | (void (llvm-build-ret builder (llvm-const-int (llvm-int32-type) 111))) 34 | 35 | (llvm-builder-position-at-end builder els) 36 | (define sum (llvm-build-add builder 37 | (llvm-const-int (llvm-int32-type) 222) 38 | (llvm-const-int (llvm-int32-type) 93281) 39 | "sum")) 40 | (void (llvm-build-ret builder sum)) 41 | 42 | (llvm-module-verify mod) 43 | 44 | (llvm-view-function-cfg if-func) 45 | (llvm-view-function-cfg-only if-func) 46 | -------------------------------------------------------------------------------- /examples/sum.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | 3 | (require racket-llvm) 4 | 5 | (define mod (llvm-module "test_mod")) 6 | 7 | (define sum (llvm-add-function mod "sum" (llvm-function-type (llvm-int32-type) (list (llvm-int32-type) (llvm-int32-type))))) 8 | (define entry (llvm-append-basic-block sum)) 9 | (define builder (llvm-builder-create)) 10 | (llvm-builder-position-at-end builder entry) 11 | (llvm-build-ret builder (llvm-build-add builder (llvm-get-param sum 0) (llvm-get-param sum 1))) 12 | 13 | (llvm-module-verify mod) 14 | 15 | (define eng (llvm-create-execution-engine-for-module mod)) 16 | 17 | (llvm-link-in-mcjit) 18 | 19 | (define (ask-integer) 20 | (printf "enter integer: ") 21 | (read)) 22 | (define-values (x y) (values (ask-integer) (ask-integer))) 23 | 24 | (define res (llvm-run-function eng sum 25 | (list (llvm-create-generic-value-of-int (llvm-int32-type) x #f) 26 | (llvm-create-generic-value-of-int (llvm-int32-type) y #f)))) 27 | (printf "JIT result: ~a\n" (llvm-generic-value->int res #f)) 28 | -------------------------------------------------------------------------------- /examples/write-bitcode.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (require racket-llvm) 3 | 4 | (define mod (llvm-module "test_mod")) 5 | 6 | (define sum (llvm-add-function mod "sum" (llvm-function-type (llvm-int32-type) (list (llvm-int32-type) (llvm-int32-type))))) 7 | (define entry (llvm-append-basic-block sum)) 8 | (define builder (llvm-builder-create)) 9 | (llvm-builder-position-at-end builder entry) 10 | (llvm-build-ret builder (llvm-build-add builder (llvm-get-param sum 0) (llvm-get-param sum 1))) 11 | 12 | (llvm-module-verify mod) 13 | (llvm-write-bitcode-to-file mod "test.bc") 14 | -------------------------------------------------------------------------------- /info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | (define collection "racket-llvm") 3 | (define deps '("base" 4 | "scribble-lib")) 5 | (define build-deps '("scribble-lib" 6 | "racket-doc" 7 | "rackunit-lib" 8 | "at-exp-lib")) 9 | (define scribblings '(("scribblings/racket-llvm.scrbl" (multi-page)))) 10 | (define pkg-desc "LLVM C-API bindings for Racket") 11 | (define version "0.0") 12 | (define license '(Apache-2.0 OR MIT)) 13 | (define pkg-authors '(dannypsnl)) 14 | 15 | (define test-omit-paths '("examples")) 16 | -------------------------------------------------------------------------------- /jit.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (provide llvm-link-in-mcjit 3 | llvm-create-execution-engine-for-module 4 | llvm-run-function 5 | llvm-create-generic-value-of-int 6 | llvm-create-generic-value-of-float 7 | llvm-generic-value->int 8 | llvm-generic-value->float 9 | ; types 10 | _LLVMExecutionEngineRef 11 | LLVMExecutionEngineRef?) 12 | (require ffi/unsafe 13 | "definer.rkt" 14 | "ref.rkt" 15 | "error.rkt") 16 | 17 | (define-cpointer-type _LLVMExecutionEngineRef #:tag 'LLVMOpaqueExecutionEngine) 18 | 19 | (define-llvm llvm-link-in-mcjit (_fun -> _void) #:c-id LLVMLinkInMCJIT) 20 | 21 | (define-llvm llvm-create-execution-engine-for-module 22 | (_fun (eng : (_ptr o _LLVMExecutionEngineRef)) 23 | _LLVMModuleRef 24 | (err : (_ptr o _string)) 25 | -> (result : _int) 26 | -> (cond 27 | [err (llvm-dispose-message err)] 28 | [(not (= 0 result)) (error "Failed to create execution engine")] 29 | [else eng])) 30 | #:c-id LLVMCreateExecutionEngineForModule) 31 | 32 | (define-llvm llvm-run-function (_fun _LLVMExecutionEngineRef 33 | _LLVMValueRef 34 | (_int = (length args)) 35 | (args : (_list i _LLVMGenericValueRef)) 36 | -> _LLVMGenericValueRef) 37 | #:c-id LLVMRunFunction) 38 | 39 | (define-llvm llvm-create-generic-value-of-int (_fun _LLVMTypeRef 40 | _int ; value 41 | _bool ; signed? 42 | -> _LLVMGenericValueRef) 43 | #:c-id LLVMCreateGenericValueOfInt) 44 | 45 | (define-llvm llvm-create-generic-value-of-float (_fun _LLVMTypeRef 46 | _double ; value 47 | -> _LLVMGenericValueRef) 48 | #:c-id LLVMCreateGenericValueOfFloat) 49 | 50 | (define-llvm llvm-generic-value->int (_fun _LLVMGenericValueRef _bool -> _llong) 51 | #:c-id LLVMGenericValueToInt) 52 | 53 | (define-llvm llvm-generic-value->float (_fun _LLVMTypeRef _LLVMGenericValueRef -> _double) 54 | #:c-id LLVMGenericValueToFloat) 55 | -------------------------------------------------------------------------------- /main.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | (provide (all-from-out 3 | "definer.rkt" 4 | "ref.rkt" 5 | "module.rkt" 6 | "types.rkt" 7 | "builder.rkt" 8 | "bit-writer.rkt" 9 | "jit.rkt" 10 | "pass-manager.rkt")) 11 | (require "definer.rkt" 12 | "ref.rkt" 13 | "module.rkt" 14 | "types.rkt" 15 | "builder.rkt" 16 | "bit-writer.rkt" 17 | "jit.rkt" 18 | "pass-manager.rkt") 19 | -------------------------------------------------------------------------------- /module.rkt: -------------------------------------------------------------------------------- 1 | #lang at-exp racket 2 | (provide llvm-module 3 | llvm-module-verify 4 | llvm-function-verify 5 | llvm-module->string 6 | llvm-get-module-context 7 | ; struct 8 | llvm-add-struct-type 9 | ; global 10 | llvm-add-global 11 | llvm-get-named-global 12 | llvm-set-initializer 13 | ; function 14 | llvm-add-function 15 | llvm-get-named-function 16 | llvm-view-function-cfg 17 | llvm-view-function-cfg-only) 18 | (require ffi/unsafe 19 | "definer.rkt" 20 | "ref.rkt" 21 | "types.rkt" 22 | "error.rkt") 23 | 24 | (define-llvm llvm-module (_fun _string -> _LLVMModuleRef) 25 | #:c-id LLVMModuleCreateWithName) 26 | 27 | (define _LLVMVerifierFailureAction 28 | (_enum '(llvm-abort-process-action 29 | llvm-print-message-action 30 | llvm-return-status-action))) 31 | 32 | (define-llvm llvm-module-verify (_fun _LLVMModuleRef 33 | (_LLVMVerifierFailureAction = 'llvm-return-status-action) 34 | (err : (_ptr o _string)) 35 | -> (failure : _bool) 36 | -> (when failure (llvm-dispose-message err))) 37 | #:c-id LLVMVerifyModule) 38 | 39 | (define-llvm llvm-function-verify (_fun _LLVMValueRef 40 | (_LLVMVerifierFailureAction = 'llvm-return-status-action) 41 | (err : (_ptr o _string)) 42 | -> (failure : _bool) 43 | -> (when failure (llvm-dispose-message err))) 44 | #:c-id LLVMVerifyFunction) 45 | 46 | (define-llvm llvm-module->string (_fun _LLVMModuleRef -> _string) 47 | #:c-id LLVMPrintModuleToString) 48 | 49 | (define-llvm llvm-get-module-context (_fun _LLVMModuleRef -> _LLVMContextRef) 50 | #:c-id LLVMGetModuleContext) 51 | 52 | (define (llvm-add-struct-type mod element-types [pack? #f]) 53 | (define ctx (llvm-get-module-context mod)) 54 | (define struct-ty (llvm-struct-create-named ctx "")) 55 | (llvm-struct-set-body struct-ty element-types pack?) 56 | struct-ty) 57 | 58 | (define-llvm llvm-add-function (_fun _LLVMModuleRef _string _LLVMTypeRef 59 | -> _LLVMValueRef) 60 | #:c-id LLVMAddFunction) 61 | (define-llvm llvm-get-named-function (_fun _LLVMModuleRef _string -> _LLVMValueRef) 62 | #:c-id LLVMGetNamedFunction) 63 | 64 | (define-llvm llvm-add-global (_fun _LLVMModuleRef _LLVMTypeRef _string -> _LLVMValueRef) 65 | #:c-id LLVMAddGlobal) 66 | (define-llvm llvm-get-named-global (_fun _LLVMModuleRef _string -> _LLVMValueRef) 67 | #:c-id LLVMGetNamedGlobal) 68 | (define-llvm llvm-set-initializer (_fun (global-var constant-val) :: 69 | (global-var : _LLVMValueRef) 70 | (constant-val : _LLVMValueRef) 71 | -> _void) 72 | #:c-id LLVMSetInitializer) 73 | 74 | (define-llvm llvm-view-function-cfg (_fun _LLVMValueRef -> _void) 75 | #:c-id LLVMViewFunctionCFG) 76 | (define-llvm llvm-view-function-cfg-only (_fun _LLVMValueRef -> _void) 77 | #:c-id LLVMViewFunctionCFGOnly) 78 | 79 | (module+ test 80 | (require rackunit 81 | "types.rkt") 82 | 83 | (let ([mod (llvm-module "test_mod")]) 84 | (define ft (llvm-function-type (llvm-int32-type) 85 | (list (llvm-pointer-type (llvm-int8-type))) 86 | #t)) 87 | (define printf-fun (llvm-add-function mod "printf" ft)) 88 | (check-equal? (llvm-module->string mod) 89 | "; ModuleID = 'test_mod' 90 | source_filename = \"test_mod\" 91 | 92 | declare i32 @printf(ptr, ...) 93 | "))) 94 | (module+ test 95 | (require rackunit 96 | "types.rkt") 97 | (let ([mod (llvm-module "test_mod")]) 98 | (define struct-ty (llvm-add-struct-type mod (list (llvm-int8-type) (llvm-double-type)))) 99 | (llvm-add-global mod struct-ty "hello") 100 | (check-equal? (llvm-module->string mod) 101 | "; ModuleID = 'test_mod' 102 | source_filename = \"test_mod\" 103 | 104 | %0 = type { i8, double } 105 | 106 | @hello = external global %0 107 | "))) 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /pass-manager.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (provide llvm-pass-manager-builder-create 3 | llvm-pass-manager-builder-set-opt-level 4 | llvm-pass-manager-builder-populate-module-pass-manager 5 | llvm-pass-manager-builder-populate-function-pass-manager 6 | llvm-pass-manager-create 7 | llvm-pass-manager-run 8 | llvm-function-pass-manager-create) 9 | (require ffi/unsafe 10 | "definer.rkt" 11 | "ref.rkt") 12 | 13 | #| Pass manager builder |# 14 | (define-llvm llvm-pass-manager-builder-create (_fun -> _LLVMPassManagerBuilderRef) 15 | #:c-id LLVMPassManagerBuilderCreate) 16 | 17 | (define-llvm llvm-pass-manager-builder-set-opt-level 18 | (_fun _LLVMPassManagerBuilderRef 19 | _uint 20 | -> _void) 21 | #:c-id LLVMPassManagerBuilderSetOptLevel) 22 | 23 | (define-llvm llvm-pass-manager-builder-populate-module-pass-manager 24 | (_fun _LLVMPassManagerBuilderRef 25 | _LLVMPassManagerRef 26 | -> _void) 27 | #:c-id LLVMPassManagerBuilderPopulateModulePassManager) 28 | 29 | (define-llvm llvm-pass-manager-builder-populate-function-pass-manager 30 | (_fun _LLVMPassManagerBuilderRef 31 | _LLVMPassManagerRef 32 | -> _void) 33 | #:c-id LLVMPassManagerBuilderPopulateFunctionPassManager) 34 | 35 | #| Whole module pass manager |# 36 | (define-llvm llvm-pass-manager-create (_fun -> _LLVMPassManagerRef) 37 | #:c-id LLVMCreatePassManager) 38 | 39 | (define-llvm llvm-pass-manager-run (_fun _LLVMPassManagerRef 40 | _LLVMModuleRef 41 | -> _bool) 42 | #:c-id LLVMRunPassManager) 43 | 44 | #| Function pass manager |# 45 | (define-llvm llvm-function-pass-manager-create (_fun _LLVMModuleRef -> _LLVMPassManagerRef) 46 | #:c-id LLVMCreateFunctionPassManagerForModule) 47 | -------------------------------------------------------------------------------- /ref.rkt: -------------------------------------------------------------------------------- 1 | #lang at-exp racket/base 2 | (provide _LLVMModuleRef 3 | _LLVMContextRef 4 | _LLVMTypeRef 5 | _LLVMValueRef 6 | _LLVMBasicBlockRef 7 | _LLVMBuilderRef 8 | _LLVMGenericValueRef 9 | _LLVMPassManagerRef 10 | _LLVMPassManagerBuilderRef 11 | LLVMModuleRef? 12 | LLVMContextRef? 13 | LLVMTypeRef? 14 | LLVMValueRef? 15 | LLVMBasicBlockRef? 16 | LLVMBuilderRef? 17 | LLVMGenericValueRef? 18 | LLVMPassManagerRef? 19 | LLVMPassManagerBuilderRef?) 20 | (require ffi/unsafe) 21 | 22 | (define-cpointer-type _LLVMModuleRef #:tag 'LLVMOpaqueModule) 23 | (define-cpointer-type _LLVMContextRef #:tag 'LLVMContextRef) 24 | (define-cpointer-type _LLVMTypeRef #:tag 'LLVMOpaqueType) 25 | (define-cpointer-type _LLVMValueRef #:tag 'LLVMOpaqueValue) 26 | (define-cpointer-type _LLVMBasicBlockRef #:tag 'LLVMOpaqueBasicBlock) 27 | (define-cpointer-type _LLVMBuilderRef #:tag 'LLVMOpaqueBuilder) 28 | (define-cpointer-type _LLVMGenericValueRef #:tag 'LLVMOpaqueGenericValue) 29 | (define-cpointer-type _LLVMPassManagerRef #:tag 'LLVMOpaquePassManager) 30 | (define-cpointer-type _LLVMPassManagerBuilderRef #:tag 'LLVMOpaquePassManagerBuilder) 31 | -------------------------------------------------------------------------------- /scribblings/.gitignore: -------------------------------------------------------------------------------- 1 | *.css 2 | *.js 3 | *.html 4 | -------------------------------------------------------------------------------- /scribblings/builder.scrbl: -------------------------------------------------------------------------------- 1 | #lang scribble/manual 2 | @require[@for-label[racket-llvm 3 | racket/base]] 4 | 5 | @title{Builder} 6 | 7 | @defproc[(llvm-append-basic-block [function LLVMValueRef?]) LLVMBasicBlockRef?]{create new basic block for given @racket[function]} 8 | @defproc[(llvm-builder-create) LLVMBuilderRef?]{new @racket[LLVMBuilderRef?]} 9 | @defproc[(llvm-builder-position-at-end [builder LLVMBuilderRef?] [basic-block LLVMBasicBlockRef?]) void?]{ 10 | set @racket[builder] insert position to the end of @racket[basic-block] 11 | } 12 | @defproc[(llvm-get-param [function LLVMValueRef?] [index integer?]) LLVMValueRef?]{get parameter from @racket[function] by index} 13 | 14 | @defproc[(llvm-build-ret [builder LLVMBuilderRef?] [value LLVMValueRef?]) LLVMValueRef?]{} 15 | @defproc[(llvm-build-ret-void [builder LLVMBuilderRef?]) LLVMValueRef?]{} 16 | -------------------------------------------------------------------------------- /scribblings/jit.scrbl: -------------------------------------------------------------------------------- 1 | #lang scribble/manual 2 | @require[@for-label[racket-llvm 3 | racket/base]] 4 | 5 | @title{JIT} 6 | 7 | @defthing[_LLVMExecutionEngineRef ctype?]{pointer to LLVMExecutionEngine} 8 | @defproc[(LLVMExecutionEngineRef? [v any/c]) boolean?]{check a value is LLVMExecutionEngine or not} 9 | 10 | @defproc[(llvm-create-execution-engine-for-module [module LLVMModuleRef?]) 11 | LLVMExecutionEngineRef? 12 | ]{ 13 | return an execution engine for given module 14 | } 15 | 16 | @defproc[(llvm-run-function [engine LLVMExecutionEngineRef?] [function LLVMValueRef?]) 17 | LLVMGenericValueRef? 18 | ]{ 19 | run given @racket[function] on @racket[engine], it returns result as @racket[LLVMGenericValueRef?] 20 | } 21 | 22 | @defproc[(llvm-create-generic-value-of-int [type LLVMTypeRef?] [value integer?] [signed? boolean?]) 23 | LLVMGenericValueRef? 24 | ]{ 25 | Converted given @racket[value] to @racket[LLVMGenericValueRef?]. 26 | @racket[type] decided it's LLVM type corresponding. 27 | @racket[signed?] decided it's signed integer or not. 28 | } 29 | 30 | @defproc[(llvm-create-generic-value-of-float [type LLVMTypeRef?] [value number?]) 31 | LLVMGenericValueRef? 32 | ]{ 33 | Converted given @racket[value] to @racket[LLVMGenericValueRef?]. 34 | @racket[type] decided it's LLVM type corresponding. 35 | } 36 | 37 | @defproc[(llvm-generic-value->int [generic-value LLVMGenericValueRef?] [signed? boolean?]) 38 | integer? 39 | ]{ 40 | convert @racket[generic-value] back to racket @racket[integer?]. @racket[signed?] decided treat it as a signed integer or not. 41 | } 42 | 43 | @defproc[(llvm-generic-value->float [type LLVMTypeRef?] [generic-value LLVMGenericValueRef?]) 44 | number? 45 | ]{ 46 | convert @racket[generic-value] back to racket @racket[number?] 47 | } 48 | -------------------------------------------------------------------------------- /scribblings/llvm-structure.scrbl: -------------------------------------------------------------------------------- 1 | #lang scribble/manual 2 | @require[@for-label[racket-llvm 3 | racket/base]] 4 | 5 | @title{LLVM Structure} 6 | 7 | @defthing*[([_LLVMModuleRef ctype?] 8 | [_LLVMContextRef ctype?] 9 | [_LLVMTypeRef ctype?] 10 | [_LLVMValueRef ctype?] 11 | [_LLVMBasicBlockRef ctype?] 12 | [_LLVMBuilderRef ctype?] 13 | [_LLVMGenericValueRef ctype?] 14 | [_LLVMPassManagerRef ctype?] 15 | [_LLVMPassManagerBuilderRef ctype?])]{ 16 | LLVM primitive types. 17 | } 18 | 19 | @defproc[(LLVMModuleRef? [v any/c]) 20 | boolean? 21 | ]{ 22 | check a value is LLVMOpaqueModule or not 23 | } 24 | @defproc[(LLVMContextRef? [v any/c]) 25 | boolean? 26 | ]{ 27 | check a value is LLVMContextRef or not 28 | } 29 | @defproc[(LLVMTypeRef? [v any/c]) 30 | boolean? 31 | ]{ 32 | check a value is LLVMOpaqueType or not 33 | } 34 | @defproc[(LLVMValueRef? [v any/c]) 35 | boolean? 36 | ]{ 37 | check a value is LLVMOpaqueValue or not 38 | } 39 | @defproc[(LLVMBasicBlockRef? [v any/c]) 40 | boolean? 41 | ]{ 42 | check a value is LLVMOpaqueBasicBlock or not 43 | } 44 | @defproc[(LLVMBuilderRef? [v any/c]) 45 | boolean? 46 | ]{ 47 | check a value is LLVMOpaqueBuilder or not 48 | } 49 | @defproc[(LLVMGenericValueRef? [v any/c]) 50 | boolean? 51 | ]{ 52 | check a value is LLVMOpaqueGenericValue or not 53 | } 54 | @defproc[(LLVMPassManagerRef? [v any/c]) 55 | boolean? 56 | ]{ 57 | check a value is LLVMOpaquePassManager or not 58 | } 59 | @defproc[(LLVMPassManagerBuilderRef? [v any/c]) 60 | boolean? 61 | ]{ 62 | check a value is LLVMOpaquePassManagerBuilder or not 63 | } 64 | -------------------------------------------------------------------------------- /scribblings/module.scrbl: -------------------------------------------------------------------------------- 1 | #lang scribble/manual 2 | @require[@for-label[racket-llvm 3 | racket/base]] 4 | 5 | @title{Module} 6 | 7 | @defproc[(llvm-module [module-name string?]) 8 | LLVMModuleRef? 9 | ]{ 10 | @racket[llvm-module] returns a module, the core concept in LLVM. We puts global variables, functions, and type definitions in module. 11 | } 12 | 13 | @defproc[(llvm-module-verify [module LLVMModuleRef?]) 14 | boolean? 15 | ]{ 16 | verify given module 17 | } 18 | 19 | @defproc[(llvm-module->string [module LLVMModuleRef?]) 20 | string? 21 | ]{ 22 | convert given module as string 23 | } 24 | 25 | @defproc[(llvm-add-function [module LLVMModuleRef?] [function-name string?] [function-type LLVMTypeRef?]) 26 | LLVMValueRef? 27 | ]{ 28 | Add function into given module, return a function value. The function name is given by function-name, the function type is given by function-type. 29 | } 30 | 31 | @defproc[(llvm-add-global [module LLVMModuleRef?] [var-type LLVMTypeRef?] [var-name string?]) 32 | LLVMValueRef? 33 | ]{ 34 | Add a global variable into given module. 35 | } 36 | 37 | @defproc[(llvm-get-named-global [module LLVMModuleRef?] [global-variable-name string?]) 38 | LLVMValueRef? 39 | ]{ 40 | Get global variable reference by its name. 41 | } 42 | 43 | @defproc[(llvm-write-bitcode-to-file [module LLVMModuleRef?] [file-path string?]) 44 | void? 45 | ]{ 46 | Write @code{module} as content of @code{file-path}. 47 | } 48 | -------------------------------------------------------------------------------- /scribblings/racket-llvm.scrbl: -------------------------------------------------------------------------------- 1 | #lang scribble/manual 2 | @require[scribble/extract 3 | @for-label[racket-llvm 4 | racket/base]] 5 | 6 | @title{Racket LLVM} 7 | 8 | A racket LLVM C-API bindings, document will show you how to use this library as simple as possible. 9 | 10 | @defmodule[racket-llvm] 11 | @defform[(define-llvm id function-type maybe-c-id) 12 | #:grammar ([maybe-c-id (code:line #:c-id c-function-id)]) 13 | ]{ 14 | @racket[define-llvm] takes an id as its produces function name, then is a C @racket[_fun] defined types for C function. Finally, takes the link name of C function. Using @racket[define-llvm] to extend any functions missing in this library will be important. 15 | } 16 | 17 | @include-section["module.scrbl"] 18 | @include-section["builder.scrbl"] 19 | @include-section["jit.scrbl"] 20 | @include-section["llvm-structure.scrbl"] 21 | -------------------------------------------------------------------------------- /types.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | (provide (all-defined-out)) 3 | (require ffi/unsafe 4 | "definer.rkt" 5 | "ref.rkt") 6 | 7 | (define-llvm llvm-typeof (_fun _LLVMValueRef -> _LLVMTypeRef) #:c-id LLVMTypeOf) 8 | 9 | (define-llvm llvm-void-type (_fun -> _LLVMTypeRef) #:c-id LLVMVoidType) 10 | (define-llvm llvm-label-type (_fun -> _LLVMTypeRef) #:c-id LLVMLabelType) 11 | (define-llvm llvm-x86mmx-type (_fun -> _LLVMTypeRef) #:c-id LLVMX86MMXType) 12 | (define-llvm llvm-x86amx-type (_fun -> _LLVMTypeRef) #:c-id LLVMX86AMXType) 13 | 14 | (define-llvm llvm-int-type (_fun _int -> _LLVMTypeRef) #:c-id LLVMIntType) 15 | (define-llvm llvm-int1-type (_fun -> _LLVMTypeRef) #:c-id LLVMInt1Type) 16 | (define-llvm llvm-int8-type (_fun -> _LLVMTypeRef) #:c-id LLVMInt8Type) 17 | (define-llvm llvm-int16-type (_fun -> _LLVMTypeRef) #:c-id LLVMInt16Type) 18 | (define-llvm llvm-int32-type (_fun -> _LLVMTypeRef) #:c-id LLVMInt32Type) 19 | (define-llvm llvm-int64-type (_fun -> _LLVMTypeRef) #:c-id LLVMInt64Type) 20 | (define-llvm llvm-int128-type (_fun -> _LLVMTypeRef) #:c-id LLVMInt128Type) 21 | 22 | (define-llvm llvm-half-type (_fun -> _LLVMTypeRef) #:c-id LLVMHalfType) 23 | (define-llvm llvm-bfloat-type (_fun -> _LLVMTypeRef) #:c-id LLVMBFloatType) 24 | (define-llvm llvm-float-type (_fun -> _LLVMTypeRef) #:c-id LLVMFloatType) 25 | (define-llvm llvm-double-type (_fun -> _LLVMTypeRef) #:c-id LLVMDoubleType) 26 | (define-llvm llvm-x86fp80-type (_fun -> _LLVMTypeRef) #:c-id LLVMX86FP80Type) 27 | (define-llvm llvm-fp128-type (_fun -> _LLVMTypeRef) #:c-id LLVMFP128Type) 28 | (define-llvm llvm-ppcfp128-type (_fun -> _LLVMTypeRef) #:c-id LLVMPPCFP128Type) 29 | 30 | (define-llvm llvm-vector-type (_fun _LLVMTypeRef _int -> _LLVMTypeRef) #:c-id LLVMVectorType) 31 | (define-llvm llvm-scalable-vector-type (_fun _LLVMTypeRef _int -> _LLVMTypeRef) #:c-id LLVMScalableVectorType) 32 | 33 | (define _LLVMAddressSpace 34 | (_enum '(addr-space-generic = 0 35 | addr-space-global 36 | addr-space-shared 37 | addr-space-const 38 | addr-space-local 39 | addr-space-param = 101))) 40 | (define-llvm llvm-pointer-type (_fun (element_type [addr-space 'addr-space-generic]) :: 41 | (element_type : _LLVMTypeRef) 42 | (addr-space : _LLVMAddressSpace) 43 | -> _LLVMTypeRef) 44 | #:c-id LLVMPointerType) 45 | (define-llvm llvm-array-type (_fun (element_type element_count) :: 46 | (element_type : _LLVMTypeRef) 47 | (element_count : _int) 48 | -> _LLVMTypeRef) 49 | #:c-id LLVMArrayType) 50 | 51 | (define-llvm llvm-function-type (_fun (return-type [param-types (list)] [variadic? #f]) :: 52 | (return-type : _LLVMTypeRef) 53 | (param-types : (_list i _LLVMTypeRef)) 54 | (_int = (length param-types)) ; num params 55 | (variadic? : _bool) 56 | -> _LLVMTypeRef) 57 | #:c-id LLVMFunctionType) 58 | 59 | (define-llvm llvm-struct-type (_fun ([element_types (list)] [pack? #f]) :: 60 | (element_types : (_list i _LLVMTypeRef)) 61 | (_int = (length element_types)) 62 | (pack? : _bool) 63 | -> _LLVMTypeRef) 64 | #:c-id LLVMStructType) 65 | (define-llvm llvm-struct-create-named (_fun _LLVMContextRef _string -> _LLVMTypeRef) 66 | #:c-id LLVMStructCreateNamed) 67 | (define-llvm llvm-struct-set-body (_fun (struct-ty [element_types (list)] [pack? #f]) :: 68 | (struct-ty : _LLVMTypeRef) 69 | (element_types : (_list i _LLVMTypeRef)) 70 | (_int = (length element_types)) 71 | (pack? : _bool) 72 | -> _void) 73 | #:c-id LLVMStructSetBody) 74 | -------------------------------------------------------------------------------- /value.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | (provide (all-defined-out)) 3 | (require ffi/unsafe 4 | "definer.rkt" 5 | "ref.rkt") 6 | 7 | (define-llvm llvm-constant? (_fun _LLVMValueRef -> _bool) #:c-id LLVMIsConstant) 8 | (define-llvm llvm-undef? (_fun _LLVMValueRef -> _bool) #:c-id LLVMIsUndef) 9 | (define-llvm llvm-poison? (_fun _LLVMValueRef -> _bool) #:c-id LLVMIsPoison) 10 | --------------------------------------------------------------------------------