2015年11月10日 星期二

Pointers and Arrays Java Native Interface JNI JDB CLI Common Intermediate Language Infrastructure

http://www.gamedev.net/page/resources/_/technical/general-programming/a-tutorial-on-pointers-and-arrays-in-c-r1697#ch7

A Tutorial on Pointers and Arrays in C

By Ted Jensen | Published Feb 16 2002 07:35 PM in General Programming


21 SWIG and Java


Java Programming/Print version


Translating Java Programs into C++

C++ Programming Language

Pointers, References and Dynamic Memory Allocation

Advanced Memory Management: Dynamic Allocation, Part 1

By Andrei Milea

malloc and free, new and delete


Dynamic allocation is one of the three ways of using memory provided by the C/C++ standard. To accomplish this in C the malloc function is used and the new keyword is used for C++. Both of them perform an allocation of a contiguous block of memory, malloc taking the size as parameter:
int *data = new int;    
int *data = (int*) malloc(sizeof(int));         //notice the use of sizeof for portability


https://github.com/davidar/lljvm
https://davidar.io
Low Level Java Virtual Machine 

LLVM 2.7 must be installed to compile and run LLJVM. Jasmin is needed for assembling JVM bytecode.
To compile LLJVM, simply call make in the project root directory, and call make check to run the test suite.

NestedVM

Binary translation for Java

Welcome

NestedVM provides binary translation for Java Bytecode. This is done by having GCC compile to a MIPS binary which is then translated to a Java class file. Hence any application written in C, C++, Fortran, or any other language supported by GCC can be run in 100% pure Java with no source changes.

Google search

http://web.mit.edu/javadev/doc/tutorial/native1.1/implementing/index.html


http://c2.com/cgi/wiki?OtherLanguagesForTheJavaVm

Other Languages For The Java Vm

Despite what some DotNet/CommonLanguageRuntime (CLR) proponents claim, the JavaVirtualMachine supports multiple languages as well. See http://www.robert-tolksdorf.de/vmlanguages.html for the gory details.

http://www.xwt.org/mips2java/

Mips2Java

XWT needs two rather crucial open source (typically Linux-related) libraries which are written in C/C++: libjpeg (for non-JVM and J2ME platforms) and Freetype2. A crucial design decision which has kept XWT so portable is that it must not use any code which cannot compile under jdk1.1, and must not use any classes outside of the java.langjava.textjava.netjava.io, and java.util packages.

https://code.google.com/p/cibyl/wiki/Cibyl

The Kawa Scheme language

Kawa is a general-purpose programming language that runs on the Java platform. It aims to combine:
  • the benefits of dynamic scripting languages (non-verbose code with less boiler-plate, fast and easy start-up, a REPL, no required compilation step); with
  • the benefits of traditional compiled languages (fast execution, static error detection, modularity, zero-overhead Java platform integration).

clang: a C language family frontend for LLVM

The goal of the Clang project is to create a new C, C++, Objective C and Objective C++ front-end for the LLVM compiler. You can get and build the source today.


libgccjit

This document describes libgccjit, an API for embedding GCC inside programs and libraries.
Note that libgccjit is currently of “Alpha” quality; the APIs are not yet set in stone, and they shouldn’t be used in production yet.
There are actually two APIs for the library:
  • a pure C API: libgccjit.h
  • a C++ wrapper API: libgccjit++.h. This is a collection of “thin” wrapper classes around the C API, to save typing.
http://usenix.org/legacy/publications/library/proceedings/coots97/full_papers/muller/muller_html/node3.htmlHarissa is a Java environment that includes a compiler from Java bytecode to C and a Java Virtual Machine integrated in a runtime library. 



The FPC backend for the Java Virtual Machine (JVM) generates Java byte code that conforms to the specifications of the JDK 1.5 (and later), and also to the Dalvik VM from the Android platform. While not all FPC language features work when targeting the JVM, most do (or will in the future) and we have done our best to introduce as few differences as possible.


Debugging Java Code

If your program terminates with a thread dump, and if you are using a just-in-time compiler Java VM (by default, JDK runs as just-in-time compiler VM), then the thread dump will likely contain lots of useless information. To get thread dump information in terms of your Java source code, compile with -g and run the Java virtual machine in all-interpreted mode using the -Djava.compiler=NONE command line argument:
java -Djava.compiler=NONE blah

Compile with -g. This will cause the java compiler to put Java source code line information in .class files:

javac -g blah.java
JDB

Use jdb to set breakpoints, dump thread stacks, and step through the execution. jdb has a dbx-like interface rather than a gdb-like interface. Running jdb and typing help will provide you with information about jdb's commands. Here are some  


List of CLI languages

From Wikipedia, the free encyclopedia


Java Programming Tutorial

Java Native Interface (JNI)


2.1  JNI with C
Step 1: Write a Java Class that uses C Codes - HelloJNI.java 
public class HelloJNI {
   static {
      System.loadLibrary("hello"); // Load native library at runtime
                                   // hello.dll (Windows) or libhello.so (Unixes)
   }

   // Declare a native method sayHello() that receives nothing and returns void
   private native void sayHello();

   // Test Driver
   public static void main(String[] args) {
      new HelloJNI().sayHello();  // invoke the native method
   }
}
The static initializer invokes System.loadLibrary() to load the native library "Hello" (which contains the native method sayHello()) during the class loading. It will be mapped to "hello.dll" in Windows; or "libhello.so" in Unixes. This library shall be included in Java's library path (kept in Java system variable java.library.path); otherwise, the program will throw a UnsatisfiedLinkError. You could include the library into Java Library's path via VM argument -Djava.library.path=path_to_lib.
Next, we declare the method sayHello() as a native instance method, via keyword native, which denotes that this method is implemented in another language. A native method does not contain a body. The sayHello() is contained in the native library loaded.
The main() method allocate an instance of HelloJNI and invoke the native method sayHello().
Compile the "HelloJNI.java" into "HelloJNI.class".
> javac HelloJNI.java
Step 2: Create the C/C++ Header file - HelloJNI.h
Run javah utility on the class file to create a header file for C/C++ programs:
> javah HelloJNI
The output is HelloJNI.h as follows: 
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class HelloJNI */

#ifndef _Included_HelloJNI
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloJNI
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
The header declares a C function Java_HelloJNI_sayHello as follows:
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *, jobject);
The naming convention for C function is Java_{package_and_classname}_{function_name}(JNI arguments). The dot in package name shall be replaced by underscore.
The arguments:
JNIEnv*: reference to JNI environment, which lets you access all the JNI fucntions.
jobject: reference to "this" Java object.
We are not using these arguments in the hello-world example, but will be using them later. Ignore the macros JNIEXPORT and JNICALL for the time being.
The extern "C" is recognized by C++ compiler only. It notifies the C++ compiler that these functions are to be compiled using C's function naming protocol (instead of C++ naming protocol). C and C++ have different function naming protocols as C++ support function overloading and uses a name mangling scheme to differentiate the overloaded functions. Read "Name Mangling".
Step 3: C Implementation - HelloJNI.c

#include
#include
#include "HelloJNI.h"

// Implementation of native method sayHello() of HelloJNI class
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
   printf("Hello World!\n");
   return;
}
Save the C program as "HelloJNI.c".
The header "jni.h" is available under the "\include" and "\include\win32" directories, where is your JDK installed directory (e.g., "c:\program files\java\jdk1.7.0").
The C function simply prints the message "Hello world!" to the console.
Compile the C program - this depends on the C compiler you used.
For MinGW GCC in Windows
> set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_{xx}
      // Define and Set environment variable JAVA_HOME to JDK installed directory
      // I recommend that you set JAVA_HOME permanently, via "Control Panel" ⇒ "System" ⇒ "Environment Variables"
> echo %JAVA_HOME%
      // In Windows, you can refer a environment variable by adding % prefix and suffix 
> gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
      // Compile HellJNI.c into shared library hello.dll
The compiler options used are:
-Wl: The -Wl to pass linker option --add-stdcall-alias to prevent UnsatisfiedLinkError (symbols with a stdcall suffix (@nn) will be exported as-is and also with the suffix stripped). (Some people suggested to use -Wl,--kill-at.)
-I: for specifying the header files directories. In this case "jni.h" (in "\include") and "jni_md.h" (in "\include\win32"), where denotes the JDK installed directory. Enclosed the directory in double quotes if it contains spaces.
-shared: to generate share library.
-o: for setting the output filename "hello.dll".
You can also compile and link in two steps:
// Compile-only with -c flag. Output is HElloJNI.o
> gcc -c -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" HelloJNI.c

// Link into shared library "hello.dll"
> gcc -Wl,--add-stdcall-alias -shared -o hello.dll HelloJNI.o
Try nm (which list all the symbols) on the shared library produced to look for the sayHello() function. Take note the GCC added prefix _ and suffix @8 (the number of bytes of parameters). Check for the function name Java_HelloJNI_sayHello with type "T" (defined).
> nm hello.dll | grep say
624011d8 T _Java_HelloJNI_sayHello@8
For Cygwin GCC in Windows
You need to define the type __int64 as "long long" via option -D _int64="long long".
For gcc-3, include option -mno-cygwin to build DLL files which are not dependent upon the Cygwin DLL.
> gcc-3 -D __int64="long long" -mno-cygwin -Wl,--add-stdcall-alias 
  -I"\include" -I"\include\win32" -shared -o hello.dll HelloJNI.c
For gcc-4: I still cannot find the correct compiler option (-mno-cygwin is not supported). The Java program hangs!
Step 4: Run the Java Program
> java HelloJNI
or
> java -Djava.library.path=. HelloJNI
You may need to specify the library path of the "hello.dll" via VM option -Djava.library.path=, as shown above.



Java programming with JNI

This tutorial describes and demonstrates the basic and most commonly used techniques of the Java Native Interface -- calling C or C++ code from Java programs, and calling Java code from C or C++ programs -- to help you develop your own JNI solutions quickly and efficiently.


Java Native Interface with C tutorial--


Wrapping a C++ library with JNI – introduction


Calling C code from Java using JNI

In this tutorial we'll be creating a Java application calling code from a native library. We'll have a Java application called HelloWorld which will call the function helloFromC from a shared library named ctest, using Java Native Interface.

沒有留言: