purego_core.go raw
1 //go:build purego
2
3 package llvm
4
5 import (
6 "fmt"
7 "runtime"
8 "unsafe"
9
10 "github.com/ebitengine/purego"
11 )
12
13 // Function pointers resolved at init time.
14 var (
15 // Context
16 symContextCreate uintptr
17 symContextDispose uintptr
18 symGetGlobalCtx uintptr
19 symGetMDKindIDCtx uintptr
20 symGetMDKindID uintptr
21
22 // Attribute
23 symGetEnumAttrKindForName uintptr
24 symCreateEnumAttr uintptr
25 symCreateTypeAttr uintptr
26 symCreateStringAttr uintptr
27 symGetEnumAttrKind uintptr
28 symGetEnumAttrValue uintptr
29 symGetStringAttrKind uintptr
30 symGetStringAttrValue uintptr
31
32 // Module
33 symModuleCreateWithNameInCtx uintptr
34 symDisposeModule uintptr
35 symGetDataLayout uintptr
36 symSetDataLayout uintptr
37 symGetTarget uintptr
38 symSetTarget uintptr
39 symGetModuleContext uintptr
40 symGetTypeByName uintptr
41 symPrintModuleToString uintptr
42 symDisposeMessage uintptr
43 symSetModuleInlineAsm uintptr
44 symDumpModule uintptr
45
46 // Integer types
47 symInt1TypeInCtx uintptr
48 symInt8TypeInCtx uintptr
49 symInt16TypeInCtx uintptr
50 symInt32TypeInCtx uintptr
51 symInt64TypeInCtx uintptr
52 symIntTypeInCtx uintptr
53 symGetIntTypeWidth uintptr
54
55 // Float types
56 symFloatTypeInCtx uintptr
57 symDoubleTypeInCtx uintptr
58 symX86FP80TypeInCtx uintptr
59 symFP128TypeInCtx uintptr
60 symPPCFP128TypeInCtx uintptr
61
62 // Function types
63 symFunctionType uintptr
64 symIsFunctionVarArg uintptr
65 symGetReturnType uintptr
66 symCountParamTypes uintptr
67 symGetParamTypes uintptr
68
69 // Struct types
70 symStructTypeInCtx uintptr
71 symStructCreateNamed uintptr
72 symGetStructName uintptr
73 symStructSetBody uintptr
74 symIsPackedStruct uintptr
75 symCountStructElementTypes uintptr
76 symGetStructElementTypes uintptr
77
78 // Sequence types
79 symArrayType uintptr
80 symPointerType uintptr
81 symVectorType uintptr
82 symGetElementType uintptr
83 symGetArrayLength uintptr
84 symGetPointerAddressSpace uintptr
85 symGetVectorSize uintptr
86 symGetNumContainedTypes uintptr
87 symGetSubtypes uintptr
88
89 // Other types
90 symVoidTypeInCtx uintptr
91 symLabelTypeInCtx uintptr
92 symTokenTypeInCtx uintptr
93
94 // Type inspection
95 symGetTypeKind uintptr
96 symGetTypeContext uintptr
97
98 // Value operations
99 symTypeOf uintptr
100 symGetValueName uintptr
101 symSetValueName uintptr
102 symDumpValue uintptr
103 symReplaceAllUsesWith uintptr
104 symHasMetadata uintptr
105 symGetMetadata uintptr
106 symPrintValueToString uintptr
107
108 // Constant scalars
109 symConstInt uintptr
110 symConstFloat uintptr // broken: purego can't pass double in XMM
111 symConstFloatOfStr uintptr
112 symConstNull uintptr
113 symConstAllOnes uintptr
114 symGetUndef uintptr
115 symConstPointerNull uintptr
116 symIsConstant uintptr
117 symIsNull uintptr
118 symIsUndef uintptr
119 symConstIntGetZExt uintptr
120 symConstIntGetSExt uintptr
121 symConstFloatGetDouble uintptr
122
123 // Constant expressions
124 symConstNeg uintptr
125 symConstNSWNeg uintptr
126 symConstNot uintptr
127 symConstAdd uintptr
128 symConstNSWAdd uintptr
129 symConstSub uintptr
130 symConstNSWSub uintptr
131 symConstMul uintptr
132 symConstNSWMul uintptr
133 symConstXor uintptr
134 symConstGEP2 uintptr
135 symConstInBoundsGEP2 uintptr
136 symConstTrunc uintptr
137 symConstSExt uintptr
138 symConstZExt uintptr
139 symConstFPTrunc uintptr
140 symConstFPExt uintptr
141 symConstUIToFP uintptr
142 symConstSIToFP uintptr
143 symConstFPToUI uintptr
144 symConstFPToSI uintptr
145 symConstPtrToInt uintptr
146 symConstIntToPtr uintptr
147 symConstBitCast uintptr
148 symConstICmp uintptr
149 symConstFCmp uintptr
150 symConstExtractValue uintptr
151 symConstInsertValue uintptr
152
153 // Constant aggregates
154 symConstStringInCtx uintptr
155 symConstStructInCtx uintptr
156 symConstArray uintptr
157 symConstNamedStruct uintptr
158
159 // Global values
160 symGetLinkage uintptr
161 symSetLinkage uintptr
162 symGetSection uintptr
163 symSetSection uintptr
164 symGetVisibility uintptr
165 symSetVisibility uintptr
166 symGetAlignment uintptr
167 symSetAlignment uintptr
168
169 // Global variables
170 symAddGlobal uintptr
171 symGetNamedGlobal uintptr
172 symGetFirstGlobal uintptr
173 symGetNextGlobal uintptr
174 symGetInitializer uintptr
175 symSetInitializer uintptr
176 symIsGlobalConstant uintptr
177 symSetGlobalConstant uintptr
178 symSetThreadLocal uintptr
179 symIsThreadLocal uintptr
180 symSetExternallyInit uintptr
181 symIsExternallyInit uintptr
182 symDeleteGlobal uintptr
183 symSetUnnamedAddr uintptr
184 symGetUnnamedAddr uintptr
185
186 // Functions
187 symAddFunction uintptr
188 symGetNamedFunction uintptr
189 symGetFirstFunction uintptr
190 symGetNextFunction uintptr
191 symDeleteFunction uintptr
192 symCountParams uintptr
193 symGetParams uintptr
194 symGetParam uintptr
195 symSetFunctionCallConv uintptr
196 symGetFunctionCallConv uintptr
197 symAddFuncAttr uintptr
198 symGetEnumFuncAttr uintptr
199 symRemoveEnumFuncAttr uintptr
200 symAddTargetDepFuncAttr uintptr
201
202 // Basic blocks
203 symAppendBasicBlockInCtx uintptr
204 symInsertBasicBlockInCtx uintptr
205 symGetBasicBlockParent uintptr
206 symGetEntryBasicBlock uintptr
207 symGetFirstBasicBlock uintptr
208 symGetLastBasicBlock uintptr
209 symGetNextBasicBlock uintptr
210 symGetPreviousBasicBlock uintptr
211 symCountBasicBlocks uintptr
212 symMoveBasicBlockAfter uintptr
213 symMoveBasicBlockBefore uintptr
214 symDeleteBasicBlock uintptr
215 symBasicBlockAsValue uintptr
216
217 // Instructions
218 symGetFirstInstruction uintptr
219 symGetLastInstruction uintptr
220 symGetNextInstruction uintptr
221 symGetInstructionOpcode uintptr
222 symInstructionEraseFromParent uintptr
223 symGetInstructionParent uintptr
224 symIsATerminatorInst uintptr
225
226 // Builder
227 symCreateBuilderInCtx uintptr
228 symPositionBuilderAtEnd uintptr
229 symPositionBuilderBefore uintptr
230 symGetInsertBlock uintptr
231 symDisposeBuilder uintptr
232
233 // Builder - terminators
234 symBuildRetVoid uintptr
235 symBuildRet uintptr
236 symBuildBr uintptr
237 symBuildCondBr uintptr
238 symBuildSwitch uintptr
239 symBuildUnreachable uintptr
240
241 // Builder - arithmetic
242 symBuildAdd uintptr
243 symBuildNSWAdd uintptr
244 symBuildNUWAdd uintptr
245 symBuildFAdd uintptr
246 symBuildSub uintptr
247 symBuildNSWSub uintptr
248 symBuildNUWSub uintptr
249 symBuildFSub uintptr
250 symBuildMul uintptr
251 symBuildNSWMul uintptr
252 symBuildNUWMul uintptr
253 symBuildFMul uintptr
254 symBuildUDiv uintptr
255 symBuildSDiv uintptr
256 symBuildExactSDiv uintptr
257 symBuildFDiv uintptr
258 symBuildURem uintptr
259 symBuildSRem uintptr
260 symBuildFRem uintptr
261 symBuildShl uintptr
262 symBuildLShr uintptr
263 symBuildAShr uintptr
264 symBuildAnd uintptr
265 symBuildOr uintptr
266 symBuildXor uintptr
267 symBuildNeg uintptr
268 symBuildNSWNeg uintptr
269 symBuildNot uintptr
270 symBuildFNeg uintptr
271
272 // Builder - memory
273 symBuildAlloca uintptr
274 symBuildLoad2 uintptr
275 symBuildStore uintptr
276 symBuildGEP2 uintptr
277 symBuildInBoundsGEP2 uintptr
278 symBuildStructGEP2 uintptr
279
280 // Builder - casts
281 symBuildTrunc uintptr
282 symBuildZExt uintptr
283 symBuildSExt uintptr
284 symBuildFPToUI uintptr
285 symBuildFPToSI uintptr
286 symBuildUIToFP uintptr
287 symBuildSIToFP uintptr
288 symBuildFPTrunc uintptr
289 symBuildFPExt uintptr
290 symBuildPtrToInt uintptr
291 symBuildIntToPtr uintptr
292 symBuildBitCast uintptr
293 symBuildPointerCast uintptr
294
295 // Builder - comparisons
296 symBuildICmp uintptr
297 symBuildFCmp uintptr
298
299 // Builder - misc
300 symBuildPhi uintptr
301 symBuildCall2 uintptr
302 symBuildSelect uintptr
303 symBuildExtractValue uintptr
304 symBuildInsertValue uintptr
305
306 // PHI
307 symAddIncoming uintptr
308
309 // Call site
310 symSetInstructionCallConv uintptr
311 symGetInstructionCallConv uintptr
312 symAddCallSiteAttribute uintptr
313 symSetInstrParamAlignment uintptr
314
315 // Metadata (glue functions)
316 symMDString2 uintptr
317 symMDNode2 uintptr
318 symConstantAsMetadata uintptr
319 symSetMetadata2 uintptr
320 symAddNamedMetadataOperand2 uintptr
321 symGlobalObjectAddMetadata uintptr
322
323 // Switch
324 symAddCase uintptr
325
326 // Landing pad
327 symBuildLandingPad uintptr
328 symSetCleanup uintptr
329 symAddClause uintptr
330
331 // Invoke
332 symBuildInvoke2 uintptr
333
334 // Resume
335 symBuildResume uintptr
336
337 // Atomics
338 symBuildFence uintptr
339 symBuildAtomicRMW uintptr
340 symBuildAtomicCmpXchg uintptr
341 symSetOrdering uintptr
342 symSetAtomicSingleThread uintptr
343
344 // Inline asm (glue)
345 symGoGetInlineAsm uintptr
346
347 // Debug location (glue for set, C API for get)
348 symGoSetCurrentDebugLocation uintptr
349 symGetCurrentDebugLocation2 uintptr
350 symDILocationGetLine uintptr
351 symDILocationGetColumn uintptr
352 symDILocationGetScope uintptr
353 symDILocationGetInlinedAt uintptr
354
355 // Module print/verify
356 symVerifyModule uintptr
357 symVerifyFunction uintptr
358
359 // Comdat
360 symGetOrInsertComdat uintptr
361 symComdatSetSelectionKind uintptr
362 symSetComdat uintptr
363
364 // Value type checks (IsA*)
365 symIsAAllocaInst uintptr
366 symIsAConstant uintptr
367 symIsAConstantInt uintptr
368 symIsAFunction uintptr
369 symIsAGlobalValue uintptr
370 symIsAInstruction uintptr
371 symIsAPHINode uintptr
372 symIsAUndefValue uintptr
373
374 // Value misc
375 symIsDeclaration uintptr
376 symGlobalGetValType uintptr
377 symGetLastParam uintptr
378 symGetOperand uintptr
379 symGetNumOperands uintptr
380 symSetVolatile uintptr
381 symInstructionRemoveFromParent uintptr
382 symInstructionGetDebugLoc uintptr
383 symCountIncoming uintptr
384 symGetIncomingValue uintptr
385 symGetIncomingBlock uintptr
386
387 // Builder misc
388 symInsertIntoBuilder uintptr
389 symBuildBinOp uintptr
390
391 // Additional Value methods
392 symGetCalledValue uintptr
393 symGetAllocatedType uintptr
394 symGetGEPSourceElementType uintptr
395 symGetNumIndices uintptr
396 symGetIndices uintptr
397 symGetICmpPredicate uintptr
398 symGetFCmpPredicate uintptr
399 symIsAReturnInst uintptr
400 symIsAConstantExpr uintptr
401 symIsAGetElementPtrInst uintptr
402 symIsACallInst uintptr
403 symIsABitCastInst uintptr
404 symGetGlobalParent uintptr
405 symGetCallSiteEnumAttr uintptr
406 symGetStringAttrAtIndex uintptr
407 symGetConstOpcode uintptr
408 symGetVolatile uintptr
409 symGetOrdering uintptr
410 symGetFirstUse uintptr
411 symGetPreviousInstruction uintptr
412 symGetPreviousParam uintptr
413 symGetNextUse uintptr
414 symGetUser uintptr
415 symGetUsedValue uintptr
416 symIsAArgument uintptr
417 symIsAGlobalVariable uintptr
418 symIsAInlineAsm uintptr
419 symIsAExtractValueInst uintptr
420 symIsALoadInst uintptr
421 symGetCalledFunctionType uintptr
422 symSetTailCall uintptr
423 symGetFirstParam uintptr
424
425 // Const misc
426 symConstPointerCast uintptr
427 )
428
429 func registerAll() {
430 registerTarget()
431 registerDIBuilder()
432 registerPassSymbols()
433
434 // Context
435 symContextCreate = mustSym(libLLVM, "LLVMContextCreate")
436 symContextDispose = mustSym(libLLVM, "LLVMContextDispose")
437 symGetGlobalCtx = mustSym(libLLVM, "LLVMGetGlobalContext")
438 symGetMDKindIDCtx = mustSym(libLLVM, "LLVMGetMDKindIDInContext")
439 symGetMDKindID = mustSym(libLLVM, "LLVMGetMDKindID")
440
441 // Attribute
442 symGetEnumAttrKindForName = mustSym(libLLVM, "LLVMGetEnumAttributeKindForName")
443 symCreateEnumAttr = mustSym(libLLVM, "LLVMCreateEnumAttribute")
444 symCreateTypeAttr = mustSym(libLLVM, "LLVMCreateTypeAttribute")
445 symCreateStringAttr = mustSym(libLLVM, "LLVMCreateStringAttribute")
446 symGetEnumAttrKind = mustSym(libLLVM, "LLVMGetEnumAttributeKind")
447 symGetEnumAttrValue = mustSym(libLLVM, "LLVMGetEnumAttributeValue")
448 symGetStringAttrKind = mustSym(libLLVM, "LLVMGetStringAttributeKind")
449 symGetStringAttrValue = mustSym(libLLVM, "LLVMGetStringAttributeValue")
450
451 // Module
452 symModuleCreateWithNameInCtx = mustSym(libLLVM, "LLVMModuleCreateWithNameInContext")
453 symDisposeModule = mustSym(libLLVM, "LLVMDisposeModule")
454 symGetDataLayout = mustSym(libLLVM, "LLVMGetDataLayout")
455 symSetDataLayout = mustSym(libLLVM, "LLVMSetDataLayout")
456 symGetTarget = mustSym(libLLVM, "LLVMGetTarget")
457 symSetTarget = mustSym(libLLVM, "LLVMSetTarget")
458 symGetModuleContext = mustSym(libLLVM, "LLVMGetModuleContext")
459 symGetTypeByName = mustSym(libLLVM, "LLVMGetTypeByName")
460 symPrintModuleToString = mustSym(libLLVM, "LLVMPrintModuleToString")
461 symDisposeMessage = mustSym(libLLVM, "LLVMDisposeMessage")
462 symSetModuleInlineAsm = mustSym(libLLVM, "LLVMSetModuleInlineAsm")
463 symDumpModule = mustSym(libLLVM, "LLVMDumpModule")
464
465 // Integer types
466 symInt1TypeInCtx = mustSym(libLLVM, "LLVMInt1TypeInContext")
467 symInt8TypeInCtx = mustSym(libLLVM, "LLVMInt8TypeInContext")
468 symInt16TypeInCtx = mustSym(libLLVM, "LLVMInt16TypeInContext")
469 symInt32TypeInCtx = mustSym(libLLVM, "LLVMInt32TypeInContext")
470 symInt64TypeInCtx = mustSym(libLLVM, "LLVMInt64TypeInContext")
471 symIntTypeInCtx = mustSym(libLLVM, "LLVMIntTypeInContext")
472 symGetIntTypeWidth = mustSym(libLLVM, "LLVMGetIntTypeWidth")
473
474 // Float types
475 symFloatTypeInCtx = mustSym(libLLVM, "LLVMFloatTypeInContext")
476 symDoubleTypeInCtx = mustSym(libLLVM, "LLVMDoubleTypeInContext")
477 symX86FP80TypeInCtx = mustSym(libLLVM, "LLVMX86FP80TypeInContext")
478 symFP128TypeInCtx = mustSym(libLLVM, "LLVMFP128TypeInContext")
479 symPPCFP128TypeInCtx = mustSym(libLLVM, "LLVMPPCFP128TypeInContext")
480
481 // Function types
482 symFunctionType = mustSym(libLLVM, "LLVMFunctionType")
483 symIsFunctionVarArg = mustSym(libLLVM, "LLVMIsFunctionVarArg")
484 symGetReturnType = mustSym(libLLVM, "LLVMGetReturnType")
485 symCountParamTypes = mustSym(libLLVM, "LLVMCountParamTypes")
486 symGetParamTypes = mustSym(libLLVM, "LLVMGetParamTypes")
487
488 // Struct types
489 symStructTypeInCtx = mustSym(libLLVM, "LLVMStructTypeInContext")
490 symStructCreateNamed = mustSym(libLLVM, "LLVMStructCreateNamed")
491 symGetStructName = mustSym(libLLVM, "LLVMGetStructName")
492 symStructSetBody = mustSym(libLLVM, "LLVMStructSetBody")
493 symIsPackedStruct = mustSym(libLLVM, "LLVMIsPackedStruct")
494 symCountStructElementTypes = mustSym(libLLVM, "LLVMCountStructElementTypes")
495 symGetStructElementTypes = mustSym(libLLVM, "LLVMGetStructElementTypes")
496
497 // Sequence types
498 symArrayType = mustSym(libLLVM, "LLVMArrayType")
499 symPointerType = mustSym(libLLVM, "LLVMPointerType")
500 symVectorType = mustSym(libLLVM, "LLVMVectorType")
501 symGetElementType = mustSym(libLLVM, "LLVMGetElementType")
502 symGetArrayLength = mustSym(libLLVM, "LLVMGetArrayLength")
503 symGetPointerAddressSpace = mustSym(libLLVM, "LLVMGetPointerAddressSpace")
504 symGetVectorSize = mustSym(libLLVM, "LLVMGetVectorSize")
505 symGetNumContainedTypes = mustSym(libLLVM, "LLVMGetNumContainedTypes")
506 symGetSubtypes = mustSym(libLLVM, "LLVMGetSubtypes")
507
508 // Other types
509 symVoidTypeInCtx = mustSym(libLLVM, "LLVMVoidTypeInContext")
510 symLabelTypeInCtx = mustSym(libLLVM, "LLVMLabelTypeInContext")
511 symTokenTypeInCtx = mustSym(libLLVM, "LLVMTokenTypeInContext")
512
513 // Type inspection
514 symGetTypeKind = mustSym(libLLVM, "LLVMGetTypeKind")
515 symGetTypeContext = mustSym(libLLVM, "LLVMGetTypeContext")
516
517 // Value operations
518 symTypeOf = mustSym(libLLVM, "LLVMTypeOf")
519 symGetValueName = mustSym(libLLVM, "LLVMGetValueName")
520 symSetValueName = mustSym(libLLVM, "LLVMSetValueName")
521 symDumpValue = mustSym(libLLVM, "LLVMDumpValue")
522 symReplaceAllUsesWith = mustSym(libLLVM, "LLVMReplaceAllUsesWith")
523 symHasMetadata = mustSym(libLLVM, "LLVMHasMetadata")
524 symGetMetadata = mustSym(libLLVM, "LLVMGetMetadata")
525 symPrintValueToString = mustSym(libLLVM, "LLVMPrintValueToString")
526
527 // Constant scalars
528 symConstInt = mustSym(libLLVM, "LLVMConstInt")
529 symConstFloat = mustSym(libLLVM, "LLVMConstReal") // broken
530 symConstFloatOfStr = mustSym(libLLVM, "LLVMConstRealOfStringAndSize")
531 symConstNull = mustSym(libLLVM, "LLVMConstNull")
532 symConstAllOnes = mustSym(libLLVM, "LLVMConstAllOnes")
533 symGetUndef = mustSym(libLLVM, "LLVMGetUndef")
534 symConstPointerNull = mustSym(libLLVM, "LLVMConstPointerNull")
535 symIsConstant = mustSym(libLLVM, "LLVMIsConstant")
536 symIsNull = mustSym(libLLVM, "LLVMIsNull")
537 symIsUndef = mustSym(libLLVM, "LLVMIsUndef")
538 symConstIntGetZExt = mustSym(libLLVM, "LLVMConstIntGetZExtValue")
539 symConstIntGetSExt = mustSym(libLLVM, "LLVMConstIntGetSExtValue")
540 symConstFloatGetDouble = mustSym(libLLVM, "LLVMConstRealGetDouble")
541
542 // Constant expressions
543 symConstNeg = mustSym(libLLVM, "LLVMConstNeg")
544 symConstNSWNeg = mustSym(libLLVM, "LLVMConstNSWNeg")
545 symConstNot = mustSym(libLLVM, "LLVMConstNot")
546 symConstAdd = mustSym(libLLVM, "LLVMConstAdd")
547 symConstNSWAdd = mustSym(libLLVM, "LLVMConstNSWAdd")
548 symConstSub = mustSym(libLLVM, "LLVMConstSub")
549 symConstNSWSub = mustSym(libLLVM, "LLVMConstNSWSub")
550 symConstMul = trySym(libLLVM, "LLVMConstMul")
551 symConstNSWMul = trySym(libLLVM, "LLVMConstNSWMul")
552 symConstXor = mustSym(libLLVM, "LLVMConstXor")
553 symConstGEP2 = mustSym(libLLVM, "LLVMConstGEP2")
554 symConstInBoundsGEP2 = mustSym(libLLVM, "LLVMConstInBoundsGEP2")
555 symConstTrunc = mustSym(libLLVM, "LLVMConstTrunc")
556 symConstSExt = trySym(libLLVM, "LLVMConstSExt")
557 symConstZExt = trySym(libLLVM, "LLVMConstZExt")
558 symConstFPTrunc = trySym(libLLVM, "LLVMConstFPTrunc")
559 symConstFPExt = trySym(libLLVM, "LLVMConstFPExt")
560 symConstUIToFP = trySym(libLLVM, "LLVMConstUIToFP")
561 symConstSIToFP = trySym(libLLVM, "LLVMConstSIToFP")
562 symConstFPToUI = trySym(libLLVM, "LLVMConstFPToUI")
563 symConstFPToSI = trySym(libLLVM, "LLVMConstFPToSI")
564 symConstPtrToInt = mustSym(libLLVM, "LLVMConstPtrToInt")
565 symConstIntToPtr = mustSym(libLLVM, "LLVMConstIntToPtr")
566 symConstBitCast = mustSym(libLLVM, "LLVMConstBitCast")
567 symConstICmp = trySym(libLLVM, "LLVMConstICmp")
568 symConstFCmp = trySym(libLLVM, "LLVMConstFCmp")
569 symConstExtractValue = trySym(libLLVM, "LLVMConstExtractValue")
570 symConstInsertValue = trySym(libLLVM, "LLVMConstInsertValue")
571
572 // Constant aggregates
573 symConstStringInCtx = mustSym(libLLVM, "LLVMConstStringInContext")
574 symConstStructInCtx = mustSym(libLLVM, "LLVMConstStructInContext")
575 symConstArray = mustSym(libLLVM, "LLVMConstArray")
576 symConstNamedStruct = mustSym(libLLVM, "LLVMConstNamedStruct")
577
578 // Global values
579 symGetLinkage = mustSym(libLLVM, "LLVMGetLinkage")
580 symSetLinkage = mustSym(libLLVM, "LLVMSetLinkage")
581 symGetSection = mustSym(libLLVM, "LLVMGetSection")
582 symSetSection = mustSym(libLLVM, "LLVMSetSection")
583 symGetVisibility = mustSym(libLLVM, "LLVMGetVisibility")
584 symSetVisibility = mustSym(libLLVM, "LLVMSetVisibility")
585 symGetAlignment = mustSym(libLLVM, "LLVMGetAlignment")
586 symSetAlignment = mustSym(libLLVM, "LLVMSetAlignment")
587
588 // Global variables
589 symAddGlobal = mustSym(libLLVM, "LLVMAddGlobal")
590 symGetNamedGlobal = mustSym(libLLVM, "LLVMGetNamedGlobal")
591 symGetFirstGlobal = mustSym(libLLVM, "LLVMGetFirstGlobal")
592 symGetNextGlobal = mustSym(libLLVM, "LLVMGetNextGlobal")
593 symGetInitializer = mustSym(libLLVM, "LLVMGetInitializer")
594 symSetInitializer = mustSym(libLLVM, "LLVMSetInitializer")
595 symIsGlobalConstant = mustSym(libLLVM, "LLVMIsGlobalConstant")
596 symSetGlobalConstant = mustSym(libLLVM, "LLVMSetGlobalConstant")
597 symSetThreadLocal = mustSym(libLLVM, "LLVMSetThreadLocal")
598 symIsThreadLocal = mustSym(libLLVM, "LLVMIsThreadLocal")
599 symSetExternallyInit = mustSym(libLLVM, "LLVMSetExternallyInitialized")
600 symIsExternallyInit = mustSym(libLLVM, "LLVMIsExternallyInitialized")
601 symDeleteGlobal = mustSym(libLLVM, "LLVMDeleteGlobal")
602 symSetUnnamedAddr = mustSym(libLLVM, "LLVMSetUnnamedAddress")
603 symGetUnnamedAddr = mustSym(libLLVM, "LLVMGetUnnamedAddress")
604
605 // Functions
606 symAddFunction = mustSym(libLLVM, "LLVMAddFunction")
607 symGetNamedFunction = mustSym(libLLVM, "LLVMGetNamedFunction")
608 symGetFirstFunction = mustSym(libLLVM, "LLVMGetFirstFunction")
609 symGetNextFunction = mustSym(libLLVM, "LLVMGetNextFunction")
610 symDeleteFunction = mustSym(libLLVM, "LLVMDeleteFunction")
611 symCountParams = mustSym(libLLVM, "LLVMCountParams")
612 symGetParams = mustSym(libLLVM, "LLVMGetParams")
613 symGetParam = mustSym(libLLVM, "LLVMGetParam")
614 symSetFunctionCallConv = mustSym(libLLVM, "LLVMSetFunctionCallConv")
615 symGetFunctionCallConv = mustSym(libLLVM, "LLVMGetFunctionCallConv")
616 symAddFuncAttr = mustSym(libLLVM, "LLVMAddAttributeAtIndex")
617 symGetEnumFuncAttr = mustSym(libLLVM, "LLVMGetEnumAttributeAtIndex")
618 symRemoveEnumFuncAttr = mustSym(libLLVM, "LLVMRemoveEnumAttributeAtIndex")
619 symAddTargetDepFuncAttr = mustSym(libLLVM, "LLVMAddTargetDependentFunctionAttr")
620
621 // Basic blocks
622 symAppendBasicBlockInCtx = mustSym(libLLVM, "LLVMAppendBasicBlockInContext")
623 symInsertBasicBlockInCtx = mustSym(libLLVM, "LLVMInsertBasicBlockInContext")
624 symGetBasicBlockParent = mustSym(libLLVM, "LLVMGetBasicBlockParent")
625 symGetEntryBasicBlock = mustSym(libLLVM, "LLVMGetEntryBasicBlock")
626 symGetFirstBasicBlock = mustSym(libLLVM, "LLVMGetFirstBasicBlock")
627 symGetLastBasicBlock = mustSym(libLLVM, "LLVMGetLastBasicBlock")
628 symGetNextBasicBlock = mustSym(libLLVM, "LLVMGetNextBasicBlock")
629 symGetPreviousBasicBlock = mustSym(libLLVM, "LLVMGetPreviousBasicBlock")
630 symCountBasicBlocks = mustSym(libLLVM, "LLVMCountBasicBlocks")
631 symMoveBasicBlockAfter = mustSym(libLLVM, "LLVMMoveBasicBlockAfter")
632 symMoveBasicBlockBefore = mustSym(libLLVM, "LLVMMoveBasicBlockBefore")
633 symDeleteBasicBlock = mustSym(libLLVM, "LLVMDeleteBasicBlock")
634 symBasicBlockAsValue = mustSym(libLLVM, "LLVMBasicBlockAsValue")
635
636 // Instructions
637 symGetFirstInstruction = mustSym(libLLVM, "LLVMGetFirstInstruction")
638 symGetLastInstruction = mustSym(libLLVM, "LLVMGetLastInstruction")
639 symGetNextInstruction = mustSym(libLLVM, "LLVMGetNextInstruction")
640 symGetInstructionOpcode = mustSym(libLLVM, "LLVMGetInstructionOpcode")
641 symInstructionEraseFromParent = mustSym(libLLVM, "LLVMInstructionEraseFromParent")
642 symGetInstructionParent = mustSym(libLLVM, "LLVMGetInstructionParent")
643 symIsATerminatorInst = mustSym(libLLVM, "LLVMIsATerminatorInst")
644
645 // Builder
646 symCreateBuilderInCtx = mustSym(libLLVM, "LLVMCreateBuilderInContext")
647 symPositionBuilderAtEnd = mustSym(libLLVM, "LLVMPositionBuilderAtEnd")
648 symPositionBuilderBefore = mustSym(libLLVM, "LLVMPositionBuilder")
649 symGetInsertBlock = mustSym(libLLVM, "LLVMGetInsertBlock")
650 symDisposeBuilder = mustSym(libLLVM, "LLVMDisposeBuilder")
651
652 // Builder - terminators
653 symBuildRetVoid = mustSym(libLLVM, "LLVMBuildRetVoid")
654 symBuildRet = mustSym(libLLVM, "LLVMBuildRet")
655 symBuildBr = mustSym(libLLVM, "LLVMBuildBr")
656 symBuildCondBr = mustSym(libLLVM, "LLVMBuildCondBr")
657 symBuildSwitch = mustSym(libLLVM, "LLVMBuildSwitch")
658 symBuildUnreachable = mustSym(libLLVM, "LLVMBuildUnreachable")
659
660 // Builder - arithmetic
661 symBuildAdd = mustSym(libLLVM, "LLVMBuildAdd")
662 symBuildNSWAdd = mustSym(libLLVM, "LLVMBuildNSWAdd")
663 symBuildNUWAdd = mustSym(libLLVM, "LLVMBuildNUWAdd")
664 symBuildFAdd = mustSym(libLLVM, "LLVMBuildFAdd")
665 symBuildSub = mustSym(libLLVM, "LLVMBuildSub")
666 symBuildNSWSub = mustSym(libLLVM, "LLVMBuildNSWSub")
667 symBuildNUWSub = mustSym(libLLVM, "LLVMBuildNUWSub")
668 symBuildFSub = mustSym(libLLVM, "LLVMBuildFSub")
669 symBuildMul = mustSym(libLLVM, "LLVMBuildMul")
670 symBuildNSWMul = mustSym(libLLVM, "LLVMBuildNSWMul")
671 symBuildNUWMul = mustSym(libLLVM, "LLVMBuildNUWMul")
672 symBuildFMul = mustSym(libLLVM, "LLVMBuildFMul")
673 symBuildUDiv = mustSym(libLLVM, "LLVMBuildUDiv")
674 symBuildSDiv = mustSym(libLLVM, "LLVMBuildSDiv")
675 symBuildExactSDiv = mustSym(libLLVM, "LLVMBuildExactSDiv")
676 symBuildFDiv = mustSym(libLLVM, "LLVMBuildFDiv")
677 symBuildURem = mustSym(libLLVM, "LLVMBuildURem")
678 symBuildSRem = mustSym(libLLVM, "LLVMBuildSRem")
679 symBuildFRem = mustSym(libLLVM, "LLVMBuildFRem")
680 symBuildShl = mustSym(libLLVM, "LLVMBuildShl")
681 symBuildLShr = mustSym(libLLVM, "LLVMBuildLShr")
682 symBuildAShr = mustSym(libLLVM, "LLVMBuildAShr")
683 symBuildAnd = mustSym(libLLVM, "LLVMBuildAnd")
684 symBuildOr = mustSym(libLLVM, "LLVMBuildOr")
685 symBuildXor = mustSym(libLLVM, "LLVMBuildXor")
686 symBuildNeg = mustSym(libLLVM, "LLVMBuildNeg")
687 symBuildNSWNeg = mustSym(libLLVM, "LLVMBuildNSWNeg")
688 symBuildNot = mustSym(libLLVM, "LLVMBuildNot")
689 symBuildFNeg = mustSym(libLLVM, "LLVMBuildFNeg")
690
691 // Builder - memory
692 symBuildAlloca = mustSym(libLLVM, "LLVMBuildAlloca")
693 symBuildLoad2 = mustSym(libLLVM, "LLVMBuildLoad2")
694 symBuildStore = mustSym(libLLVM, "LLVMBuildStore")
695 symBuildGEP2 = mustSym(libLLVM, "LLVMBuildGEP2")
696 symBuildInBoundsGEP2 = mustSym(libLLVM, "LLVMBuildInBoundsGEP2")
697 symBuildStructGEP2 = mustSym(libLLVM, "LLVMBuildStructGEP2")
698
699 // Builder - casts
700 symBuildTrunc = mustSym(libLLVM, "LLVMBuildTrunc")
701 symBuildZExt = mustSym(libLLVM, "LLVMBuildZExt")
702 symBuildSExt = mustSym(libLLVM, "LLVMBuildSExt")
703 symBuildFPToUI = mustSym(libLLVM, "LLVMBuildFPToUI")
704 symBuildFPToSI = mustSym(libLLVM, "LLVMBuildFPToSI")
705 symBuildUIToFP = mustSym(libLLVM, "LLVMBuildUIToFP")
706 symBuildSIToFP = mustSym(libLLVM, "LLVMBuildSIToFP")
707 symBuildFPTrunc = mustSym(libLLVM, "LLVMBuildFPTrunc")
708 symBuildFPExt = mustSym(libLLVM, "LLVMBuildFPExt")
709 symBuildPtrToInt = mustSym(libLLVM, "LLVMBuildPtrToInt")
710 symBuildIntToPtr = mustSym(libLLVM, "LLVMBuildIntToPtr")
711 symBuildBitCast = mustSym(libLLVM, "LLVMBuildBitCast")
712 symBuildPointerCast = mustSym(libLLVM, "LLVMBuildPointerCast")
713
714 // Builder - comparisons
715 symBuildICmp = mustSym(libLLVM, "LLVMBuildICmp")
716 symBuildFCmp = mustSym(libLLVM, "LLVMBuildFCmp")
717
718 // Builder - misc
719 symBuildPhi = mustSym(libLLVM, "LLVMBuildPhi")
720 symBuildCall2 = mustSym(libLLVM, "LLVMBuildCall2")
721 symBuildSelect = mustSym(libLLVM, "LLVMBuildSelect")
722 symBuildExtractValue = mustSym(libLLVM, "LLVMBuildExtractValue")
723 symBuildInsertValue = mustSym(libLLVM, "LLVMBuildInsertValue")
724
725 // PHI
726 symAddIncoming = mustSym(libLLVM, "LLVMAddIncoming")
727
728 // Call site
729 symSetInstructionCallConv = mustSym(libLLVM, "LLVMSetInstructionCallConv")
730 symGetInstructionCallConv = mustSym(libLLVM, "LLVMGetInstructionCallConv")
731 symAddCallSiteAttribute = mustSym(libLLVM, "LLVMAddCallSiteAttribute")
732 symSetInstrParamAlignment = mustSym(libLLVM, "LLVMSetInstrParamAlignment")
733
734 // Switch
735 symAddCase = mustSym(libLLVM, "LLVMAddCase")
736
737 // Landing pad
738 symBuildLandingPad = mustSym(libLLVM, "LLVMBuildLandingPad")
739 symSetCleanup = mustSym(libLLVM, "LLVMSetCleanup")
740 symAddClause = mustSym(libLLVM, "LLVMAddClause")
741
742 // Invoke
743 symBuildInvoke2 = mustSym(libLLVM, "LLVMBuildInvoke2")
744
745 // Resume
746 symBuildResume = mustSym(libLLVM, "LLVMBuildResume")
747
748 // Atomics
749 symBuildFence = mustSym(libLLVM, "LLVMBuildFence")
750 symBuildAtomicRMW = mustSym(libLLVM, "LLVMBuildAtomicRMW")
751 symBuildAtomicCmpXchg = mustSym(libLLVM, "LLVMBuildAtomicCmpXchg")
752 symSetOrdering = mustSym(libLLVM, "LLVMSetOrdering")
753 symSetAtomicSingleThread = mustSym(libLLVM, "LLVMSetAtomicSingleThread")
754
755 // Module verify
756 symVerifyModule = mustSym(libLLVM, "LLVMVerifyModule")
757 symVerifyFunction = mustSym(libLLVM, "LLVMVerifyFunction")
758
759 // Comdat
760 symGetOrInsertComdat = mustSym(libLLVM, "LLVMGetOrInsertComdat")
761 symComdatSetSelectionKind = mustSym(libLLVM, "LLVMSetComdatSelectionKind")
762 symSetComdat = mustSym(libLLVM, "LLVMSetComdat")
763
764 // Value type checks
765 symIsAAllocaInst = mustSym(libLLVM, "LLVMIsAAllocaInst")
766 symIsAConstant = mustSym(libLLVM, "LLVMIsAConstant")
767 symIsAConstantInt = mustSym(libLLVM, "LLVMIsAConstantInt")
768 symIsAFunction = mustSym(libLLVM, "LLVMIsAFunction")
769 symIsAGlobalValue = mustSym(libLLVM, "LLVMIsAGlobalValue")
770 symIsAInstruction = mustSym(libLLVM, "LLVMIsAInstruction")
771 symIsAPHINode = mustSym(libLLVM, "LLVMIsAPHINode")
772 symIsAUndefValue = mustSym(libLLVM, "LLVMIsAUndefValue")
773
774 // Value misc
775 symIsDeclaration = mustSym(libLLVM, "LLVMIsDeclaration")
776 symGlobalGetValType = mustSym(libLLVM, "LLVMGlobalGetValueType")
777 symGetLastParam = mustSym(libLLVM, "LLVMGetLastParam")
778 symGetOperand = mustSym(libLLVM, "LLVMGetOperand")
779 symGetNumOperands = mustSym(libLLVM, "LLVMGetNumOperands")
780 symSetVolatile = mustSym(libLLVM, "LLVMSetVolatile")
781 symInstructionRemoveFromParent = mustSym(libLLVM, "LLVMInstructionRemoveFromParent")
782 symInstructionGetDebugLoc = mustSym(libLLVM, "LLVMInstructionGetDebugLoc")
783 symCountIncoming = mustSym(libLLVM, "LLVMCountIncoming")
784 symGetIncomingValue = mustSym(libLLVM, "LLVMGetIncomingValue")
785 symGetIncomingBlock = mustSym(libLLVM, "LLVMGetIncomingBlock")
786
787 // Builder misc
788 symInsertIntoBuilder = mustSym(libLLVM, "LLVMInsertIntoBuilder")
789 symBuildBinOp = mustSym(libLLVM, "LLVMBuildBinOp")
790
791 // Additional Value methods
792 symGetCalledValue = mustSym(libLLVM, "LLVMGetCalledValue")
793 symGetAllocatedType = mustSym(libLLVM, "LLVMGetAllocatedType")
794 symGetGEPSourceElementType = mustSym(libLLVM, "LLVMGetGEPSourceElementType")
795 symGetNumIndices = mustSym(libLLVM, "LLVMGetNumIndices")
796 symGetIndices = mustSym(libLLVM, "LLVMGetIndices")
797 symGetICmpPredicate = mustSym(libLLVM, "LLVMGetICmpPredicate")
798 symGetFCmpPredicate = mustSym(libLLVM, "LLVMGetFCmpPredicate")
799 symIsAReturnInst = mustSym(libLLVM, "LLVMIsAReturnInst")
800 symIsAConstantExpr = mustSym(libLLVM, "LLVMIsAConstantExpr")
801 symIsAGetElementPtrInst = mustSym(libLLVM, "LLVMIsAGetElementPtrInst")
802 symIsACallInst = mustSym(libLLVM, "LLVMIsACallInst")
803 symIsABitCastInst = mustSym(libLLVM, "LLVMIsABitCastInst")
804 symGetGlobalParent = mustSym(libLLVM, "LLVMGetGlobalParent")
805 symGetCallSiteEnumAttr = mustSym(libLLVM, "LLVMGetCallSiteEnumAttribute")
806 symGetStringAttrAtIndex = mustSym(libLLVM, "LLVMGetStringAttributeAtIndex")
807 symGetConstOpcode = mustSym(libLLVM, "LLVMGetConstOpcode")
808 symGetVolatile = mustSym(libLLVM, "LLVMGetVolatile")
809 symGetOrdering = mustSym(libLLVM, "LLVMGetOrdering")
810 symGetFirstUse = mustSym(libLLVM, "LLVMGetFirstUse")
811 symGetPreviousInstruction = mustSym(libLLVM, "LLVMGetPreviousInstruction")
812 symGetPreviousParam = mustSym(libLLVM, "LLVMGetPreviousParam")
813 symGetNextUse = mustSym(libLLVM, "LLVMGetNextUse")
814 symGetUser = mustSym(libLLVM, "LLVMGetUser")
815 symGetUsedValue = mustSym(libLLVM, "LLVMGetUsedValue")
816 symIsAArgument = mustSym(libLLVM, "LLVMIsAArgument")
817 symIsAGlobalVariable = mustSym(libLLVM, "LLVMIsAGlobalVariable")
818 symIsAInlineAsm = mustSym(libLLVM, "LLVMIsAInlineAsm")
819 symIsAExtractValueInst = mustSym(libLLVM, "LLVMIsAExtractValueInst")
820 symIsALoadInst = mustSym(libLLVM, "LLVMIsALoadInst")
821 symGetCalledFunctionType = mustSym(libLLVM, "LLVMGetCalledFunctionType")
822 symSetTailCall = mustSym(libLLVM, "LLVMSetTailCall")
823 symGetFirstParam = mustSym(libLLVM, "LLVMGetFirstParam")
824
825 // Const misc
826 symConstPointerCast = trySym(libLLVM, "LLVMConstPointerCast")
827
828 // Glue functions (from libmoxie-llvm-glue.so)
829 if libGlue != 0 {
830 symMDString2 = mustSym(libGlue, "LLVMMDString2")
831 symMDNode2 = mustSym(libGlue, "LLVMMDNode2")
832 symConstantAsMetadata = mustSym(libGlue, "LLVMConstantAsMetadata")
833 symSetMetadata2 = mustSym(libGlue, "LLVMSetMetadata2")
834 symAddNamedMetadataOperand2 = mustSym(libGlue, "LLVMAddNamedMetadataOperand2")
835 symGlobalObjectAddMetadata = mustSym(libGlue, "LLVMGlobalObjectAddMetadata")
836 symGoGetInlineAsm = mustSym(libGlue, "LLVMGoGetInlineAsm")
837 symGoSetCurrentDebugLocation = mustSym(libGlue, "LLVMGoSetCurrentDebugLocation")
838 }
839 symGetCurrentDebugLocation2 = mustSym(libLLVM, "LLVMGetCurrentDebugLocation2")
840 symDILocationGetLine = mustSym(libLLVM, "LLVMDILocationGetLine")
841 symDILocationGetColumn = mustSym(libLLVM, "LLVMDILocationGetColumn")
842 symDILocationGetScope = mustSym(libLLVM, "LLVMDILocationGetScope")
843 symDILocationGetInlinedAt = mustSym(libLLVM, "LLVMDILocationGetInlinedAt")
844 }
845
846 // ---- Context ----
847
848 func NewContext() Context {
849 r, _, _ := purego.SyscallN(symContextCreate)
850 return Context{r}
851 }
852
853 func GlobalContext() Context {
854 r, _, _ := purego.SyscallN(symGetGlobalCtx)
855 return Context{r}
856 }
857
858 func (c Context) Dispose() {
859 purego.SyscallN(symContextDispose, c.C)
860 }
861
862 func (c Context) MDKindID(name string) int {
863 cname := cString(name)
864 r, _, _ := purego.SyscallN(symGetMDKindIDCtx, c.C, cname, uintptr(len(name)))
865 runtime.KeepAlive(name)
866 return int(r)
867 }
868
869 func MDKindID(name string) int {
870 cname := cString(name)
871 r, _, _ := purego.SyscallN(symGetMDKindID, cname, uintptr(len(name)))
872 runtime.KeepAlive(name)
873 return int(r)
874 }
875
876 // ---- Attributes ----
877
878 func AttributeKindID(name string) uint {
879 cname := cString(name)
880 r, _, _ := purego.SyscallN(symGetEnumAttrKindForName, cname, uintptr(len(name)))
881 runtime.KeepAlive(name)
882 return uint(r)
883 }
884
885 func (c Context) CreateEnumAttribute(kind uint, val uint64) Attribute {
886 r, _, _ := purego.SyscallN(symCreateEnumAttr, c.C, uintptr(kind), uintptr(val))
887 return Attribute{r}
888 }
889
890 func (c Context) CreateTypeAttribute(kind uint, t Type) Attribute {
891 r, _, _ := purego.SyscallN(symCreateTypeAttr, c.C, uintptr(kind), t.C)
892 return Attribute{r}
893 }
894
895 func (c Context) CreateStringAttribute(kind string, val string) Attribute {
896 ckind := cString(kind)
897 cval := cString(val)
898 r, _, _ := purego.SyscallN(symCreateStringAttr, c.C, ckind, uintptr(len(kind)), cval, uintptr(len(val)))
899 runtime.KeepAlive(kind)
900 runtime.KeepAlive(val)
901 return Attribute{r}
902 }
903
904 func (a Attribute) GetEnumKind() uint {
905 r, _, _ := purego.SyscallN(symGetEnumAttrKind, a.C)
906 return uint(r)
907 }
908
909 func (a Attribute) GetEnumValue() uint64 {
910 r, _, _ := purego.SyscallN(symGetEnumAttrValue, a.C)
911 return uint64(r)
912 }
913
914 func (a Attribute) GetStringKind() string {
915 var length uintptr
916 r, _, _ := purego.SyscallN(symGetStringAttrKind, a.C, uintptr(unsafe.Pointer(&length)))
917 return goStringN(r, int(length))
918 }
919
920 func (a Attribute) GetStringValue() string {
921 var length uintptr
922 r, _, _ := purego.SyscallN(symGetStringAttrValue, a.C, uintptr(unsafe.Pointer(&length)))
923 return goStringN(r, int(length))
924 }
925
926 func goStringN(ptr uintptr, n int) string {
927 if ptr == 0 || n == 0 {
928 return ""
929 }
930 return string((*[1 << 30]byte)(unsafe.Pointer(ptr))[:n:n])
931 }
932
933 // ---- Module ----
934
935 func (c Context) NewModule(name string) Module {
936 cname := cString(name)
937 r, _, _ := purego.SyscallN(symModuleCreateWithNameInCtx, cname, c.C)
938 runtime.KeepAlive(name)
939 return Module{r}
940 }
941
942 func (m Module) Dispose() {
943 purego.SyscallN(symDisposeModule, m.C)
944 }
945
946 func (m Module) DataLayout() string {
947 r, _, _ := purego.SyscallN(symGetDataLayout, m.C)
948 return goString(r)
949 }
950
951 func (m Module) SetDataLayout(layout string) {
952 cl := cString(layout)
953 purego.SyscallN(symSetDataLayout, m.C, cl)
954 runtime.KeepAlive(layout)
955 }
956
957 func (m Module) Target() string {
958 r, _, _ := purego.SyscallN(symGetTarget, m.C)
959 return goString(r)
960 }
961
962 func (m Module) SetTarget(target string) {
963 ct := cString(target)
964 purego.SyscallN(symSetTarget, m.C, ct)
965 runtime.KeepAlive(target)
966 }
967
968 func (m Module) Context() Context {
969 r, _, _ := purego.SyscallN(symGetModuleContext, m.C)
970 return Context{r}
971 }
972
973 func (m Module) GetTypeByName(name string) Type {
974 cname := cString(name)
975 r, _, _ := purego.SyscallN(symGetTypeByName, m.C, cname)
976 runtime.KeepAlive(name)
977 return Type{r}
978 }
979
980 func (m Module) String() string {
981 r, _, _ := purego.SyscallN(symPrintModuleToString, m.C)
982 s := goString(r)
983 purego.SyscallN(symDisposeMessage, r)
984 return s
985 }
986
987 func (m Module) Dump() {
988 purego.SyscallN(symDumpModule, m.C)
989 }
990
991 func (m Module) SetInlineAsm(asm string) {
992 casm := cString(asm)
993 purego.SyscallN(symSetModuleInlineAsm, m.C, casm)
994 runtime.KeepAlive(asm)
995 }
996
997 func (m Module) AddNamedMetadataOperand(name string, operand Metadata) {
998 if symAddNamedMetadataOperand2 == 0 {
999 return
1000 }
1001 cname := cString(name)
1002 purego.SyscallN(symAddNamedMetadataOperand2, m.C, cname, operand.C)
1003 runtime.KeepAlive(name)
1004 }
1005
1006 // ---- Type inspection ----
1007
1008 func (t Type) TypeKind() TypeKind {
1009 r, _, _ := purego.SyscallN(symGetTypeKind, t.C)
1010 return TypeKind(r)
1011 }
1012
1013 func (t Type) Context() Context {
1014 r, _, _ := purego.SyscallN(symGetTypeContext, t.C)
1015 return Context{r}
1016 }
1017
1018 // ---- Integer types ----
1019
1020 func (c Context) Int1Type() Type {
1021 r, _, _ := purego.SyscallN(symInt1TypeInCtx, c.C)
1022 return Type{r}
1023 }
1024
1025 func (c Context) Int8Type() Type {
1026 r, _, _ := purego.SyscallN(symInt8TypeInCtx, c.C)
1027 return Type{r}
1028 }
1029
1030 func (c Context) Int16Type() Type {
1031 r, _, _ := purego.SyscallN(symInt16TypeInCtx, c.C)
1032 return Type{r}
1033 }
1034
1035 func (c Context) Int32Type() Type {
1036 r, _, _ := purego.SyscallN(symInt32TypeInCtx, c.C)
1037 return Type{r}
1038 }
1039
1040 func (c Context) Int64Type() Type {
1041 r, _, _ := purego.SyscallN(symInt64TypeInCtx, c.C)
1042 return Type{r}
1043 }
1044
1045 func (c Context) IntType(numbits int) Type {
1046 r, _, _ := purego.SyscallN(symIntTypeInCtx, c.C, uintptr(numbits))
1047 return Type{r}
1048 }
1049
1050 func (t Type) IntTypeWidth() int {
1051 r, _, _ := purego.SyscallN(symGetIntTypeWidth, t.C)
1052 return int(r)
1053 }
1054
1055 // ---- Float types ----
1056
1057 func (c Context) FloatType() Type {
1058 r, _, _ := purego.SyscallN(symFloatTypeInCtx, c.C)
1059 return Type{r}
1060 }
1061
1062 func (c Context) DoubleType() Type {
1063 r, _, _ := purego.SyscallN(symDoubleTypeInCtx, c.C)
1064 return Type{r}
1065 }
1066
1067 func (c Context) X86FP80Type() Type {
1068 r, _, _ := purego.SyscallN(symX86FP80TypeInCtx, c.C)
1069 return Type{r}
1070 }
1071
1072 func (c Context) FP128Type() Type {
1073 r, _, _ := purego.SyscallN(symFP128TypeInCtx, c.C)
1074 return Type{r}
1075 }
1076
1077 func (c Context) PPCFP128Type() Type {
1078 r, _, _ := purego.SyscallN(symPPCFP128TypeInCtx, c.C)
1079 return Type{r}
1080 }
1081
1082 // ---- Function types ----
1083
1084 func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) Type {
1085 r, _, _ := purego.SyscallN(symFunctionType, returnType.C, typeRefPtr(paramTypes), uintptr(len(paramTypes)), boolToInt(isVarArg))
1086 return Type{r}
1087 }
1088
1089 func (t Type) IsFunctionVarArg() bool {
1090 r, _, _ := purego.SyscallN(symIsFunctionVarArg, t.C)
1091 return r != 0
1092 }
1093
1094 func (t Type) ReturnType() Type {
1095 r, _, _ := purego.SyscallN(symGetReturnType, t.C)
1096 return Type{r}
1097 }
1098
1099 func (t Type) ParamTypesCount() int {
1100 r, _, _ := purego.SyscallN(symCountParamTypes, t.C)
1101 return int(r)
1102 }
1103
1104 func (t Type) ParamTypes() []Type {
1105 count := t.ParamTypesCount()
1106 if count == 0 {
1107 return nil
1108 }
1109 out := make([]Type, count)
1110 purego.SyscallN(symGetParamTypes, t.C, uintptr(unsafe.Pointer(&out[0])))
1111 return out
1112 }
1113
1114 // ---- Struct types ----
1115
1116 func (c Context) StructType(elementTypes []Type, packed bool) Type {
1117 r, _, _ := purego.SyscallN(symStructTypeInCtx, c.C, typeRefPtr(elementTypes), uintptr(len(elementTypes)), boolToInt(packed))
1118 return Type{r}
1119 }
1120
1121 func (c Context) StructCreateNamed(name string) Type {
1122 cname := cString(name)
1123 r, _, _ := purego.SyscallN(symStructCreateNamed, c.C, cname)
1124 runtime.KeepAlive(name)
1125 return Type{r}
1126 }
1127
1128 func (t Type) StructName() string {
1129 r, _, _ := purego.SyscallN(symGetStructName, t.C)
1130 return goString(r)
1131 }
1132
1133 func (t Type) StructSetBody(elementTypes []Type, packed bool) {
1134 purego.SyscallN(symStructSetBody, t.C, typeRefPtr(elementTypes), uintptr(len(elementTypes)), boolToInt(packed))
1135 }
1136
1137 func (t Type) IsStructPacked() bool {
1138 r, _, _ := purego.SyscallN(symIsPackedStruct, t.C)
1139 return r != 0
1140 }
1141
1142 func (t Type) StructElementTypesCount() int {
1143 r, _, _ := purego.SyscallN(symCountStructElementTypes, t.C)
1144 return int(r)
1145 }
1146
1147 func (t Type) StructElementTypes() []Type {
1148 count := t.StructElementTypesCount()
1149 if count == 0 {
1150 return nil
1151 }
1152 out := make([]Type, count)
1153 purego.SyscallN(symGetStructElementTypes, t.C, uintptr(unsafe.Pointer(&out[0])))
1154 return out
1155 }
1156
1157 // ---- Sequence types ----
1158
1159 func ArrayType(elementType Type, elementCount int) Type {
1160 r, _, _ := purego.SyscallN(symArrayType, elementType.C, uintptr(elementCount))
1161 return Type{r}
1162 }
1163
1164 func PointerType(elementType Type, addressSpace int) Type {
1165 r, _, _ := purego.SyscallN(symPointerType, elementType.C, uintptr(addressSpace))
1166 return Type{r}
1167 }
1168
1169 func VectorType(elementType Type, elementCount int) Type {
1170 r, _, _ := purego.SyscallN(symVectorType, elementType.C, uintptr(elementCount))
1171 return Type{r}
1172 }
1173
1174 func (t Type) ElementType() Type {
1175 r, _, _ := purego.SyscallN(symGetElementType, t.C)
1176 return Type{r}
1177 }
1178
1179 func (t Type) ArrayLength() int {
1180 r, _, _ := purego.SyscallN(symGetArrayLength, t.C)
1181 return int(r)
1182 }
1183
1184 func (t Type) PointerAddressSpace() int {
1185 r, _, _ := purego.SyscallN(symGetPointerAddressSpace, t.C)
1186 return int(r)
1187 }
1188
1189 func (t Type) VectorSize() int {
1190 r, _, _ := purego.SyscallN(symGetVectorSize, t.C)
1191 return int(r)
1192 }
1193
1194 func (t Type) Subtypes() []Type {
1195 n, _, _ := purego.SyscallN(symGetNumContainedTypes, t.C)
1196 if n == 0 {
1197 return nil
1198 }
1199 out := make([]Type, int(n))
1200 purego.SyscallN(symGetSubtypes, t.C, uintptr(unsafe.Pointer(&out[0])))
1201 return out
1202 }
1203
1204 // ---- Other types ----
1205
1206 func (c Context) VoidType() Type {
1207 r, _, _ := purego.SyscallN(symVoidTypeInCtx, c.C)
1208 return Type{r}
1209 }
1210
1211 func (c Context) LabelType() Type {
1212 r, _, _ := purego.SyscallN(symLabelTypeInCtx, c.C)
1213 return Type{r}
1214 }
1215
1216 func (c Context) TokenType() Type {
1217 r, _, _ := purego.SyscallN(symTokenTypeInCtx, c.C)
1218 return Type{r}
1219 }
1220
1221 // ---- Value operations ----
1222
1223 func (v Value) Type() Type {
1224 r, _, _ := purego.SyscallN(symTypeOf, v.C)
1225 return Type{r}
1226 }
1227
1228 func (v Value) Name() string {
1229 r, _, _ := purego.SyscallN(symGetValueName, v.C)
1230 return goString(r)
1231 }
1232
1233 func (v Value) SetName(name string) {
1234 cname := cString(name)
1235 purego.SyscallN(symSetValueName, v.C, cname)
1236 runtime.KeepAlive(name)
1237 }
1238
1239 func (v Value) Dump() {
1240 purego.SyscallN(symDumpValue, v.C)
1241 }
1242
1243 func (v Value) ReplaceAllUsesWith(nv Value) {
1244 purego.SyscallN(symReplaceAllUsesWith, v.C, nv.C)
1245 }
1246
1247 func (v Value) HasMetadata() bool {
1248 r, _, _ := purego.SyscallN(symHasMetadata, v.C)
1249 return r != 0
1250 }
1251
1252 func (v Value) InstructionDebugLoc() Metadata {
1253 r, _, _ := purego.SyscallN(symInstructionGetDebugLoc, v.C)
1254 return Metadata{r}
1255 }
1256
1257 func (v Value) SetMetadata(kind int, node Metadata) {
1258 if symSetMetadata2 == 0 {
1259 return
1260 }
1261 purego.SyscallN(symSetMetadata2, v.C, uintptr(kind), node.C)
1262 }
1263
1264 func (v Value) String() string {
1265 r, _, _ := purego.SyscallN(symPrintValueToString, v.C)
1266 s := goString(r)
1267 purego.SyscallN(symDisposeMessage, r)
1268 return s
1269 }
1270
1271 // ---- Constant scalars ----
1272
1273 func ConstInt(t Type, n uint64, signExtend bool) Value {
1274 r, _, _ := purego.SyscallN(symConstInt, t.C, uintptr(n), boolToInt(signExtend))
1275 return Value{r}
1276 }
1277
1278 func ConstFloat(t Type, n float64) Value {
1279 // LLVMConstReal expects double in XMM0 (System V ABI), but
1280 // purego.SyscallN maps a2->f2 (XMM1), not a2->f1 (XMM0).
1281 // Use LLVMConstRealOfStringAndSize (all-integer args) with a
1282 // full-precision decimal string that LLVM parses correctly.
1283 s := fmt.Sprintf("%.17e", n)
1284 var buf [32]byte
1285 copy(buf[:], s)
1286 buf[len(s)] = 0
1287 r, _, _ := purego.SyscallN(symConstFloatOfStr, t.C,
1288 uintptr(unsafe.Pointer(&buf[0])), uintptr(len(s)))
1289 return Value{r}
1290 }
1291
1292 func ConstNull(t Type) Value {
1293 r, _, _ := purego.SyscallN(symConstNull, t.C)
1294 return Value{r}
1295 }
1296
1297 func ConstAllOnes(t Type) Value {
1298 r, _, _ := purego.SyscallN(symConstAllOnes, t.C)
1299 return Value{r}
1300 }
1301
1302 func Undef(t Type) Value {
1303 r, _, _ := purego.SyscallN(symGetUndef, t.C)
1304 return Value{r}
1305 }
1306
1307 func ConstPointerNull(t Type) Value {
1308 r, _, _ := purego.SyscallN(symConstPointerNull, t.C)
1309 return Value{r}
1310 }
1311
1312 func (v Value) IsConstant() bool {
1313 r, _, _ := purego.SyscallN(symIsConstant, v.C)
1314 return r != 0
1315 }
1316
1317 func (v Value) IsNull() bool {
1318 r, _, _ := purego.SyscallN(symIsNull, v.C)
1319 return r != 0
1320 }
1321
1322 func (v Value) IsUndef() bool {
1323 r, _, _ := purego.SyscallN(symIsUndef, v.C)
1324 return r != 0
1325 }
1326
1327 func (v Value) ZExtValue() uint64 {
1328 r, _, _ := purego.SyscallN(symConstIntGetZExt, v.C)
1329 return uint64(r)
1330 }
1331
1332 func (v Value) SExtValue() int64 {
1333 r, _, _ := purego.SyscallN(symConstIntGetSExt, v.C)
1334 return int64(r)
1335 }
1336
1337 func (v Value) DoubleValue() (float64, bool) {
1338 var lossy uintptr
1339 r, _, _ := purego.SyscallN(symConstFloatGetDouble, v.C, uintptr(unsafe.Pointer(&lossy)))
1340 return *(*float64)(unsafe.Pointer(&r)), lossy != 0
1341 }
1342
1343 // ---- Constant expressions ----
1344
1345 func (v Value) ConstNeg() Value {
1346 r, _, _ := purego.SyscallN(symConstNeg, v.C)
1347 return Value{r}
1348 }
1349
1350 func (v Value) ConstNSWNeg() Value {
1351 r, _, _ := purego.SyscallN(symConstNSWNeg, v.C)
1352 return Value{r}
1353 }
1354
1355 func ConstNot(v Value) Value {
1356 r, _, _ := purego.SyscallN(symConstNot, v.C)
1357 return Value{r}
1358 }
1359
1360 func ConstAdd(lhs, rhs Value) Value {
1361 r, _, _ := purego.SyscallN(symConstAdd, lhs.C, rhs.C)
1362 return Value{r}
1363 }
1364
1365 func ConstNSWAdd(lhs, rhs Value) Value {
1366 r, _, _ := purego.SyscallN(symConstNSWAdd, lhs.C, rhs.C)
1367 return Value{r}
1368 }
1369
1370 func ConstSub(lhs, rhs Value) Value {
1371 r, _, _ := purego.SyscallN(symConstSub, lhs.C, rhs.C)
1372 return Value{r}
1373 }
1374
1375 func ConstNSWSub(lhs, rhs Value) Value {
1376 r, _, _ := purego.SyscallN(symConstNSWSub, lhs.C, rhs.C)
1377 return Value{r}
1378 }
1379
1380 func ConstMul(lhs, rhs Value) Value {
1381 r, _, _ := purego.SyscallN(symConstMul, lhs.C, rhs.C)
1382 return Value{r}
1383 }
1384
1385 func ConstNSWMul(lhs, rhs Value) Value {
1386 r, _, _ := purego.SyscallN(symConstNSWMul, lhs.C, rhs.C)
1387 return Value{r}
1388 }
1389
1390 func ConstXor(lhs, rhs Value) Value {
1391 r, _, _ := purego.SyscallN(symConstXor, lhs.C, rhs.C)
1392 return Value{r}
1393 }
1394
1395 func ConstGEP(pointeeType Type, val Value, indices []Value) Value {
1396 r, _, _ := purego.SyscallN(symConstGEP2, pointeeType.C, val.C, valueRefPtr(indices), uintptr(len(indices)))
1397 return Value{r}
1398 }
1399
1400 func ConstInBoundsGEP(pointeeType Type, val Value, indices []Value) Value {
1401 r, _, _ := purego.SyscallN(symConstInBoundsGEP2, pointeeType.C, val.C, valueRefPtr(indices), uintptr(len(indices)))
1402 return Value{r}
1403 }
1404
1405 func ConstTrunc(val Value, t Type) Value {
1406 r, _, _ := purego.SyscallN(symConstTrunc, val.C, t.C)
1407 return Value{r}
1408 }
1409
1410 func ConstSExt(val Value, t Type) Value {
1411 r, _, _ := purego.SyscallN(symConstSExt, val.C, t.C)
1412 return Value{r}
1413 }
1414
1415 func ConstZExt(val Value, t Type) Value {
1416 r, _, _ := purego.SyscallN(symConstZExt, val.C, t.C)
1417 return Value{r}
1418 }
1419
1420 func ConstFPTrunc(val Value, t Type) Value {
1421 r, _, _ := purego.SyscallN(symConstFPTrunc, val.C, t.C)
1422 return Value{r}
1423 }
1424
1425 func ConstFPExt(val Value, t Type) Value {
1426 r, _, _ := purego.SyscallN(symConstFPExt, val.C, t.C)
1427 return Value{r}
1428 }
1429
1430 func ConstUIToFP(val Value, t Type) Value {
1431 r, _, _ := purego.SyscallN(symConstUIToFP, val.C, t.C)
1432 return Value{r}
1433 }
1434
1435 func ConstSIToFP(val Value, t Type) Value {
1436 r, _, _ := purego.SyscallN(symConstSIToFP, val.C, t.C)
1437 return Value{r}
1438 }
1439
1440 func ConstFPToUI(val Value, t Type) Value {
1441 r, _, _ := purego.SyscallN(symConstFPToUI, val.C, t.C)
1442 return Value{r}
1443 }
1444
1445 func ConstFPToSI(val Value, t Type) Value {
1446 r, _, _ := purego.SyscallN(symConstFPToSI, val.C, t.C)
1447 return Value{r}
1448 }
1449
1450 func ConstPtrToInt(val Value, t Type) Value {
1451 r, _, _ := purego.SyscallN(symConstPtrToInt, val.C, t.C)
1452 return Value{r}
1453 }
1454
1455 func ConstIntToPtr(val Value, t Type) Value {
1456 r, _, _ := purego.SyscallN(symConstIntToPtr, val.C, t.C)
1457 return Value{r}
1458 }
1459
1460 func ConstBitCast(val Value, t Type) Value {
1461 r, _, _ := purego.SyscallN(symConstBitCast, val.C, t.C)
1462 return Value{r}
1463 }
1464
1465 func ConstPointerCast(val Value, t Type) Value {
1466 if symConstPointerCast == 0 {
1467 return ConstBitCast(val, t)
1468 }
1469 r, _, _ := purego.SyscallN(symConstPointerCast, val.C, t.C)
1470 return Value{r}
1471 }
1472
1473 func ConstICmp(pred IntPredicate, lhs, rhs Value) Value {
1474 r, _, _ := purego.SyscallN(symConstICmp, uintptr(pred), lhs.C, rhs.C)
1475 return Value{r}
1476 }
1477
1478 func ConstFCmp(pred FloatPredicate, lhs, rhs Value) Value {
1479 r, _, _ := purego.SyscallN(symConstFCmp, uintptr(pred), lhs.C, rhs.C)
1480 return Value{r}
1481 }
1482
1483 func ConstExtractValue(agg Value, indices []uint32) Value {
1484 r, _, _ := purego.SyscallN(symConstExtractValue, agg.C, uintptr(unsafe.Pointer(&indices[0])), uintptr(len(indices)))
1485 return Value{r}
1486 }
1487
1488 func ConstInsertValue(agg, val Value, indices []uint32) Value {
1489 r, _, _ := purego.SyscallN(symConstInsertValue, agg.C, val.C, uintptr(unsafe.Pointer(&indices[0])), uintptr(len(indices)))
1490 return Value{r}
1491 }
1492
1493 // ---- Constant aggregates ----
1494
1495 func (c Context) ConstString(str string, addnull bool) Value {
1496 cstr := cString(str)
1497 r, _, _ := purego.SyscallN(symConstStringInCtx, c.C, cstr, uintptr(len(str)), boolToInt(!addnull))
1498 runtime.KeepAlive(str)
1499 return Value{r}
1500 }
1501
1502 func (c Context) ConstStruct(constVals []Value, packed bool) Value {
1503 r, _, _ := purego.SyscallN(symConstStructInCtx, c.C, valueRefPtr(constVals), uintptr(len(constVals)), boolToInt(packed))
1504 return Value{r}
1505 }
1506
1507 func ConstArray(t Type, constVals []Value) Value {
1508 r, _, _ := purego.SyscallN(symConstArray, t.C, valueRefPtr(constVals), uintptr(len(constVals)))
1509 return Value{r}
1510 }
1511
1512 func ConstNamedStruct(t Type, constVals []Value) Value {
1513 r, _, _ := purego.SyscallN(symConstNamedStruct, t.C, valueRefPtr(constVals), uintptr(len(constVals)))
1514 return Value{r}
1515 }
1516
1517 // ---- Global values ----
1518
1519 func (v Value) Linkage() Linkage {
1520 r, _, _ := purego.SyscallN(symGetLinkage, v.C)
1521 return Linkage(r)
1522 }
1523
1524 func (v Value) SetLinkage(l Linkage) {
1525 purego.SyscallN(symSetLinkage, v.C, uintptr(l))
1526 }
1527
1528 func (v Value) Section() string {
1529 r, _, _ := purego.SyscallN(symGetSection, v.C)
1530 return goString(r)
1531 }
1532
1533 func (v Value) SetSection(s string) {
1534 cs := cString(s)
1535 purego.SyscallN(symSetSection, v.C, cs)
1536 runtime.KeepAlive(s)
1537 }
1538
1539 func (v Value) Visibility() Visibility {
1540 r, _, _ := purego.SyscallN(symGetVisibility, v.C)
1541 return Visibility(r)
1542 }
1543
1544 func (v Value) SetVisibility(vis Visibility) {
1545 purego.SyscallN(symSetVisibility, v.C, uintptr(vis))
1546 }
1547
1548 func (v Value) Alignment() int {
1549 r, _, _ := purego.SyscallN(symGetAlignment, v.C)
1550 return int(r)
1551 }
1552
1553 func (v Value) SetAlignment(a int) {
1554 purego.SyscallN(symSetAlignment, v.C, uintptr(a))
1555 }
1556
1557 // ---- Global variables ----
1558
1559 func (m Module) AddGlobal(t Type, name string) Value {
1560 cname := cString(name)
1561 r, _, _ := purego.SyscallN(symAddGlobal, m.C, t.C, cname)
1562 runtime.KeepAlive(name)
1563 return Value{r}
1564 }
1565
1566 func (m Module) NamedGlobal(name string) Value {
1567 cname := cString(name)
1568 r, _, _ := purego.SyscallN(symGetNamedGlobal, m.C, cname)
1569 runtime.KeepAlive(name)
1570 return Value{r}
1571 }
1572
1573 func (m Module) FirstGlobal() Value {
1574 r, _, _ := purego.SyscallN(symGetFirstGlobal, m.C)
1575 return Value{r}
1576 }
1577
1578 func (v Value) NextGlobal() Value {
1579 r, _, _ := purego.SyscallN(symGetNextGlobal, v.C)
1580 return Value{r}
1581 }
1582
1583 func (v Value) Initializer() Value {
1584 r, _, _ := purego.SyscallN(symGetInitializer, v.C)
1585 return Value{r}
1586 }
1587
1588 func (v Value) SetInitializer(c Value) {
1589 purego.SyscallN(symSetInitializer, v.C, c.C)
1590 }
1591
1592 func (v Value) IsGlobalConstant() bool {
1593 r, _, _ := purego.SyscallN(symIsGlobalConstant, v.C)
1594 return r != 0
1595 }
1596
1597 func (v Value) SetGlobalConstant(b bool) {
1598 purego.SyscallN(symSetGlobalConstant, v.C, boolToInt(b))
1599 }
1600
1601 func (v Value) SetThreadLocal(b bool) {
1602 purego.SyscallN(symSetThreadLocal, v.C, boolToInt(b))
1603 }
1604
1605 func (v Value) IsThreadLocal() bool {
1606 r, _, _ := purego.SyscallN(symIsThreadLocal, v.C)
1607 return r != 0
1608 }
1609
1610 func (v Value) SetExternallyInitialized(b bool) {
1611 purego.SyscallN(symSetExternallyInit, v.C, boolToInt(b))
1612 }
1613
1614 func (v Value) IsExternallyInitialized() bool {
1615 r, _, _ := purego.SyscallN(symIsExternallyInit, v.C)
1616 return r != 0
1617 }
1618
1619 func (v Value) EraseFromParentAsGlobal() {
1620 purego.SyscallN(symDeleteGlobal, v.C)
1621 }
1622
1623 func (v Value) SetUnnamedAddr(b bool) {
1624 val := uintptr(0)
1625 if b {
1626 val = 1 // LLVMGlobalUnnamedAddr
1627 }
1628 purego.SyscallN(symSetUnnamedAddr, v.C, val)
1629 }
1630
1631 func (v Value) UnnamedAddr() bool {
1632 r, _, _ := purego.SyscallN(symGetUnnamedAddr, v.C)
1633 return r != 0
1634 }
1635
1636 // ---- Functions ----
1637
1638 func (m Module) AddFunction(name string, t Type) Value {
1639 cname := cString(name)
1640 r, _, _ := purego.SyscallN(symAddFunction, m.C, cname, t.C)
1641 runtime.KeepAlive(name)
1642 return Value{r}
1643 }
1644
1645 func AddFunction(m Module, name string, ft Type) Value {
1646 return m.AddFunction(name, ft)
1647 }
1648
1649 func AddGlobal(m Module, t Type, name string) Value {
1650 return m.AddGlobal(t, name)
1651 }
1652
1653 func (m Module) NamedFunction(name string) Value {
1654 cname := cString(name)
1655 r, _, _ := purego.SyscallN(symGetNamedFunction, m.C, cname)
1656 runtime.KeepAlive(name)
1657 return Value{r}
1658 }
1659
1660 func (m Module) FirstFunction() Value {
1661 r, _, _ := purego.SyscallN(symGetFirstFunction, m.C)
1662 return Value{r}
1663 }
1664
1665 func (v Value) NextFunction() Value {
1666 r, _, _ := purego.SyscallN(symGetNextFunction, v.C)
1667 return Value{r}
1668 }
1669
1670 func (v Value) EraseFromParentAsFunction() {
1671 purego.SyscallN(symDeleteFunction, v.C)
1672 }
1673
1674 func (v Value) ParamsCount() int {
1675 r, _, _ := purego.SyscallN(symCountParams, v.C)
1676 return int(r)
1677 }
1678
1679 func (v Value) Params() []Value {
1680 count := v.ParamsCount()
1681 if count == 0 {
1682 return nil
1683 }
1684 out := make([]Value, count)
1685 purego.SyscallN(symGetParams, v.C, uintptr(unsafe.Pointer(&out[0])))
1686 return out
1687 }
1688
1689 func (v Value) Param(i int) Value {
1690 r, _, _ := purego.SyscallN(symGetParam, v.C, uintptr(i))
1691 return Value{r}
1692 }
1693
1694 func (v Value) SetFunctionCallConv(cc CallConv) {
1695 purego.SyscallN(symSetFunctionCallConv, v.C, uintptr(cc))
1696 }
1697
1698 func (v Value) FunctionCallConv() CallConv {
1699 r, _, _ := purego.SyscallN(symGetFunctionCallConv, v.C)
1700 return CallConv(r)
1701 }
1702
1703 func (v Value) AddFunctionAttr(attr Attribute) {
1704 // LLVMAttributeFunctionIndex = -1, but passed as unsigned = 0xFFFFFFFF
1705 purego.SyscallN(symAddFuncAttr, v.C, ^uintptr(0), attr.C)
1706 }
1707
1708 func (v Value) GetEnumFunctionAttribute(kind uint) Attribute {
1709 r, _, _ := purego.SyscallN(symGetEnumFuncAttr, v.C, ^uintptr(0), uintptr(kind))
1710 return Attribute{C: r}
1711 }
1712
1713 func (v Value) GetEnumAttributeAtIndex(index int, kind uint) Attribute {
1714 r, _, _ := purego.SyscallN(symGetEnumFuncAttr, v.C, uintptr(index), uintptr(kind))
1715 return Attribute{C: r}
1716 }
1717
1718 func (v Value) RemoveEnumFunctionAttribute(index int, kindID uint) {
1719 purego.SyscallN(symRemoveEnumFuncAttr, v.C, uintptr(index), uintptr(kindID))
1720 }
1721
1722 func (v Value) AddTargetDependentFunctionAttr(attr, val string) {
1723 cattr := cString(attr)
1724 cval := cString(val)
1725 purego.SyscallN(symAddTargetDepFuncAttr, v.C, cattr, cval)
1726 runtime.KeepAlive(attr)
1727 runtime.KeepAlive(val)
1728 }
1729
1730 // ---- Basic blocks ----
1731
1732 func (c Context) AddBasicBlock(f Value, name string) BasicBlock {
1733 cname := cString(name)
1734 r, _, _ := purego.SyscallN(symAppendBasicBlockInCtx, c.C, f.C, cname)
1735 runtime.KeepAlive(name)
1736 return BasicBlock{r}
1737 }
1738
1739 func AddBasicBlock(f Value, name string) BasicBlock {
1740 return GlobalContext().AddBasicBlock(f, name)
1741 }
1742
1743 func (c Context) InsertBasicBlock(ref BasicBlock, name string) BasicBlock {
1744 cname := cString(name)
1745 r, _, _ := purego.SyscallN(symInsertBasicBlockInCtx, c.C, ref.C, cname)
1746 runtime.KeepAlive(name)
1747 return BasicBlock{r}
1748 }
1749
1750 func (bb BasicBlock) Parent() Value {
1751 r, _, _ := purego.SyscallN(symGetBasicBlockParent, bb.C)
1752 return Value{r}
1753 }
1754
1755 func (v Value) EntryBasicBlock() BasicBlock {
1756 r, _, _ := purego.SyscallN(symGetEntryBasicBlock, v.C)
1757 return BasicBlock{r}
1758 }
1759
1760 func (v Value) FirstBasicBlock() BasicBlock {
1761 r, _, _ := purego.SyscallN(symGetFirstBasicBlock, v.C)
1762 return BasicBlock{r}
1763 }
1764
1765 func (v Value) LastBasicBlock() BasicBlock {
1766 r, _, _ := purego.SyscallN(symGetLastBasicBlock, v.C)
1767 return BasicBlock{r}
1768 }
1769
1770 func (bb BasicBlock) NextBasicBlock() BasicBlock {
1771 r, _, _ := purego.SyscallN(symGetNextBasicBlock, bb.C)
1772 return BasicBlock{r}
1773 }
1774
1775 func (bb BasicBlock) PrevBasicBlock() BasicBlock {
1776 r, _, _ := purego.SyscallN(symGetPreviousBasicBlock, bb.C)
1777 return BasicBlock{r}
1778 }
1779
1780 func (v Value) BasicBlocksCount() int {
1781 r, _, _ := purego.SyscallN(symCountBasicBlocks, v.C)
1782 return int(r)
1783 }
1784
1785 func (bb BasicBlock) MoveAfter(other BasicBlock) {
1786 purego.SyscallN(symMoveBasicBlockAfter, bb.C, other.C)
1787 }
1788
1789 func (bb BasicBlock) MoveBefore(other BasicBlock) {
1790 purego.SyscallN(symMoveBasicBlockBefore, bb.C, other.C)
1791 }
1792
1793 func (bb BasicBlock) EraseFromParent() {
1794 purego.SyscallN(symDeleteBasicBlock, bb.C)
1795 }
1796
1797 func (bb BasicBlock) AsValue() Value {
1798 r, _, _ := purego.SyscallN(symBasicBlockAsValue, bb.C)
1799 return Value{r}
1800 }
1801
1802 // ---- Instructions ----
1803
1804 func (bb BasicBlock) FirstInstruction() Value {
1805 r, _, _ := purego.SyscallN(symGetFirstInstruction, bb.C)
1806 return Value{r}
1807 }
1808
1809 func (bb BasicBlock) LastInstruction() Value {
1810 r, _, _ := purego.SyscallN(symGetLastInstruction, bb.C)
1811 return Value{r}
1812 }
1813
1814 func (v Value) NextInstruction() Value {
1815 r, _, _ := purego.SyscallN(symGetNextInstruction, v.C)
1816 return Value{r}
1817 }
1818
1819 func (v Value) InstructionOpcode() Opcode {
1820 r, _, _ := purego.SyscallN(symGetInstructionOpcode, v.C)
1821 return Opcode(r)
1822 }
1823
1824 func (v Value) EraseFromParentAsInstruction() {
1825 purego.SyscallN(symInstructionEraseFromParent, v.C)
1826 }
1827
1828 func (v Value) InstructionParent() BasicBlock {
1829 r, _, _ := purego.SyscallN(symGetInstructionParent, v.C)
1830 return BasicBlock{r}
1831 }
1832
1833 func (v Value) IsATerminatorInst() Value {
1834 r, _, _ := purego.SyscallN(symIsATerminatorInst, v.C)
1835 return Value{r}
1836 }
1837
1838 func (v Value) IsAAllocaInst() Value {
1839 r, _, _ := purego.SyscallN(symIsAAllocaInst, v.C)
1840 return Value{r}
1841 }
1842
1843 func (v Value) IsAConstant() Value {
1844 r, _, _ := purego.SyscallN(symIsAConstant, v.C)
1845 return Value{r}
1846 }
1847
1848 func (v Value) IsAConstantInt() Value {
1849 r, _, _ := purego.SyscallN(symIsAConstantInt, v.C)
1850 return Value{r}
1851 }
1852
1853 func (v Value) IsAFunction() Value {
1854 r, _, _ := purego.SyscallN(symIsAFunction, v.C)
1855 return Value{r}
1856 }
1857
1858 func (v Value) IsAGlobalValue() Value {
1859 r, _, _ := purego.SyscallN(symIsAGlobalValue, v.C)
1860 return Value{r}
1861 }
1862
1863 func (v Value) IsAInstruction() Value {
1864 r, _, _ := purego.SyscallN(symIsAInstruction, v.C)
1865 return Value{r}
1866 }
1867
1868 func (v Value) IsAPHINode() Value {
1869 r, _, _ := purego.SyscallN(symIsAPHINode, v.C)
1870 return Value{r}
1871 }
1872
1873 func (v Value) IsAUndefValue() Value {
1874 r, _, _ := purego.SyscallN(symIsAUndefValue, v.C)
1875 return Value{r}
1876 }
1877
1878 func (v Value) IsDeclaration() bool {
1879 r, _, _ := purego.SyscallN(symIsDeclaration, v.C)
1880 return r != 0
1881 }
1882
1883 func (v Value) GlobalValueType() Type {
1884 r, _, _ := purego.SyscallN(symGlobalGetValType, v.C)
1885 return Type{r}
1886 }
1887
1888 func (v Value) LastParam() Value {
1889 r, _, _ := purego.SyscallN(symGetLastParam, v.C)
1890 return Value{r}
1891 }
1892
1893 func (v Value) Operand(i int) Value {
1894 r, _, _ := purego.SyscallN(symGetOperand, v.C, uintptr(i))
1895 return Value{r}
1896 }
1897
1898 func (v Value) OperandsCount() int {
1899 r, _, _ := purego.SyscallN(symGetNumOperands, v.C)
1900 return int(r)
1901 }
1902
1903 func (v Value) SetVolatile(volatile bool) {
1904 purego.SyscallN(symSetVolatile, v.C, boolToInt(volatile))
1905 }
1906
1907 func (v Value) RemoveFromParentAsInstruction() {
1908 purego.SyscallN(symInstructionRemoveFromParent, v.C)
1909 }
1910
1911 func (v Value) IncomingCount() int {
1912 r, _, _ := purego.SyscallN(symCountIncoming, v.C)
1913 return int(r)
1914 }
1915
1916 func (v Value) IncomingValue(i int) Value {
1917 r, _, _ := purego.SyscallN(symGetIncomingValue, v.C, uintptr(i))
1918 return Value{r}
1919 }
1920
1921 func (v Value) IncomingBlock(i int) BasicBlock {
1922 r, _, _ := purego.SyscallN(symGetIncomingBlock, v.C, uintptr(i))
1923 return BasicBlock{r}
1924 }
1925
1926 func (v Value) AddAttributeAtIndex(i int, a Attribute) {
1927 purego.SyscallN(symAddFuncAttr, v.C, uintptr(i), a.C)
1928 }
1929
1930 func (v Value) AddMetadata(kind int, md Metadata) {
1931 if symGlobalObjectAddMetadata == 0 {
1932 return
1933 }
1934 purego.SyscallN(symGlobalObjectAddMetadata, v.C, uintptr(kind), md.C)
1935 }
1936
1937 func (v Value) CalledValue() (rv Value) {
1938 r, _, _ := purego.SyscallN(symGetCalledValue, v.C)
1939 return Value{C: r}
1940 }
1941
1942 func (v Value) AllocatedType() (t Type) {
1943 r, _, _ := purego.SyscallN(symGetAllocatedType, v.C)
1944 return Type{C: r}
1945 }
1946
1947 func (v Value) GEPSourceElementType() (t Type) {
1948 r, _, _ := purego.SyscallN(symGetGEPSourceElementType, v.C)
1949 return Type{C: r}
1950 }
1951
1952 func (v Value) Indices() []uint32 {
1953 n, _, _ := purego.SyscallN(symGetNumIndices, v.C)
1954 if n == 0 {
1955 return nil
1956 }
1957 ptr, _, _ := purego.SyscallN(symGetIndices, v.C)
1958 result := make([]uint32, int(n))
1959 for i := 0; i < int(n); i++ {
1960 result[i] = *(*uint32)(unsafe.Pointer(ptr + uintptr(i)*4))
1961 }
1962 return result
1963 }
1964
1965 func (v Value) IntPredicate() IntPredicate {
1966 r, _, _ := purego.SyscallN(symGetICmpPredicate, v.C)
1967 return IntPredicate(r)
1968 }
1969
1970 func (v Value) FloatPredicate() FloatPredicate {
1971 r, _, _ := purego.SyscallN(symGetFCmpPredicate, v.C)
1972 return FloatPredicate(r)
1973 }
1974
1975 func (v Value) IsAReturnInst() (rv Value) {
1976 r, _, _ := purego.SyscallN(symIsAReturnInst, v.C)
1977 return Value{C: r}
1978 }
1979
1980 func (v Value) IsAConstantExpr() (rv Value) {
1981 r, _, _ := purego.SyscallN(symIsAConstantExpr, v.C)
1982 return Value{C: r}
1983 }
1984
1985 func (v Value) IsAGetElementPtrInst() (rv Value) {
1986 r, _, _ := purego.SyscallN(symIsAGetElementPtrInst, v.C)
1987 return Value{C: r}
1988 }
1989
1990 func (v Value) ConstantAsMetadata() (md Metadata) {
1991 r, _, _ := purego.SyscallN(symConstantAsMetadata, v.C)
1992 return Metadata{C: r}
1993 }
1994
1995 func (v Value) IsACallInst() (rv Value) {
1996 r, _, _ := purego.SyscallN(symIsACallInst, v.C)
1997 return Value{C: r}
1998 }
1999
2000 func (v Value) IsABitCastInst() (rv Value) {
2001 r, _, _ := purego.SyscallN(symIsABitCastInst, v.C)
2002 return Value{C: r}
2003 }
2004
2005 func (v Value) GlobalParent() (m Module) {
2006 r, _, _ := purego.SyscallN(symGetGlobalParent, v.C)
2007 return Module{C: r}
2008 }
2009
2010 func (v Value) GetCallSiteEnumAttribute(i int, kind uint) (a Attribute) {
2011 r, _, _ := purego.SyscallN(symGetCallSiteEnumAttr, v.C, uintptr(i), uintptr(kind))
2012 return Attribute{C: r}
2013 }
2014
2015 func (v Value) GetStringAttributeAtIndex(i int, kind string) (a Attribute) {
2016 ckind := cString(kind)
2017 r, _, _ := purego.SyscallN(symGetStringAttrAtIndex, v.C, uintptr(i), ckind, uintptr(len(kind)))
2018 return Attribute{C: r}
2019 }
2020
2021 func (v Value) Opcode() Opcode {
2022 r, _, _ := purego.SyscallN(symGetConstOpcode, v.C)
2023 return Opcode(r)
2024 }
2025
2026 func (v Value) IsVolatile() bool {
2027 r, _, _ := purego.SyscallN(symGetVolatile, v.C)
2028 return r != 0
2029 }
2030
2031 func (v Value) Ordering() AtomicOrdering {
2032 r, _, _ := purego.SyscallN(symGetOrdering, v.C)
2033 return AtomicOrdering(r)
2034 }
2035
2036 func (v Value) FirstUse() (u Use) {
2037 r, _, _ := purego.SyscallN(symGetFirstUse, v.C)
2038 return Use{C: r}
2039 }
2040
2041 func (u Use) NextUse() (ru Use) {
2042 r, _, _ := purego.SyscallN(symGetNextUse, u.C)
2043 return Use{C: r}
2044 }
2045
2046 func (u Use) User() (v Value) {
2047 r, _, _ := purego.SyscallN(symGetUser, u.C)
2048 return Value{C: r}
2049 }
2050
2051 func (u Use) UsedValue() (v Value) {
2052 r, _, _ := purego.SyscallN(symGetUsedValue, u.C)
2053 return Value{C: r}
2054 }
2055
2056 func (v Value) IsAArgument() (rv Value) {
2057 r, _, _ := purego.SyscallN(symIsAArgument, v.C)
2058 return Value{C: r}
2059 }
2060
2061 func (v Value) IsAGlobalVariable() (rv Value) {
2062 r, _, _ := purego.SyscallN(symIsAGlobalVariable, v.C)
2063 return Value{C: r}
2064 }
2065
2066 func (v Value) IsAInlineAsm() (rv Value) {
2067 r, _, _ := purego.SyscallN(symIsAInlineAsm, v.C)
2068 return Value{C: r}
2069 }
2070
2071 func (v Value) IsAExtractValueInst() (rv Value) {
2072 r, _, _ := purego.SyscallN(symIsAExtractValueInst, v.C)
2073 return Value{C: r}
2074 }
2075
2076 func (v Value) IsALoadInst() (rv Value) {
2077 r, _, _ := purego.SyscallN(symIsALoadInst, v.C)
2078 return Value{C: r}
2079 }
2080
2081 func (v Value) CalledFunctionType() (t Type) {
2082 r, _, _ := purego.SyscallN(symGetCalledFunctionType, v.C)
2083 return Type{C: r}
2084 }
2085
2086 func (v Value) SetTailCall(isTailCall bool) {
2087 purego.SyscallN(symSetTailCall, v.C, boolToInt(isTailCall))
2088 }
2089
2090 func (v Value) FirstParam() (rv Value) {
2091 r, _, _ := purego.SyscallN(symGetFirstParam, v.C)
2092 return Value{C: r}
2093 }
2094
2095 // Free-function wrappers for iteration and navigation
2096
2097 func PrevInstruction(v Value) (rv Value) {
2098 r, _, _ := purego.SyscallN(symGetPreviousInstruction, v.C)
2099 return Value{C: r}
2100 }
2101
2102 func PrevParam(v Value) (rv Value) {
2103 r, _, _ := purego.SyscallN(symGetPreviousParam, v.C)
2104 return Value{C: r}
2105 }
2106
2107 // Free-function wrappers for iteration (compiler uses these)
2108
2109 func NextInstruction(v Value) Value { return v.NextInstruction() }
2110 func NextBasicBlock(bb BasicBlock) BasicBlock { return bb.NextBasicBlock() }
2111 func NextFunction(v Value) Value { return v.NextFunction() }
2112 func NextGlobal(v Value) Value { return v.NextGlobal() }
2113
2114 // ---- Builder ----
2115
2116 func (c Context) NewBuilder() Builder {
2117 r, _, _ := purego.SyscallN(symCreateBuilderInCtx, c.C)
2118 return Builder{r}
2119 }
2120
2121 func (b Builder) SetInsertPointAtEnd(bb BasicBlock) {
2122 purego.SyscallN(symPositionBuilderAtEnd, b.C, bb.C)
2123 }
2124
2125 func (b Builder) SetInsertPointBefore(instr Value) {
2126 parent := instr.InstructionParent()
2127 purego.SyscallN(symPositionBuilderBefore, b.C, parent.C, instr.C)
2128 }
2129
2130 func (b Builder) GetInsertBlock() BasicBlock {
2131 r, _, _ := purego.SyscallN(symGetInsertBlock, b.C)
2132 return BasicBlock{r}
2133 }
2134
2135 func (b Builder) Dispose() {
2136 purego.SyscallN(symDisposeBuilder, b.C)
2137 }
2138
2139 func (b Builder) Insert(instr Value) {
2140 purego.SyscallN(symInsertIntoBuilder, b.C, instr.C)
2141 }
2142
2143 func (b Builder) CreateBinOp(op Opcode, lhs, rhs Value, name string) Value {
2144 cname := cString(name)
2145 r, _, _ := purego.SyscallN(symBuildBinOp, b.C, uintptr(op), lhs.C, rhs.C, cname)
2146 runtime.KeepAlive(name)
2147 return Value{r}
2148 }
2149
2150 // Builder - terminators
2151
2152 func (b Builder) CreateRetVoid() Value {
2153 r, _, _ := purego.SyscallN(symBuildRetVoid, b.C)
2154 return Value{r}
2155 }
2156
2157 func (b Builder) CreateRet(v Value) Value {
2158 r, _, _ := purego.SyscallN(symBuildRet, b.C, v.C)
2159 return Value{r}
2160 }
2161
2162 func (b Builder) CreateBr(dest BasicBlock) Value {
2163 r, _, _ := purego.SyscallN(symBuildBr, b.C, dest.C)
2164 return Value{r}
2165 }
2166
2167 func (b Builder) CreateCondBr(cond Value, thenBB, elseBB BasicBlock) Value {
2168 r, _, _ := purego.SyscallN(symBuildCondBr, b.C, cond.C, thenBB.C, elseBB.C)
2169 return Value{r}
2170 }
2171
2172 func (b Builder) CreateSwitch(v Value, elseBB BasicBlock, numCases int) Value {
2173 r, _, _ := purego.SyscallN(symBuildSwitch, b.C, v.C, elseBB.C, uintptr(numCases))
2174 return Value{r}
2175 }
2176
2177 func (b Builder) CreateUnreachable() Value {
2178 r, _, _ := purego.SyscallN(symBuildUnreachable, b.C)
2179 return Value{r}
2180 }
2181
2182 // Builder - arithmetic
2183
2184 func (b Builder) CreateAdd(lhs, rhs Value, name string) Value {
2185 cname := cString(name)
2186 r, _, _ := purego.SyscallN(symBuildAdd, b.C, lhs.C, rhs.C, cname)
2187 runtime.KeepAlive(name)
2188 return Value{r}
2189 }
2190
2191 func (b Builder) CreateNSWAdd(lhs, rhs Value, name string) Value {
2192 cname := cString(name)
2193 r, _, _ := purego.SyscallN(symBuildNSWAdd, b.C, lhs.C, rhs.C, cname)
2194 runtime.KeepAlive(name)
2195 return Value{r}
2196 }
2197
2198 func (b Builder) CreateNUWAdd(lhs, rhs Value, name string) Value {
2199 cname := cString(name)
2200 r, _, _ := purego.SyscallN(symBuildNUWAdd, b.C, lhs.C, rhs.C, cname)
2201 runtime.KeepAlive(name)
2202 return Value{r}
2203 }
2204
2205 func (b Builder) CreateFAdd(lhs, rhs Value, name string) Value {
2206 cname := cString(name)
2207 r, _, _ := purego.SyscallN(symBuildFAdd, b.C, lhs.C, rhs.C, cname)
2208 runtime.KeepAlive(name)
2209 return Value{r}
2210 }
2211
2212 func (b Builder) CreateSub(lhs, rhs Value, name string) Value {
2213 cname := cString(name)
2214 r, _, _ := purego.SyscallN(symBuildSub, b.C, lhs.C, rhs.C, cname)
2215 runtime.KeepAlive(name)
2216 return Value{r}
2217 }
2218
2219 func (b Builder) CreateNSWSub(lhs, rhs Value, name string) Value {
2220 cname := cString(name)
2221 r, _, _ := purego.SyscallN(symBuildNSWSub, b.C, lhs.C, rhs.C, cname)
2222 runtime.KeepAlive(name)
2223 return Value{r}
2224 }
2225
2226 func (b Builder) CreateNUWSub(lhs, rhs Value, name string) Value {
2227 cname := cString(name)
2228 r, _, _ := purego.SyscallN(symBuildNUWSub, b.C, lhs.C, rhs.C, cname)
2229 runtime.KeepAlive(name)
2230 return Value{r}
2231 }
2232
2233 func (b Builder) CreateFSub(lhs, rhs Value, name string) Value {
2234 cname := cString(name)
2235 r, _, _ := purego.SyscallN(symBuildFSub, b.C, lhs.C, rhs.C, cname)
2236 runtime.KeepAlive(name)
2237 return Value{r}
2238 }
2239
2240 func (b Builder) CreateMul(lhs, rhs Value, name string) Value {
2241 cname := cString(name)
2242 r, _, _ := purego.SyscallN(symBuildMul, b.C, lhs.C, rhs.C, cname)
2243 runtime.KeepAlive(name)
2244 return Value{r}
2245 }
2246
2247 func (b Builder) CreateNSWMul(lhs, rhs Value, name string) Value {
2248 cname := cString(name)
2249 r, _, _ := purego.SyscallN(symBuildNSWMul, b.C, lhs.C, rhs.C, cname)
2250 runtime.KeepAlive(name)
2251 return Value{r}
2252 }
2253
2254 func (b Builder) CreateNUWMul(lhs, rhs Value, name string) Value {
2255 cname := cString(name)
2256 r, _, _ := purego.SyscallN(symBuildNUWMul, b.C, lhs.C, rhs.C, cname)
2257 runtime.KeepAlive(name)
2258 return Value{r}
2259 }
2260
2261 func (b Builder) CreateFMul(lhs, rhs Value, name string) Value {
2262 cname := cString(name)
2263 r, _, _ := purego.SyscallN(symBuildFMul, b.C, lhs.C, rhs.C, cname)
2264 runtime.KeepAlive(name)
2265 return Value{r}
2266 }
2267
2268 func (b Builder) CreateUDiv(lhs, rhs Value, name string) Value {
2269 cname := cString(name)
2270 r, _, _ := purego.SyscallN(symBuildUDiv, b.C, lhs.C, rhs.C, cname)
2271 runtime.KeepAlive(name)
2272 return Value{r}
2273 }
2274
2275 func (b Builder) CreateSDiv(lhs, rhs Value, name string) Value {
2276 cname := cString(name)
2277 r, _, _ := purego.SyscallN(symBuildSDiv, b.C, lhs.C, rhs.C, cname)
2278 runtime.KeepAlive(name)
2279 return Value{r}
2280 }
2281
2282 func (b Builder) CreateExactSDiv(lhs, rhs Value, name string) Value {
2283 cname := cString(name)
2284 r, _, _ := purego.SyscallN(symBuildExactSDiv, b.C, lhs.C, rhs.C, cname)
2285 runtime.KeepAlive(name)
2286 return Value{r}
2287 }
2288
2289 func (b Builder) CreateFDiv(lhs, rhs Value, name string) Value {
2290 cname := cString(name)
2291 r, _, _ := purego.SyscallN(symBuildFDiv, b.C, lhs.C, rhs.C, cname)
2292 runtime.KeepAlive(name)
2293 return Value{r}
2294 }
2295
2296 func (b Builder) CreateURem(lhs, rhs Value, name string) Value {
2297 cname := cString(name)
2298 r, _, _ := purego.SyscallN(symBuildURem, b.C, lhs.C, rhs.C, cname)
2299 runtime.KeepAlive(name)
2300 return Value{r}
2301 }
2302
2303 func (b Builder) CreateSRem(lhs, rhs Value, name string) Value {
2304 cname := cString(name)
2305 r, _, _ := purego.SyscallN(symBuildSRem, b.C, lhs.C, rhs.C, cname)
2306 runtime.KeepAlive(name)
2307 return Value{r}
2308 }
2309
2310 func (b Builder) CreateFRem(lhs, rhs Value, name string) Value {
2311 cname := cString(name)
2312 r, _, _ := purego.SyscallN(symBuildFRem, b.C, lhs.C, rhs.C, cname)
2313 runtime.KeepAlive(name)
2314 return Value{r}
2315 }
2316
2317 func (b Builder) CreateShl(lhs, rhs Value, name string) Value {
2318 cname := cString(name)
2319 r, _, _ := purego.SyscallN(symBuildShl, b.C, lhs.C, rhs.C, cname)
2320 runtime.KeepAlive(name)
2321 return Value{r}
2322 }
2323
2324 func (b Builder) CreateLShr(lhs, rhs Value, name string) Value {
2325 cname := cString(name)
2326 r, _, _ := purego.SyscallN(symBuildLShr, b.C, lhs.C, rhs.C, cname)
2327 runtime.KeepAlive(name)
2328 return Value{r}
2329 }
2330
2331 func (b Builder) CreateAShr(lhs, rhs Value, name string) Value {
2332 cname := cString(name)
2333 r, _, _ := purego.SyscallN(symBuildAShr, b.C, lhs.C, rhs.C, cname)
2334 runtime.KeepAlive(name)
2335 return Value{r}
2336 }
2337
2338 func (b Builder) CreateAnd(lhs, rhs Value, name string) Value {
2339 cname := cString(name)
2340 r, _, _ := purego.SyscallN(symBuildAnd, b.C, lhs.C, rhs.C, cname)
2341 runtime.KeepAlive(name)
2342 return Value{r}
2343 }
2344
2345 func (b Builder) CreateOr(lhs, rhs Value, name string) Value {
2346 cname := cString(name)
2347 r, _, _ := purego.SyscallN(symBuildOr, b.C, lhs.C, rhs.C, cname)
2348 runtime.KeepAlive(name)
2349 return Value{r}
2350 }
2351
2352 func (b Builder) CreateXor(lhs, rhs Value, name string) Value {
2353 cname := cString(name)
2354 r, _, _ := purego.SyscallN(symBuildXor, b.C, lhs.C, rhs.C, cname)
2355 runtime.KeepAlive(name)
2356 return Value{r}
2357 }
2358
2359 func (b Builder) CreateNeg(v Value, name string) Value {
2360 cname := cString(name)
2361 r, _, _ := purego.SyscallN(symBuildNeg, b.C, v.C, cname)
2362 runtime.KeepAlive(name)
2363 return Value{r}
2364 }
2365
2366 func (b Builder) CreateNSWNeg(v Value, name string) Value {
2367 cname := cString(name)
2368 r, _, _ := purego.SyscallN(symBuildNSWNeg, b.C, v.C, cname)
2369 runtime.KeepAlive(name)
2370 return Value{r}
2371 }
2372
2373 func (b Builder) CreateNot(v Value, name string) Value {
2374 cname := cString(name)
2375 r, _, _ := purego.SyscallN(symBuildNot, b.C, v.C, cname)
2376 runtime.KeepAlive(name)
2377 return Value{r}
2378 }
2379
2380 func (b Builder) CreateFNeg(v Value, name string) Value {
2381 cname := cString(name)
2382 r, _, _ := purego.SyscallN(symBuildFNeg, b.C, v.C, cname)
2383 runtime.KeepAlive(name)
2384 return Value{r}
2385 }
2386
2387 // Builder - memory
2388
2389 func (b Builder) CreateAlloca(t Type, name string) Value {
2390 cname := cString(name)
2391 r, _, _ := purego.SyscallN(symBuildAlloca, b.C, t.C, cname)
2392 runtime.KeepAlive(name)
2393 return Value{r}
2394 }
2395
2396 func (b Builder) CreateLoad(t Type, ptr Value, name string) Value {
2397 cname := cString(name)
2398 r, _, _ := purego.SyscallN(symBuildLoad2, b.C, t.C, ptr.C, cname)
2399 runtime.KeepAlive(name)
2400 return Value{r}
2401 }
2402
2403 func (b Builder) CreateStore(val, ptr Value) Value {
2404 r, _, _ := purego.SyscallN(symBuildStore, b.C, val.C, ptr.C)
2405 return Value{r}
2406 }
2407
2408 func (b Builder) CreateGEP(t Type, ptr Value, indices []Value, name string) Value {
2409 cname := cString(name)
2410 r, _, _ := purego.SyscallN(symBuildGEP2, b.C, t.C, ptr.C, valueRefPtr(indices), uintptr(len(indices)), cname)
2411 runtime.KeepAlive(name)
2412 return Value{r}
2413 }
2414
2415 func (b Builder) CreateInBoundsGEP(t Type, ptr Value, indices []Value, name string) Value {
2416 cname := cString(name)
2417 r, _, _ := purego.SyscallN(symBuildInBoundsGEP2, b.C, t.C, ptr.C, valueRefPtr(indices), uintptr(len(indices)), cname)
2418 runtime.KeepAlive(name)
2419 return Value{r}
2420 }
2421
2422 func (b Builder) CreateStructGEP(t Type, ptr Value, idx int, name string) Value {
2423 cname := cString(name)
2424 r, _, _ := purego.SyscallN(symBuildStructGEP2, b.C, t.C, ptr.C, uintptr(idx), cname)
2425 runtime.KeepAlive(name)
2426 return Value{r}
2427 }
2428
2429 // Builder - casts
2430
2431 func (b Builder) CreateTrunc(val Value, t Type, name string) Value {
2432 cname := cString(name)
2433 r, _, _ := purego.SyscallN(symBuildTrunc, b.C, val.C, t.C, cname)
2434 runtime.KeepAlive(name)
2435 return Value{r}
2436 }
2437
2438 func (b Builder) CreateZExt(val Value, t Type, name string) Value {
2439 cname := cString(name)
2440 r, _, _ := purego.SyscallN(symBuildZExt, b.C, val.C, t.C, cname)
2441 runtime.KeepAlive(name)
2442 return Value{r}
2443 }
2444
2445 func (b Builder) CreateSExt(val Value, t Type, name string) Value {
2446 cname := cString(name)
2447 r, _, _ := purego.SyscallN(symBuildSExt, b.C, val.C, t.C, cname)
2448 runtime.KeepAlive(name)
2449 return Value{r}
2450 }
2451
2452 func (b Builder) CreateFPToUI(val Value, t Type, name string) Value {
2453 cname := cString(name)
2454 r, _, _ := purego.SyscallN(symBuildFPToUI, b.C, val.C, t.C, cname)
2455 runtime.KeepAlive(name)
2456 return Value{r}
2457 }
2458
2459 func (b Builder) CreateFPToSI(val Value, t Type, name string) Value {
2460 cname := cString(name)
2461 r, _, _ := purego.SyscallN(symBuildFPToSI, b.C, val.C, t.C, cname)
2462 runtime.KeepAlive(name)
2463 return Value{r}
2464 }
2465
2466 func (b Builder) CreateUIToFP(val Value, t Type, name string) Value {
2467 cname := cString(name)
2468 r, _, _ := purego.SyscallN(symBuildUIToFP, b.C, val.C, t.C, cname)
2469 runtime.KeepAlive(name)
2470 return Value{r}
2471 }
2472
2473 func (b Builder) CreateSIToFP(val Value, t Type, name string) Value {
2474 cname := cString(name)
2475 r, _, _ := purego.SyscallN(symBuildSIToFP, b.C, val.C, t.C, cname)
2476 runtime.KeepAlive(name)
2477 return Value{r}
2478 }
2479
2480 func (b Builder) CreateFPTrunc(val Value, t Type, name string) Value {
2481 cname := cString(name)
2482 r, _, _ := purego.SyscallN(symBuildFPTrunc, b.C, val.C, t.C, cname)
2483 runtime.KeepAlive(name)
2484 return Value{r}
2485 }
2486
2487 func (b Builder) CreateFPExt(val Value, t Type, name string) Value {
2488 cname := cString(name)
2489 r, _, _ := purego.SyscallN(symBuildFPExt, b.C, val.C, t.C, cname)
2490 runtime.KeepAlive(name)
2491 return Value{r}
2492 }
2493
2494 func (b Builder) CreatePtrToInt(val Value, t Type, name string) Value {
2495 cname := cString(name)
2496 r, _, _ := purego.SyscallN(symBuildPtrToInt, b.C, val.C, t.C, cname)
2497 runtime.KeepAlive(name)
2498 return Value{r}
2499 }
2500
2501 func (b Builder) CreateIntToPtr(val Value, t Type, name string) Value {
2502 cname := cString(name)
2503 r, _, _ := purego.SyscallN(symBuildIntToPtr, b.C, val.C, t.C, cname)
2504 runtime.KeepAlive(name)
2505 return Value{r}
2506 }
2507
2508 func (b Builder) CreateBitCast(val Value, t Type, name string) Value {
2509 cname := cString(name)
2510 r, _, _ := purego.SyscallN(symBuildBitCast, b.C, val.C, t.C, cname)
2511 runtime.KeepAlive(name)
2512 return Value{r}
2513 }
2514
2515 func (b Builder) CreatePointerCast(val Value, t Type, name string) Value {
2516 cname := cString(name)
2517 r, _, _ := purego.SyscallN(symBuildPointerCast, b.C, val.C, t.C, cname)
2518 runtime.KeepAlive(name)
2519 return Value{r}
2520 }
2521
2522 // Builder - comparisons
2523
2524 func (b Builder) CreateICmp(pred IntPredicate, lhs, rhs Value, name string) Value {
2525 cname := cString(name)
2526 r, _, _ := purego.SyscallN(symBuildICmp, b.C, uintptr(pred), lhs.C, rhs.C, cname)
2527 runtime.KeepAlive(name)
2528 return Value{r}
2529 }
2530
2531 func (b Builder) CreateFCmp(pred FloatPredicate, lhs, rhs Value, name string) Value {
2532 cname := cString(name)
2533 r, _, _ := purego.SyscallN(symBuildFCmp, b.C, uintptr(pred), lhs.C, rhs.C, cname)
2534 runtime.KeepAlive(name)
2535 return Value{r}
2536 }
2537
2538 // Builder - misc
2539
2540 func (b Builder) CreatePHI(t Type, name string) Value {
2541 cname := cString(name)
2542 r, _, _ := purego.SyscallN(symBuildPhi, b.C, t.C, cname)
2543 runtime.KeepAlive(name)
2544 return Value{r}
2545 }
2546
2547 func (b Builder) CreateCall(fnType Type, fn Value, args []Value, name string) Value {
2548 cname := cString(name)
2549 r, _, _ := purego.SyscallN(symBuildCall2, b.C, fnType.C, fn.C, valueRefPtr(args), uintptr(len(args)), cname)
2550 runtime.KeepAlive(name)
2551 return Value{r}
2552 }
2553
2554 func (b Builder) CreateSelect(cond, thenVal, elseVal Value, name string) Value {
2555 cname := cString(name)
2556 r, _, _ := purego.SyscallN(symBuildSelect, b.C, cond.C, thenVal.C, elseVal.C, cname)
2557 runtime.KeepAlive(name)
2558 return Value{r}
2559 }
2560
2561 func (b Builder) CreateExtractValue(agg Value, index int, name string) Value {
2562 cname := cString(name)
2563 r, _, _ := purego.SyscallN(symBuildExtractValue, b.C, agg.C, uintptr(index), cname)
2564 runtime.KeepAlive(name)
2565 return Value{r}
2566 }
2567
2568 func (b Builder) CreateInsertValue(agg, val Value, index int, name string) Value {
2569 cname := cString(name)
2570 r, _, _ := purego.SyscallN(symBuildInsertValue, b.C, agg.C, val.C, uintptr(index), cname)
2571 runtime.KeepAlive(name)
2572 return Value{r}
2573 }
2574
2575 // ---- PHI ----
2576
2577 func (v Value) AddIncoming(vals []Value, blocks []BasicBlock) {
2578 if len(vals) == 0 {
2579 return
2580 }
2581 purego.SyscallN(symAddIncoming, v.C, valueRefPtr(vals), uintptr(unsafe.Pointer(&blocks[0])), uintptr(len(vals)))
2582 }
2583
2584 // ---- Call site ----
2585
2586 func (v Value) SetInstructionCallConv(cc CallConv) {
2587 purego.SyscallN(symSetInstructionCallConv, v.C, uintptr(cc))
2588 }
2589
2590 func (v Value) InstructionCallConv() CallConv {
2591 r, _, _ := purego.SyscallN(symGetInstructionCallConv, v.C)
2592 return CallConv(r)
2593 }
2594
2595 func (v Value) AddCallSiteAttribute(index int, attr Attribute) {
2596 purego.SyscallN(symAddCallSiteAttribute, v.C, uintptr(index), attr.C)
2597 }
2598
2599 func (v Value) SetInstrParamAlignment(index, align int) {
2600 purego.SyscallN(symSetInstrParamAlignment, v.C, uintptr(index), uintptr(align))
2601 }
2602
2603 // ---- Switch ----
2604
2605 func (v Value) AddCase(onVal Value, dest BasicBlock) {
2606 purego.SyscallN(symAddCase, v.C, onVal.C, dest.C)
2607 }
2608
2609 // ---- Landing pad ----
2610
2611 func (b Builder) CreateLandingPad(t Type, numClauses int, name string) Value {
2612 cname := cString(name)
2613 r, _, _ := purego.SyscallN(symBuildLandingPad, b.C, t.C, 0, uintptr(numClauses), cname)
2614 runtime.KeepAlive(name)
2615 return Value{r}
2616 }
2617
2618 func (v Value) SetCleanup(b bool) {
2619 purego.SyscallN(symSetCleanup, v.C, boolToInt(b))
2620 }
2621
2622 func (v Value) AddClause(clauseVal Value) {
2623 purego.SyscallN(symAddClause, v.C, clauseVal.C)
2624 }
2625
2626 // ---- Invoke ----
2627
2628 func (b Builder) CreateInvoke(fnType Type, fn Value, args []Value, thenBB, catchBB BasicBlock, name string) Value {
2629 cname := cString(name)
2630 r, _, _ := purego.SyscallN(symBuildInvoke2, b.C, fnType.C, fn.C, valueRefPtr(args), uintptr(len(args)), thenBB.C, catchBB.C, cname)
2631 runtime.KeepAlive(name)
2632 return Value{r}
2633 }
2634
2635 // ---- Resume ----
2636
2637 func (b Builder) CreateResume(v Value) Value {
2638 r, _, _ := purego.SyscallN(symBuildResume, b.C, v.C)
2639 return Value{r}
2640 }
2641
2642 // ---- Atomics ----
2643
2644 func (b Builder) CreateFence(ordering AtomicOrdering, singleThread bool, name string) Value {
2645 cname := cString(name)
2646 r, _, _ := purego.SyscallN(symBuildFence, b.C, uintptr(ordering), boolToInt(singleThread), cname)
2647 runtime.KeepAlive(name)
2648 return Value{r}
2649 }
2650
2651 func (b Builder) CreateAtomicRMW(op AtomicRMWBinOp, ptr, val Value, ordering AtomicOrdering, singleThread bool) Value {
2652 r, _, _ := purego.SyscallN(symBuildAtomicRMW, b.C, uintptr(op), ptr.C, val.C, uintptr(ordering), boolToInt(singleThread))
2653 return Value{r}
2654 }
2655
2656 func (b Builder) CreateAtomicCmpXchg(ptr, cmp, new_ Value, successOrdering, failureOrdering AtomicOrdering, singleThread bool) Value {
2657 r, _, _ := purego.SyscallN(symBuildAtomicCmpXchg, b.C, ptr.C, cmp.C, new_.C, uintptr(successOrdering), uintptr(failureOrdering), boolToInt(singleThread))
2658 return Value{r}
2659 }
2660
2661 func (v Value) SetOrdering(ordering AtomicOrdering) {
2662 purego.SyscallN(symSetOrdering, v.C, uintptr(ordering))
2663 }
2664
2665 func (v Value) SetAtomicSingleThread(singleThread bool) {
2666 purego.SyscallN(symSetAtomicSingleThread, v.C, boolToInt(singleThread))
2667 }
2668
2669 // ---- Metadata (glue functions) ----
2670
2671 func (c Context) MDString(str string) Metadata {
2672 if symMDString2 == 0 {
2673 return Metadata{}
2674 }
2675 cstr := cString(str)
2676 r, _, _ := purego.SyscallN(symMDString2, c.C, cstr, uintptr(len(str)))
2677 runtime.KeepAlive(str)
2678 return Metadata{r}
2679 }
2680
2681 func (c Context) MDNode(mds []Metadata) Metadata {
2682 if symMDNode2 == 0 {
2683 return Metadata{}
2684 }
2685 r, _, _ := purego.SyscallN(symMDNode2, c.C, metadataRefPtr(mds), uintptr(len(mds)))
2686 return Metadata{r}
2687 }
2688
2689 func ConstantAsMetadata(v Value) Metadata {
2690 if symConstantAsMetadata == 0 {
2691 return Metadata{}
2692 }
2693 r, _, _ := purego.SyscallN(symConstantAsMetadata, v.C)
2694 return Metadata{r}
2695 }
2696
2697 func (v Value) GlobalObjectAddMetadata(kind uint, md Metadata) {
2698 if symGlobalObjectAddMetadata == 0 {
2699 return
2700 }
2701 purego.SyscallN(symGlobalObjectAddMetadata, v.C, uintptr(kind), md.C)
2702 }
2703
2704 // ---- Inline asm (glue) ----
2705
2706 func InlineAsm(t Type, asmString, constraints string, hasSideEffects, isAlignStack bool, dialect InlineAsmDialect, canThrow bool) Value {
2707 if symGoGetInlineAsm == 0 {
2708 return Value{}
2709 }
2710 casm := cString(asmString)
2711 ccon := cString(constraints)
2712 r, _, _ := purego.SyscallN(symGoGetInlineAsm, t.C,
2713 casm, uintptr(len(asmString)),
2714 ccon, uintptr(len(constraints)),
2715 boolToInt(hasSideEffects), boolToInt(isAlignStack),
2716 uintptr(dialect), boolToInt(canThrow))
2717 runtime.KeepAlive(asmString)
2718 runtime.KeepAlive(constraints)
2719 return Value{r}
2720 }
2721
2722 // ---- Debug location (glue) ----
2723
2724 func (b Builder) SetCurrentDebugLocation(line, col uint, scope, inlinedAt Metadata) {
2725 if symGoSetCurrentDebugLocation == 0 {
2726 return
2727 }
2728 purego.SyscallN(symGoSetCurrentDebugLocation, b.C, uintptr(line), uintptr(col), scope.C, inlinedAt.C)
2729 }
2730
2731 func (b Builder) GetCurrentDebugLocation() DebugLoc {
2732 loc, _, _ := purego.SyscallN(symGetCurrentDebugLocation2, b.C)
2733 if loc == 0 {
2734 return DebugLoc{}
2735 }
2736 line, _, _ := purego.SyscallN(symDILocationGetLine, loc)
2737 col, _, _ := purego.SyscallN(symDILocationGetColumn, loc)
2738 scope, _, _ := purego.SyscallN(symDILocationGetScope, loc)
2739 inlinedAt, _, _ := purego.SyscallN(symDILocationGetInlinedAt, loc)
2740 return DebugLoc{Line: uint(line), Col: uint(col), Scope: Metadata{scope}, InlinedAt: Metadata{inlinedAt}}
2741 }
2742
2743 // ---- Module verify ----
2744
2745 func (m Module) VerifyModule() error {
2746 var msg uintptr
2747 r, _, _ := purego.SyscallN(symVerifyModule, m.C, 2, uintptr(unsafe.Pointer(&msg)))
2748 if r != 0 {
2749 s := goString(msg)
2750 purego.SyscallN(symDisposeMessage, msg)
2751 return fmt.Errorf("%s", s)
2752 }
2753 return nil
2754 }
2755
2756 func (v Value) VerifyFunction() error {
2757 r, _, _ := purego.SyscallN(symVerifyFunction, v.C, 2)
2758 if r != 0 {
2759 return fmt.Errorf("function verification failed")
2760 }
2761 return nil
2762 }
2763
2764 // ---- Comdat ----
2765
2766 func (m Module) Comdat(name string) Comdat {
2767 cname := cString(name)
2768 r, _, _ := purego.SyscallN(symGetOrInsertComdat, m.C, cname)
2769 runtime.KeepAlive(name)
2770 return Comdat{r}
2771 }
2772
2773 func (c Comdat) SetSelectionKind(kind ComdatSelectionKind) {
2774 purego.SyscallN(symComdatSetSelectionKind, c.C, uintptr(kind))
2775 }
2776
2777 func (v Value) SetComdat(c Comdat) {
2778 purego.SyscallN(symSetComdat, v.C, c.C)
2779 }
2780