Merge branch 'master' of github.com:nurv/JSJVM

This commit is contained in:
Artur Ventura 2011-07-20 18:25:04 +01:00
commit b4d67b71a0
15 changed files with 155 additions and 97 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
*~
*#
*.py[co]
build

View File

@ -1,14 +1,23 @@
SRCS=$(wildcard src/*.js)
OBJS=$(SRCS:.js=.jspp)
all: ids
ids:
cd webserver && python app.py
preprocess:
%.jspp: %.js
cpp -P -undef -CC -Wundef -std=c99 -nostdinc -Wtrigraphs -fdollars-in-identifiers $< `echo $@ | sed s/src/build/`
builddir:
mkdir -p build
/usr/bin/cpp -P -undef -CC -Wundef -std=c99 -nostdinc -Wtrigraphs -fdollars-in-identifiers src/main.js build/pp_jvm.js
preprocess: builddir $(OBJS)
compile: preprocess
java -jar lib/compiler.jar --warning_level VERBOSE --compilation_level ADVANCED_OPTIMIZATIONS --js build/pp_jvm.js --js_output_file build/jvm.js
java -jar lib/compiler.jar --warning_level VERBOSE --compilation_level ADVANCED_OPTIMIZATIONS `find build/ -name *.jspp -exec echo --js {} \;` --js_output_file build/jvm.js
clean:
rm -dR build

View File

@ -8,6 +8,8 @@
* Author: Artur Ventura
*
*/
#include "constantPool.jsh"
/** @constructor */
var ExceptionTableEntry = function(dStream, constantPool){
@ -191,4 +193,4 @@ var Attribute = function(dStream, constantPool){
result.attribute_length = dStream.getU4();
result.read(dStream, constantPool);
return result;
}
}

View File

@ -9,19 +9,8 @@
*
*/
#define CLASS_MAGIC 0xCAFEBABE
#define ACC_PUBLIC 0x0001 // Declared public; may be accessed from outside its package.
#define ACC_PRIVATE 0x0002 // Declared private; usable only within the defining class.
#define ACC_PROTECTED 0x0004 // Declared protected; may be accessed within subclasses.
#define ACC_STATIC 0x0008 // Declared static.
#define ACC_FINAL 0x0010 // Declared final; no subclasses allowed.
#define ACC_SUPER 0x0020 // Treat superclass methods specially when invoked by the invokespecial instruction.
#define ACC_VOLATILE 0x0040 // Declared volatile; cannot be cached.
#define ACC_NATIVE 0x0100 // Declared native; implemented in a language other than Java.
#define ACC_INTERFACE 0x0200 // Is an interface, not a class.
#define ACC_ABSTRACT 0x0400 // Declared abstract; may not be instantiated.
#define ACC_TRANSIENT 0x0080 // Declared transient; not written or read by a persistent object manager.
#include "constantPool.jsh"
#include "class.jsh"
function slurpFile (filename, fa) {
var xmlHttpRequest, response, result ;

25
src/class.jsh Normal file
View File

@ -0,0 +1,25 @@
/* -*- Mode: Javascript -*-
* -*- coding: UTF-8 -*-
* Copyright (C) 2011 by Artur Ventura
*
* File: class.jsh
*
* Author: Artur Ventura
*
*/
#define CLASS_MAGIC 0xCAFEBABE
#define ACC_PUBLIC 0x0001 // Declared public; may be accessed from outside its package.
#define ACC_PRIVATE 0x0002 // Declared private; usable only within the defining class.
#define ACC_PROTECTED 0x0004 // Declared protected; may be accessed within subclasses.
#define ACC_STATIC 0x0008 // Declared static.
#define ACC_FINAL 0x0010 // Declared final; no subclasses allowed.
#define ACC_SUPER 0x0020 // Treat superclass methods specially when invoked by the invokespecial instruction.
#define ACC_VOLATILE 0x0040 // Declared volatile; cannot be cached.
#define ACC_NATIVE 0x0100 // Declared native; implemented in a language other than Java.
#define ACC_INTERFACE 0x0200 // Is an interface, not a class.
#define ACC_ABSTRACT 0x0400 // Declared abstract; may not be instantiated.
#define ACC_TRANSIENT 0x0080 // Declared transient; not written or read by a persistent object manager.

View File

@ -9,20 +9,7 @@
*
*/
#define CONSTANT_Utf8 1
//what is CONSTANT_????? 2
#define CONSTANT_Integer 3
#define CONSTANT_Float 4
#define CONSTANT_Long 5
#define CONSTANT_Double 6
#define CONSTANT_Class 7
#define CONSTANT_String 8
#define CONSTANT_Fieldref 9
#define CONSTANT_Methodref 10
#define CONSTANT_InterfaceMethodref 11
#define CONSTANT_NameAndType 12
// constant pool members
#include "constantPool.jsh"
/** @constructor */
var constUtf8 = function(){

22
src/constantPool.jsh Normal file
View File

@ -0,0 +1,22 @@
/* -*- Mode: Javascript -*-
* -*- coding: UTF-8 -*-
* Copyright (C) 2011 by Artur Ventura
*
* File: constantPool.jsh
*
* Author: Artur Ventura
*
*/
#define CONSTANT_Utf8 1
//what is CONSTANT_????? 2
#define CONSTANT_Integer 3
#define CONSTANT_Float 4
#define CONSTANT_Long 5
#define CONSTANT_Double 6
#define CONSTANT_Class 7
#define CONSTANT_String 8
#define CONSTANT_Fieldref 9
#define CONSTANT_Methodref 10
#define CONSTANT_InterfaceMethodref 11
#define CONSTANT_NameAndType 12

View File

@ -9,40 +9,11 @@
*
*/
#import "opcodes.js"
#import "types.js"
#define JVM_THROWS_NEW(exception) throw "VER: throws new exception"
#ifdef DEBUG_INTRP
#define LOG_INTRP(x) LOG(x)
#define DEFALIAS(opx) case opx: if(temp!=null) { temp = pc + ": opx" }
#include "constantPool.jsh"
#include "opcodes.jsh"
#include "types.jsh"
#include "cpu.jsh"
#else
#define LOG_INTRP(x)
#define DEFALIAS(opx) case opx:
#endif
#define PANIC(msg) throw new JVMPanic(msg)
#define DEFOP(opx) case opx: LOG_INTRP(pc + ": opx");
#define DEFNOP() LOG_INTRP(temp)
#define ENDDEF break;
#define OPPOP() operand_stack.pop()
#define OPPUSH(v) operand_stack.push(v)
#define OPPOPD() operand_stack.pop() && operand_stack.pop()
#define OPPUSHD(v) operand_stack.push(v) && operand_stack.push(null)
#define OPSTACK(v) operand_stack[v]
#define OPSTACK_LENGTH() operand_stack.length
#define LOCAL_VAR(v) local_variables[v]
#define OPCODE opcode
#define PC pc
#define STACK_MOVE(y,x) OPSTACK(OPSTACK_LENGTH()-y) = OPSTACK(OPSTACK_LENGTH()-x)
#define READ_NEXT() code[++pc]
#define canonicalName(ref) ref.str.replace(/\//g,".")
/** @constructor */
var JVM = function(params,args){
this.nativeMappingTable = {}
@ -128,6 +99,6 @@ function interpret(frame){
var temp = null;
#endif
switch(OPCODE){
#include "intrp.js"
#include "intrp.def"
}
}
}

56
src/cpu.jsh Normal file
View File

@ -0,0 +1,56 @@
/* -*- Mode: Javascript -*-
* -*- coding: UTF-8 -*-
* Copyright (C) 2011 by Artur Ventura
*
* File: cpu.jsh
*
* Author: Artur Ventura
*
*/
#ifndef _CPU_JSH_
#define _CPU_JSH_
#include "log.jsh"
#define JVM_THROWS_NEW(exception) throw "VER: throws new exception"
#ifdef DEBUG_INTRP
#define LOG_INTRP(x) LOG(x)
#define DEFALIAS(opx) case opx: if(temp!=null) { temp = pc + ": opx" }
#else
#define LOG_INTRP(x)
#define DEFALIAS(opx) case opx:
#endif
#define PANIC(msg) throw new JVMPanic(msg)
#define DEFOP(opx) case opx: LOG_INTRP(pc + ": opx");
#define DEFNOP() LOG_INTRP(temp)
#define ENDDEF break;
#define OPPOP() operand_stack.pop()
#define OPPUSH(v) operand_stack.push(v)
#define OPPOPD() operand_stack.pop() && operand_stack.pop()
#define OPPUSHD(v) operand_stack.push(v) && operand_stack.push(null)
#define OPSTACK(v) operand_stack[v]
#define OPSTACK_LENGTH() operand_stack.length
#define LOCAL_VAR(v) local_variables[v]
#define OPCODE opcode
#define PC pc
#define STACK_MOVE(y,x) OPSTACK(OPSTACK_LENGTH()-y) = OPSTACK(OPSTACK_LENGTH()-x)
#define READ_NEXT() code[++pc]
#define CHECK_NULL(ref) if (ref == NULL){ JVM_THROWS_NEW(java.lang.NullPointerException); }
#define CHECK_ARRAY_INDEX(ind,ref) if (ind >= ref.length){ JVM_THROWS_NEW(java.lang.ArrayIndexOutOfBoundsException); }
#define TO_INT(value) if (isNaN(value)){ OPPUSH(0); }else if(IS_OVERFLOW(value,INT_MAX_VALUE)){ OPPUSH(INT_MAX_VALUE); }else if(IS_UNDERFLOW(value,INT_MIN_VALUE)){ OPPUSH(INT_MIN_VALUE); }else{ OPPUSH(Math.round(value)); }
#define TO_FLOAT(value) if (isNaN(value)){ OPPUSHD(NaN); }else if(IS_OVERFLOW(value,FLOAT_MAX_VALUE)){ OPPUSHD(POSITIVE_INF); }else if (IS_UNDERFLOW(value,FLOAT_MIN_VALUE)){ OPPUSHD(NEGATIVE_INF); }else{ OPPUSHD(value);}
#define TO_LONG(value) if (isNaN(value)){ OPPUSH(0); }else if(IS_OVERFLOW(value,LONG_MAX_VALUE)){ OPPUSH(LONG_MAX_VALUE); }else if(IS_UNDERFLOW(value,LONG_MIN_VALUE)){ OPPUSH(LONG_MIN_VALUE); }else{ OPPUSH(Math.round(value));}
function canonicalName(ref) {return ref.str.replace(/\//g,".");}
#endif //_CPU_JSH_

View File

@ -9,6 +9,8 @@
*
*/
#include "constantPool.jsh"
/** @constructor */
var FieldInfo = function(dStream,constantPool){
this.access_flags = dStream.getU2();
@ -33,4 +35,4 @@ var MethodInfo = function(dStream, constantPool){
for (var i=0; i<this.attributes_count; i++){
this.attributes[i] = Attribute(dStream,constantPool);
}
}
}

View File

@ -9,14 +9,6 @@
*
*/
#include "opcodes.js"
#define CHECK_NULL(ref) if (ref == NULL){ JVM_THROWS_NEW(java.lang.NullPointerException); }
#define CHECK_ARRAY_INDEX(ind,ref) if (ind >= ref.length){ JVM_THROWS_NEW(java.lang.ArrayIndexOutOfBoundsException); }
#define TO_INT(value) if (isNaN(value)){ OPPUSH(0); }else if(IS_OVERFLOW(value,INT_MAX_VALUE)){ OPPUSH(INT_MAX_VALUE); }else if(IS_UNDERFLOW(value,INT_MIN_VALUE)){ OPPUSH(INT_MIN_VALUE); }else{ OPPUSH(Math.round(value)); }
#define TO_FLOAT(value) if (isNaN(value)){ OPPUSHD(NaN); }else if(IS_OVERFLOW(value,FLOAT_MAX_VALUE)){ OPPUSHD(POSITIVE_INF); }else if (IS_UNDERFLOW(value,FLOAT_MIN_VALUE)){ OPPUSHD(NEGATIVE_INF); }else{ OPPUSHD(value);}
#define TO_LONG(value) if (isNaN(value)){ OPPUSH(0); }else if(IS_OVERFLOW(value,LONG_MAX_VALUE)){ OPPUSH(LONG_MAX_VALUE); }else if(IS_UNDERFLOW(value,LONG_MIN_VALUE)){ OPPUSH(LONG_MIN_VALUE); }else{ OPPUSH(Math.round(value));}
DEFOP(AALOAD)
/*
* ..., array ref, index -> ..., ref value

18
src/log.jsh Normal file
View File

@ -0,0 +1,18 @@
/* -*- Mode: Javascript -*-
* -*- coding: UTF-8 -*-
* Copyright (C) 2011 by Artur Ventura
*
* File: log.jsh
*
* Author: Artur Ventura
*
*/
#ifndef DEBUG
#define LOG(msg)
#else
#define LOG(msg) write(msg);\
if (console){\
console.log(msg);\
}
#endif

View File

@ -9,26 +9,8 @@
*
*/
#ifndef DEBUG
#define LOG(msg)
#else
#define LOG(msg) write(msg);\
if (console){\
console.log(msg);\
}
#endif
#include "linearDataStream.js"
#include "constantPool.js"
#include "attributes.js"
#include "infos.js"
#include "class.js"
#include "cpu.js"
#include "types.js"
var test_jvm;
function main (args){
test_jvm = new JVM({},["foo"])
test_jvm.run();
}
}

View File

@ -8,7 +8,10 @@
* Author: Artur Ventura
*
*/
#ifndef _TYPES_JSH_
#define _TYPES_JSH_
// Local Variables Types
#define LOC_VAR_boolean 0x001;
#define LOC_VAR_byte 0x002;
@ -86,5 +89,5 @@
}
}*/
#endif //_TYPES_JSH_