Skip to content

Commit 2334533

Browse files
committed
fix array
1 parent 654ab08 commit 2334533

16 files changed

+414
-343
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11

22
# java2llvm
33

4-
An Example Project Show Convert Java Byte Code to LLVM IR assembler , compile standalone executable file
4+
An Example Project Show Convert Java Byte Code to LLVM IR assembler , compile standalone executable file
55

6-
This project is based on [class2ir](https://github.com/MParygin/class2ir), that based on an old llvm version.
7-
So I've changed some instruction syntex, and repaired bug, enhanced functional.
6+
This project is based on [class2ir](https://github.com/MParygin/class2ir), that based on an old llvm version.
7+
So I've changed some instruction syntex, and repaired bug, enhanced functional.
88

99
### Currently:
10-
Generated CentOS x64 executable file, and say "Hello world".
10+
Generated CentOS x64 executable file, and say "Hello world".
1111

1212
### Make:
13-
1. Enter directory java2llvm/
14-
2. Run build.sh, then you will get app.exe here.
13+
1. Enter directory java2llvm/
14+
2. Run build.sh, then you will get app.exe here.
1515

16-
## Request:
17-
CentOS 7.0 x86_64
18-
java 1.8
19-
llvm-as / llc / clang 5.0
20-
make
16+
### Request:
17+
CentOS 7.0 x86_64
18+
java 1.8
19+
llvm-as / llc / clang 5.0
20+
make
2121

2222
### Known issue:
23-
1. No GC.
24-
2. Maybe some of java instruction can't work, need test
25-
3. some of java instruction not implementation , see MV.java
26-
4. Object memory allocation
23+
* No GC.
24+
* Maybe some of java instruction can't work, need test
25+
* some of java instruction not implementation , see MV.java
26+
* Object memory allocation
2727

2828
### change log:
29-
1. Add base class java.lang.*, java.io.PrintStream
30-
2. Add String to handle text output, StringBuilder to handle string concat
31-
3. Trace instruction flow , to fix register var scope bug.
29+
* Add base class java.lang.*, java.io.PrintStream
30+
* Add String to handle text output, StringBuilder to handle string concat
31+
* Trace instruction flow , to fix register var scope bug.
3232

3333

3434

app/c/runner.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ void classes_clinit();
99

1010
void test_Test_main();
1111

12-
int main() {
13-
classes_clinit();
14-
test_Test_main();
15-
}
16-
1712
void print_debug(int v) {
1813
printf("lldebug: %d \n", v);
1914
}
@@ -22,6 +17,27 @@ void print_ptr(long long v) {
2217
printf("ll_ptr: %llx (%lld)\n", v, v);
2318
}
2419

20+
typedef union _SlotEntry{
21+
char ch;
22+
short s16;
23+
unsigned short u16;
24+
int s32;
25+
unsigned int u32;
26+
long long s64;
27+
unsigned long long u64;
28+
float f32;
29+
double f64;
30+
void *_ptr;
31+
}SlotEntry;
32+
33+
int main() {
34+
SlotEntry slot;
35+
slot.s32=40;
36+
print_ptr(slot.s64);
37+
classes_clinit();
38+
test_Test_main();
39+
}
40+
2541
/*
2642
inline s32 stack_size(RuntimeStack *stack) {
2743
return (stack->sp - stack->store);

class2ir/dist/class2ir.jar

1.4 KB
Binary file not shown.

class2ir/src/main/java/j2ll/AssistLLVM.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
public class AssistLLVM {
1010

11-
static String declares = "declare noalias i8* @malloc(i32)\n" +
11+
static final String declares = "declare noalias i8* @malloc(i32)\n" +
1212
"declare void @free(i8*)\n" +
1313
"declare void @print_debug(i32)\n" +
1414
"declare void @print_ptr(i64)\n" +
1515
"declare i32 @ptr_size()\n";
1616

17+
static final String CHAR_ARR = "[C";
1718

1819
static Resolver resolver = new Resolver();
1920
static List<String> clinitMethods = new ArrayList<>();
@@ -26,13 +27,15 @@ static public void genClinits(String outpath) {
2627

2728
//declare
2829
ps.println(getAssistFuncDeclare());
29-
ps.println("declare void @java_lang_String__init___C(%java_lang_String*, {i32, [0 x i16]}*)");
30+
String carrIRtype = Util.javaSignature2irType(resolver, CHAR_ARR);
31+
ps.println("declare void @java_lang_String__init___C(%java_lang_String* %s0, " + carrIRtype + " %s1)");
3032
for (String s : clinitMethods) {
3133
if (s != null) ps.println("declare " + s);
3234
}
3335
ps.println("\n");
3436

3537
ps.println(Util.class2struct(resolver, "java/lang/String"));
38+
ps.println(Util.class2struct(resolver, CHAR_ARR));
3639
ps.println("\n");
3740

3841
//call
@@ -45,7 +48,6 @@ static public void genClinits(String outpath) {
4548
ps.println("\n");
4649

4750

48-
String carrIRtype = Util.javaSignature2irType(resolver, "[C \n");
4951
String funcName = getConstStringFuncName();
5052
String extFunc = "define " + funcName + " {\n"
5153
+ " %tmps0 = alloca " + carrIRtype + "\n"
@@ -58,7 +60,7 @@ static public void genClinits(String outpath) {
5860
+ " %__tmp0 = call i8* @malloc(i32 %__memsize)\n"
5961
+ " %stack1 = bitcast i8* %__tmp0 to %java_lang_String*\n"
6062
+ " ; \n"
61-
+ " call void @java_lang_String__init___C(%java_lang_String* %stack1, {i32, [0 x i16]}* %stack0) \n"
63+
+ " call void @java_lang_String__init___C(%java_lang_String* %stack1, " + carrIRtype + " %stack0) \n"
6264
+ " ret %java_lang_String* %stack1\n"
6365
+ "} \n";
6466
ps.println(extFunc);
@@ -78,7 +80,7 @@ static public void addClinit(String s) {
7880

7981
static public String getConstStringFuncName() {
8082
String ty = Util.javaSignature2irType(resolver, "Ljava/lang/String;"); //for add class java.lang.String declare
81-
String carrIRtype = Util.javaSignature2irType(resolver, "[C \n");
83+
String carrIRtype = Util.javaSignature2irType(resolver, CHAR_ARR);
8284
String funcName = ty + " @construct_string_with_char_arr_(" + carrIRtype + " %s0)";
8385
return funcName;
8486
}

0 commit comments

Comments
 (0)