Skip to content

A compiler that converts C language to Java bytecode or can directly interpret execution

Notifications You must be signed in to change notification settings

bitzhuwei/C2j-Complier

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C2j-Complier

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

Supported

  • Support for all basic statements

  • Interpreter:supports pointers, arrays, structs, and struct arrays

  • Complier: Pointer not supported

How to use

1. Go to releases and download the jar

2. Go to the command line to start

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

An example

The source 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]);
    }
}

Interpreted

The first run generates lrStateTable.sb under the folder, which is the parse table

Complier

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

doc

Waiting for the update

About

A compiler that converts C language to Java bytecode or can directly interpret execution

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 98.5%
  • Jasmin 1.2%
  • C 0.3%