llvm-ir-editor
llvm-ir-editor copied to clipboard
Tycho build and sync with Maven Central
To allow the LLVM IR parser to be used in Maven/Gradle/Ivy projects we could move to using Tycho. The resulting artifacts could then be uploaded to Maven Central through Sonatype Nexus.
It is unfortunate that it is not currently possible to configure a "POM first" Tycho build. The dependencies (Xtext in particular) are not available from a Maven repository. Projects wishing to use the IR model are tied to P2 for automated dependency resolution. The *StandaloneSetup class generated by Xtext provides registration support in non-Equinox OSGI environments. Maven projects could use this to build and serialise IR models like so:
LLVM_IRFactory factory = LLVM_IRFactoryImpl.eINSTANCE;
Model model = factory.createModel();
GlobalVariable global = factory.createGlobalVariable();
global.setName("foo");
Type type = factory.createType();
NonLeftRecursiveType nonLeftRecursiveType = factory
.createNonLeftRecursiveType();
nonLeftRecursiveType.setType(factory.createOpaqueType());
type.setBaseType(nonLeftRecursiveType);
global.setType(type);
model.getElements().add(global);
FunctionDef funcDef = factory.createFunctionDef();
FunctionHeader funcHeader = factory.createFunctionHeader();
funcHeader.setName("@b1");
Parameters params = factory.createParameters();
Parameter param = factory.createParameter();
param.setName("%argc");
ParameterType paramType = factory.createParameterType();
Type type4 = factory.createType();
NonLeftRecursiveType nonLeftRecursiveType4 = factory
.createNonLeftRecursiveType();
nonLeftRecursiveType4.setType(factory.createIntType());
type4.setBaseType(nonLeftRecursiveType4);
paramType.setType(type4);
param.setType(paramType);
params.getParameters().add(param);
// params.setVararg("...");
funcHeader.setParameters(params);
ParameterType pt = factory.createParameterType();
Type type3 = factory.createType();
NonLeftRecursiveType nonLeftRecursiveType3 = factory
.createNonLeftRecursiveType();
nonLeftRecursiveType3.setType(factory.createVoidType());
type3.setBaseType(nonLeftRecursiveType3);
pt.setType(type3);
funcHeader.setRettype(pt);
funcDef.setHeader(funcHeader);
BasicBlock bb = factory.createBasicBlock();
bb.setName("%entrypoint");
MiddleInstruction mi = factory.createMiddleInstruction();
NamedMiddleInstruction nmi = factory.createNamedMiddleInstruction();
nmi.setName("%a1");
Instruction_alloca alloca = factory.createInstruction_alloca();
Type type2 = factory.createType();
NonLeftRecursiveType nonLeftRecursiveType2 = factory
.createNonLeftRecursiveType();
nonLeftRecursiveType2.setType(factory.createIntType());
type2.setBaseType(nonLeftRecursiveType2);
alloca.setType(type2);
alloca.setOpcode("alloca");
nmi.setInstruction(alloca);
mi.setInstruction(nmi);
bb.getInstructions().add(mi);
TerminatorInstruction ti = factory.createTerminatorInstruction();
Instruction_ret ret = factory.createInstruction_ret();
ret.setOpcode("ret");
ti.setInstruction(ret);
bb.getInstructions().add(ti);
funcDef.getBasicBlocks().add(bb);
model.getElements().add(funcDef);
Injector injector = new LLVM_IRStandaloneSetup()
.createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector
.getInstance(XtextResourceSet.class);
XtextResource resource = (XtextResource) resourceSet.createResource(URI
.createURI("platform:/resource/./dummy.ll"));
resource.getContents().add(model);
//System.out.println(EmfFormatter.listToStr(model.getElements()));
SaveOptions options = SaveOptions.newBuilder().format().getOptions();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
resource.save(outputStream, options.toOptionsMap());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Serialized result: " + outputStream.toString());