A compiler that converts C language to Java bytecode or can directly interpret execution
- As a learning project
- Can be interpreted to execute most C or compiled into Java bytecode
- Toy level, with many features not added, and no optimization
-
Support for all basic statements
-
Interpreter:supports pointers, arrays, structs, and struct arrays
-
Complier: Pointer not supported
java -jar C2j-Complier.jar -m interpreter -f test.c
java -jar C2j-Complier.jar -m codegen -d true -f test.c
parameter | detailed | ||
---|---|---|---|
-m | codegen | interpreter | Start mode, default to interpreter |
-d | true | false | Whether debug information is turned on, the default is false (recommended not to turn on) |
-f | Specifies the path to run the file |
void swap(int arr[10], int i, int j) {
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void quickSort(int a[10], int p, int r) {
int x;
int i;
i = p - 1;
int j;
int t;
int v;
v = r - 1;
if (p < r) {
x = a[r];
for (j = p; j <= v; j++) {
if (a[j] <= x) {
i++;
swap(a, i, j);
}
}
v = i + 1;
swap(a, v, r);
t = v - 1;
quickSort(a, p, t);
t = v + 1;
quickSort(a, t, r);
}
}
void main () {
int a[10];
int i;
int t;
printf("Array before quicksort:");
for(i = 0; i < 10; i++) {
t = (10 - i);
a[i] = t;
printf("value of a[%d] is %d", i, a[i]);
}
quickSort(a, 0, 9);
printf("Array after quicksort:");
for (i = 0; i < 10; i++) {
printf("value of a[%d] is %d", i, a[i]);
}
}
The first run generates lrStateTable.sb under the folder, which is the parse table
java -jar C2j-Complier.jar -m codegen -f test.c
Generate C2Bytecode.j under the folder,Use a third-party bytecode compiler to generate class files
Part of the generated bytecode:
.class public C2Bytecode
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
sipush 10
newarray int
astore 0
sipush 0
istore 1
sipush 0
istore 2
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Array before quicksort:"
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "
"
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
sipush 0
istore 1
loop0:
iload 1
sipush 10
if_icmpge branch0
sipush 10
iload 1
isub
istore 2
aload 0
iload 1
iload 2
iastore
aload 0
iload 1
iaload
istore 3
iload 1
istore 4
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "value of a["
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
getstatic java/lang/System/out Ljava/io/PrintStream;
iload 4
invokevirtual java/io/PrintStream/print(I)V
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "] is "
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
getstatic java/lang/System/out Ljava/io/PrintStream;
iload 3
invokevirtual java/io/PrintStream/print(I)V
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "
"
invokevirtual java/io/PrintStream/print(Ljava/lang/String;)V
iload 1
sipush 1
iadd
istore 1
goto loop0
Code generation does not support Pointers
void quicksort(int A[10], int p, int r) {
int x;
int i;
i = p - 1;
int j;
int t;
int v;
v = r - 1;
if (p < r) {
x = A[r];
for (j = p; j <= v; j++) {
if (A[j] <= x) {
i++;
t = A[i];
A[i] = A[j];
A[j] = t;
}
}
v = i + 1;
t = A[v];
A[v] = A[r];
A[r] = t;
t = v - 1;
quicksort(A, p, t);
t = v + 1;
quicksort(A, t, r);
}
}
void main () {
int a[10];
int i;
int t;
printf("before quick sort:");
for(i = 0; i < 10; i++) {
t = (10 - i);
a[i] = t;
printf("value of a[%d] is %d", i, a[i]);
}
quicksort(a, 0, 9);
printf("after quick sort:");
for (i = 0; i < 10; i++) {
printf("value of a[%d] is %d", i, a[i]);
}
}
void main() {
struct s1 {
int a1;
};
struct s2 {
struct s1 s;
int a2;
};
struct s2 tag;
tag.s.a1 = 1;
tag.a2 = 2;
printf("set filed a1 of struct s1 to value : %d, and a2 of tag to value : %d", tag.s.a1, tag.a2);
}
void main() {
char *p;
p = malloc(2);
*(p+0) = 1;
*(p+1) = 2;
printf("p[0] is : %d, p[1] is : %d", *(p+0), *(p+1));
}
void main() {
struct TAG {
char c;
char b[3];
int p;
} tag;
struct TAG* pTag;
int sz;
sz = sizeof(tag);
pTag = malloc(sz + 8);
pTag[0] = 5;
pTag[1] = 10;
pTag[2] = 12;
printf("c is %d, b[0] is %d, b[2] is %d ", pTag->c, pTag->b[0], pTag->b[1]);
}
struct Body {
int a;
char c;
};
void main() {
struct Body body;
body.a = 66;
printf("The value of a of this structure is %d", body.a);
}
struct Body {
int a;
char c;
};
void main() {
struct Body bodys[5];
bodys[2].a = 66;
printf("The value of a of this structure is %d", bodys[2].a);
}