use of org.bytedeco.llvm.LLVM.LLVMTargetMachineRef in project javacpp-presets by bytedeco.
the class EmitBitcode method EmitBitcodeAndRelocatableObject.
/**
* Sample for generating both LLVM bitcode and relocatable object file from an LLVM module
* <p>
* The generated module (and objec file) will have a single sum function, which returns
* the sum of two integers.
* <p>
* declare i32 @sum(i32 %lhs, i32 %rhs)
* <p>
* This sample contains code for the following steps
* <p>
* 1. Initializing required LLVM components
* 2. Generating LLVM IR for a sum function
* 3. Write the LLVM bitcode to a file on disk
* 4. Write the relocatable object file to a file on disk
* 5. Dispose of the allocated resources
*/
public static void EmitBitcodeAndRelocatableObject() {
// Stage 1: Initialize LLVM components
LLVMInitializeCore(LLVMGetGlobalPassRegistry());
LLVMInitializeNativeAsmPrinter();
LLVMInitializeNativeAsmParser();
LLVMInitializeNativeDisassembler();
LLVMInitializeNativeTarget();
// Stage 2: Build the sum function
LLVMContextRef context = LLVMContextCreate();
LLVMModuleRef module = LLVMModuleCreateWithNameInContext("sum", context);
LLVMBuilderRef builder = LLVMCreateBuilderInContext(context);
LLVMTypeRef i32Type = LLVMInt32TypeInContext(context);
PointerPointer<Pointer> sumArgumentTypes = new PointerPointer<>(2).put(0, i32Type).put(1, i32Type);
LLVMTypeRef sumType = LLVMFunctionType(i32Type, sumArgumentTypes, /* argumentCount */
2, /* isVariadic */
0);
LLVMValueRef sum = LLVMAddFunction(module, "sum", sumType);
LLVMSetFunctionCallConv(sum, LLVMCCallConv);
LLVMValueRef lhs = LLVMGetParam(sum, 0);
LLVMValueRef rhs = LLVMGetParam(sum, 1);
LLVMBasicBlockRef entry = LLVMAppendBasicBlockInContext(context, sum, "entry");
LLVMPositionBuilderAtEnd(builder, entry);
LLVMValueRef result = LLVMBuildAdd(builder, lhs, rhs, "result = lhs + rhs");
LLVMBuildRet(builder, result);
LLVMDumpModule(module);
if (LLVMVerifyModule(module, LLVMPrintMessageAction, error) != 0) {
System.out.println("Failed to validate module: " + error.getString());
return;
}
// Stage 3: Dump the module to file
if (LLVMWriteBitcodeToFile(module, "./sum.bc") != 0) {
System.err.println("Failed to write bitcode to file");
return;
}
// Stage 4: Create the relocatable object file
BytePointer triple = LLVMGetDefaultTargetTriple();
LLVMTargetRef target = new LLVMTargetRef();
if (LLVMGetTargetFromTriple(triple, target, error) != 0) {
System.out.println("Failed to get target from triple: " + error.getString());
LLVMDisposeMessage(error);
return;
}
String cpu = "generic";
String cpuFeatures = "";
int optimizationLevel = 0;
LLVMTargetMachineRef tm = LLVMCreateTargetMachine(target, triple.getString(), cpu, cpuFeatures, optimizationLevel, LLVMRelocDefault, LLVMCodeModelDefault);
BytePointer outputFile = new BytePointer("./sum.o");
if (LLVMTargetMachineEmitToFile(tm, module, outputFile, LLVMObjectFile, error) != 0) {
System.err.println("Failed to emit relocatable object file: " + error.getString());
LLVMDisposeMessage(error);
return;
}
// Stage 5: Dispose of allocated resources
outputFile.deallocate();
LLVMDisposeMessage(triple);
LLVMDisposeBuilder(builder);
LLVMDisposeModule(module);
LLVMContextDispose(context);
}
Aggregations