Skip to content

Commit 654ab08

Browse files
committed
Hello world
1 parent eb4e572 commit 654ab08

File tree

17 files changed

+313
-227
lines changed

17 files changed

+313
-227
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ An Example Project Show Convert Java Byte Code to LLVM IR assembler , compile st
66
This project is based on [class2ir](https://github.com/MParygin/class2ir), that based on an old llvm version.
77
So I've changed some instruction syntex, and repaired bug, enhanced functional.
88

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

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

@@ -19,13 +19,13 @@ java 1.8
1919
llvm-as / llc / clang 5.0
2020
make
2121

22-
## Known issue:
22+
### Known issue:
2323
1. No GC.
2424
2. Maybe some of java instruction can't work, need test
2525
3. some of java instruction not implementation , see MV.java
2626
4. Object memory allocation
2727

28-
## change log:
28+
### change log:
2929
1. Add base class java.lang.*, java.io.PrintStream
3030
2. Add String to handle text output, StringBuilder to handle string concat
3131
3. Trace instruction flow , to fix register var scope bug.

app.exe

21.7 KB
Binary file not shown.

app/c/jni_func.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,34 @@
55
#include <time.h>
66
#include <unistd.h>
77

8+
int ptr_size() {
9+
return sizeof(int *);
10+
}
11+
812
long long java_lang_System_currentTimeMillis() {
9-
struct timeval tv;
10-
gettimeofday(&tv, NULL);
11-
long long v=tv.tv_sec * 1000 + tv.tv_usec / 1000;
12-
return v;
13+
struct timeval tv;
14+
gettimeofday(&tv, NULL);
15+
long long v = tv.tv_sec * 1000 + tv.tv_usec / 1000;
16+
return v;
1317
}
1418

1519
long long java_lang_System_nanoTime() {
16-
struct timeval tv;
17-
gettimeofday(&tv, NULL);
18-
long long v=tv.tv_sec * 1000000 + tv.tv_usec;
19-
v*=1000;
20-
return v;
20+
struct timeval tv;
21+
gettimeofday(&tv, NULL);
22+
long long v = tv.tv_sec * 1000000 + tv.tv_usec;
23+
v *= 1000;
24+
return v;
2125
}
2226

2327

24-
void java_io_PrintStream_println_I(int value) {
25-
printf("%d\n", value);
26-
}
28+
void java_io_PrintStream_println_I(void *ps, int value) {
29+
printf("%d\n", value);
30+
}
2731

28-
void java_io_PrintStream_println_J(long long value) {
29-
printf("%lld\n", value);
30-
}
32+
void java_io_PrintStream_println_J(void *ps, long long value) {
33+
printf("%lld\n", value);
34+
}
3135

32-
void java_io_PrintStream_print_C(short value) {
33-
printf("%c\n", (char)value);
34-
printf("%d\n", (int)value);
35-
}
36+
void java_io_PrintStream_print_C(void *ps, unsigned short value) {
37+
printf("%c", (char) value);
38+
}

app/c/runner.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,41 @@
55

66
#include "runner.h"
77

8-
void test_Test__clinit_();
8+
void classes_clinit();
99

1010
void test_Test_main();
1111

1212
int main() {
13-
test_Test__clinit_();
13+
classes_clinit();
1414
test_Test_main();
1515
}
1616

17+
void print_debug(int v) {
18+
printf("lldebug: %d \n", v);
19+
}
20+
21+
void print_ptr(long long v) {
22+
printf("ll_ptr: %llx (%lld)\n", v, v);
23+
}
1724

25+
/*
1826
inline s32 stack_size(RuntimeStack *stack) {
1927
return (stack->sp - stack->store);
2028
}
2129
22-
/* push Integer */
30+
2331
inline void push_int(RuntimeStack *stack, s32 value) {
2432
stack->sp->ivalue = value;//clear 64bit
2533
stack->sp->type = STACK_ENTRY_INT;
2634
stack->sp++;
2735
}
2836
29-
30-
/* pop Integer */
3137
inline s32 pop_int(RuntimeStack *stack) {
3238
stack->sp--;
3339
return stack->sp->ivalue;
3440
}
3541
36-
/* push Double */
42+
3743
inline void push_double(RuntimeStack *stack, f64 value) {
3844
stack->sp->dvalue = value;
3945
stack->sp->type = STACK_ENTRY_DOUBLE;
@@ -43,28 +49,24 @@ int main() {
4349
stack->sp++;
4450
}
4551
46-
/* pop Double */
4752
inline f64 pop_double(RuntimeStack *stack) {
4853
stack->sp -= 2;
4954
return stack->sp->dvalue;
5055
}
5156
52-
/* push Float */
5357
inline void push_float(RuntimeStack *stack, f32 value) {
5458
//ptr->lvalue = 0;//clear 64bit
5559
stack->sp->fvalue = value;
5660
stack->sp->type = STACK_ENTRY_FLOAT;
5761
stack->sp++;
5862
}
5963
60-
/* pop Float */
6164
inline f32 pop_float(RuntimeStack *stack) {
6265
stack->sp--;
6366
return stack->sp->fvalue;
6467
}
6568
6669
67-
/* push Long */
6870
inline void push_long(RuntimeStack *stack, s64 value) {
6971
stack->sp->lvalue = value;
7072
stack->sp->type = STACK_ENTRY_LONG;
@@ -74,13 +76,11 @@ int main() {
7476
stack->sp++;
7577
}
7678
77-
/* pop Long */
7879
inline s64 pop_long(RuntimeStack *stack) {
7980
stack->sp -= 2;
8081
return stack->sp->lvalue;
8182
}
8283
83-
/* push Ref */
8484
inline void push_ref(RuntimeStack *stack, __refer value) {
8585
stack->sp->type = STACK_ENTRY_REF;
8686
stack->sp->rvalue = value;
@@ -99,7 +99,6 @@ int main() {
9999
stack->sp++;
100100
}
101101
102-
/* Pop Stack Entry */
103102
inline void pop_entry(RuntimeStack *stack, StackEntry *entry) {
104103
stack->sp--;
105104
entry->lvalue = stack->sp->lvalue;
@@ -118,7 +117,6 @@ int main() {
118117
}
119118
120119
121-
/* Entry to Int */
122120
inline s32 entry_2_int(StackEntry *entry) {
123121
return entry->ivalue;
124122
}
@@ -130,3 +128,4 @@ int main() {
130128
inline __refer entry_2_refer(StackEntry *entry) {
131129
return entry->rvalue;
132130
}
131+
*/

app/c/runner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef RUNNER_H
22
#define RUNNER_H
33

4-
4+
/*
55
#define STACK_ENTRY_NONE 0
66
#define STACK_ENTRY_INT 1
77
#define STACK_ENTRY_FLOAT 2
@@ -74,5 +74,5 @@ s64 entry_2_long(StackEntry *entry);
7474
7575
__refer entry_2_refer(StackEntry *entry);
7676
77-
77+
*/
7878
#endif //RUNNER_H

app/java/java/lang/StringBuilder.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package java.lang;
22

3-
class StringBuilder {
3+
public class StringBuilder {
44

55
char[] value = new char[16];
66
int count = 0;
@@ -25,13 +25,19 @@ public StringBuilder append(String s) {
2525

2626
public StringBuilder append(long i) {
2727
String s = "";
28-
while (i != 0) {
29-
int v = (int) (i % 10);
30-
s = chars[v] + s;
31-
i /= 10;
32-
}
33-
if (i < 0) {
34-
s = '-' + s;
28+
if(i == 0){
29+
s = "0";
30+
}else{
31+
boolean ne = i < 0;
32+
if (ne) i = -i;
33+
while (i != 0) {
34+
int v = (int) (i % 10);
35+
s = chars[v] + s;
36+
i /= 10;
37+
}
38+
if (ne) {
39+
s = '-' + s;
40+
}
3541
}
3642
return append(s);
3743
}

app/java/java/lang/System.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public class System {
1010
static public native long nanoTime();
1111

1212
static public void arraycopy(char[] src, int srcPos, char[] dest, int destPos, int arr_length) {
13+
if(src==null||dest==null){
14+
return;
15+
}
1316
char[] a = src;
1417
char[] b = dest;
15-
if (a.length > srcPos + arr_length && b.length > destPos + arr_length) {
18+
if (a.length >= srcPos + arr_length && b.length >= destPos + arr_length) {
1619
for (int i = 0; i < arr_length; i++) {
1720
b[destPos + i] = a[srcPos + i];
1821
}

app/java/test/Test.java

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public class Test extends TestParent {
1515
static Test singleton = new Test();
1616

1717
public Test() {
18-
this.bl = true;
19-
this.bt = 127;
20-
this.sh = 100;
21-
this.in = 142;
22-
this.ln = 42L;
23-
this.ft = 123.33f;
24-
this.db = 0.53;
25-
26-
System.out.println(this.in);
18+
// this.bl = true;
19+
// this.bt = 127;
20+
// this.sh = 100;
21+
// this.in = 142;
22+
// this.ln = 42L;
23+
// this.ft = 123.33f;
24+
// this.db = 0.53;
25+
//
26+
// System.out.println(this.in);
2727
}
2828

2929
@Override
@@ -33,68 +33,47 @@ public int length() {
3333

3434
public static void main() {
3535
long a = System.currentTimeMillis();
36-
System.out.println(a);
36+
System.out.println("begin: " + a);
3737
System.out.println("Hello world");
3838

3939
Test test = new Test();
4040
test.in = 9;
4141
System.out.println(test.in);
42-
43-
/*
44-
double v[] = new double[10000];
45-
int ii[] = new int[123];
46-
for (int i = 0; i < 10000; i++) v[i] = i * 1.2d;
47-
48-
switch (test.in) {
49-
case 7:
50-
case 9:
51-
case 10: System.out.println(10);
52-
break;
53-
case 11: System.out.println(7);
54-
break;
55-
//case 16:
56-
case 18: System.out.println(5);
57-
break;
58-
default:
59-
System.out.println(5888);
60-
}
61-
*/
62-
63-
64-
System.out.println(System.currentTimeMillis() - a);
65-
6642
System.out.println(sl);
67-
// System.out.println(ii[6]);
6843
System.out.println(singleton.ln);
6944

45+
long b = System.currentTimeMillis() ;
46+
System.out.println("end: " + b);
47+
System.out.println("cost : " + (b - a));
7048

71-
//double d[][] = new double[101][201];
72-
}
73-
74-
public void t1() {
75-
int a, b, c, d;
76-
a = 3;
77-
b = 4;
78-
c = a * 2 + b * b;
79-
d = 0;
8049

81-
if (c > 0) {
82-
a = b > 3 ? 2 : 1;
83-
} else {
84-
if (b > 3) {
85-
while (d < a) {
86-
if(d==a-1){
87-
c++;
88-
}
89-
c++;
90-
d++;
91-
}
92-
a = 100000;
93-
} else {
94-
a = 200000;
95-
}
96-
}
97-
d = a + c;
9850

9951
}
52+
53+
// public void t1() {
54+
// int a, b, c, d;
55+
// a = 3;
56+
// b = 4;
57+
// c = a * 2 + b * b;
58+
// d = 0;
59+
//
60+
// if (c > 0) {
61+
// a = b > 3 ? 2 : 1;
62+
// } else {
63+
// if (b > 3) {
64+
// while (d < a) {
65+
// if(d==a-1){
66+
// c++;
67+
// }
68+
// c++;
69+
// d++;
70+
// }
71+
// a = 100000;
72+
// } else {
73+
// a = 200000;
74+
// }
75+
// }
76+
// d = a + c;
77+
//
78+
// }
10079
}

class2ir/dist/class2ir.jar

1.82 KB
Binary file not shown.

0 commit comments

Comments
 (0)