UNIXworkcode

/* * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE * Version 2, December 2004 * * Copyright (C) 2018 Olaf Wintermann <olaf.wintermann@gmail.com> * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed. * * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION * * 0. You just DO WHAT THE FUCK YOU WANT TO. */ // GraalVM example: mixing Java and native Code (LLVM bitcode) import org.graalvm.polyglot.*; import java.io.*; /* * create a file test.c with the following content and compile it with: * clang -c -O1 -emit-llvm test.c #include <stdio.h> int main(void) { printf("Hello World!\n"); return 0; } int add(int a, int b) { return a+b; } */ public class HelloPolyglot { public static void main(String[] args) { try { // create a polyglot context that can access the native interface Context ctx = Context.newBuilder().allowAllAccess(true).build(); // load an llvm bitcode file File file = new File("test.bc"); Source source = Source.newBuilder("llvm", file).build(); Value c = ctx.eval(source); // get the main function and execute it c.getMember("main").execute(); // get add function Value add = c.getMember("add"); Value ret = add.execute(10, 20); System.out.println(ret.asInt()); } catch (IOException e) { System.err.println(e); } } }