Giter Site home page Giter Site logo

c2j-complier's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.