diff --git a/.gitignore b/.gitignore index ddc09c8..3c3ae12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *~ *# *.py[co] - +build diff --git a/makefile b/makefile index 97ce022..7eb2d7e 100644 --- a/makefile +++ b/makefile @@ -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 diff --git a/src/attributes.js b/src/attributes.js index 32eb3e5..8a86675 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -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; -} \ No newline at end of file +} diff --git a/src/class.js b/src/class.js index 6fa47e6..9df593d 100644 --- a/src/class.js +++ b/src/class.js @@ -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 ; diff --git a/src/class.jsh b/src/class.jsh new file mode 100644 index 0000000..7caa773 --- /dev/null +++ b/src/class.jsh @@ -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. diff --git a/src/constantPool.js b/src/constantPool.js index b155719..7b9f78a 100644 --- a/src/constantPool.js +++ b/src/constantPool.js @@ -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(){ diff --git a/src/constantPool.jsh b/src/constantPool.jsh new file mode 100644 index 0000000..75cf9f9 --- /dev/null +++ b/src/constantPool.jsh @@ -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 diff --git a/src/cpu.js b/src/cpu.js index e15315c..955aa65 100644 --- a/src/cpu.js +++ b/src/cpu.js @@ -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" } -} \ No newline at end of file +} diff --git a/src/cpu.jsh b/src/cpu.jsh new file mode 100644 index 0000000..23bb40f --- /dev/null +++ b/src/cpu.jsh @@ -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_ diff --git a/src/infos.js b/src/infos.js index c317540..03fb8b5 100644 --- a/src/infos.js +++ b/src/infos.js @@ -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= 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 diff --git a/src/log.jsh b/src/log.jsh new file mode 100644 index 0000000..7f1cfbe --- /dev/null +++ b/src/log.jsh @@ -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 diff --git a/src/main.js b/src/main.js index 3dc755a..27c928a 100644 --- a/src/main.js +++ b/src/main.js @@ -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(); -} \ No newline at end of file +} diff --git a/src/opcodes.js b/src/opcodes.jsh similarity index 100% rename from src/opcodes.js rename to src/opcodes.jsh diff --git a/src/types.js b/src/types.jsh similarity index 97% rename from src/types.js rename to src/types.jsh index 827039f..e07faef 100644 --- a/src/types.js +++ b/src/types.jsh @@ -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_