Examples

This is a simple C++ program, for which we have provided a custom implementation of a function. Full code is provided here

Produce the ll file

Below is the C++ program that we are going to use(src ll file)
#include <stdio.h> #include <stdlib.h> #include <string.h> void usage() { puts("Usage : message [-dialog] \"title\" \"message\""); exit(-1); }; void dialogBox(char* title , char* message) { puts("Dialog:"); puts(title); puts(message); }; void console(char* title , char* message) { puts(title); puts(message); }; int main (int argc,char **argv) { int base =0; int dialog=0; char *title; char *message; switch (argc) { case 3 : if (strcmp("-dialog",argv[1])) { title =argv[1]; message=argv[2] ; console(title,message); } else { usage(); } break; case 4 : if (!strcmp("-dialog",argv[1])) { title =argv[2]; message=argv[3]; dialogBox(title,message); } else { usage(); } break; default : usage(); break; } return 0; }
Using the below command line the ll file was generated
llvm-g++ message.cc -S --emit-llvm -o message.ll

Compile with Proteus

Now using this ll file generate an executable jar. But we are going to provide an alternate implementation of dialogBox. First, we generated the source/classes/jar (note -force-name true is a useful option here). Then, we found the function we wanted to substitute (_Z9dialogBoxPcS_.java) in the generated folder. This is copied to source/org/proteus and and the static call method modified to modified (see below) to provide our alternate implementation.

/** Custom version of the DialogBox Function */ package org.proteus; import com.emt.proteus.lib.api.*; import com.emt.proteus.runtime.api.*; import javax.swing.*; public final class _Z9dialogBoxPcS_ extends ImplementedFunction{ public _Z9dialogBoxPcS_() { super("_Z9dialogBoxPcS_",2); } public int execute(int $title, int $message){ call($title, $message); return 0; } public int execute(Env env, Frame caller, int return_addr, int return_width, int argc, int[] code, int pc) { int $title = caller.getI32(code[pc]); pc=pc+2; argc--; int $message = caller.getI32(code[pc]); pc=pc+2; argc--; call($title, $message); return pc; } public static void call(int $title, int $message){ JOptionPane.showMessageDialog(null,MainMemory.getString($message),MainMemory.getString($title),JOptionPane.PLAIN_MESSAGE); } //End of Function }
Now to combine our hand-coded class with the generated code we do the following (remember that we copied the java file to source/org/proteus folder).
java -jar Proteus.jar -source-path source message.ll

Run the generated java executable

The below will output the usage message
java -jar message.jar
The below will output to the console 'Title' and 'Message' (Note '--' to direct parameters to the generated code).
java -jar message.jar -- Title Message
The below will show a java JOptionPane with 'Title' and 'Message' (Note '--' to direct parameters to the generated code).
java -jar message.jar -- -dialog Title Message