Skip to content

Commit

Permalink
Rearrange packages
Browse files Browse the repository at this point in the history
  • Loading branch information
zxh0 committed Oct 19, 2019
1 parent 6bc5fd3 commit 26de814
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 62 deletions.
4 changes: 2 additions & 2 deletions classpath/classpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"path/filepath"
"strings"

"github.com/zxh0/jvm.go/options"
"github.com/zxh0/jvm.go/vm"
)

type ClassPath struct {
CompositeEntry
}

func Parse(opts options.Options) *ClassPath {
func Parse(opts vm.Options) *ClassPath {
cp := &ClassPath{}
cp.parseBootAndExtClassPath(opts.AbsJavaHome)
cp.parseUserClassPath(opts.Classpath)
Expand Down
38 changes: 30 additions & 8 deletions cmd/java/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package main

import (
"flag"
"fmt"
"os"
"runtime/pprof"
"strings"

"github.com/zxh0/jvm.go/classpath"
"github.com/zxh0/jvm.go/interpreter"
"github.com/zxh0/jvm.go/cpu"
_ "github.com/zxh0/jvm.go/native"
"github.com/zxh0/jvm.go/options"
"github.com/zxh0/jvm.go/rtda"
"github.com/zxh0/jvm.go/rtda/heap"
"github.com/zxh0/jvm.go/vm"
)

func main() {
opts, args := options.Parse()
opts, args := parseOptions()
if opts.HelpFlag || len(args) == 0 {
printUsage()
} else if opts.VersionFlag {
Expand All @@ -25,16 +26,37 @@ func main() {
}
}

func parseOptions() (vm.Options, []string) {
options := vm.Options{}
flag.StringVar(&options.Classpath, "classpath", "", "Specifies a list of directories, JAR files, and ZIP archives to search for class files.")
flag.StringVar(&options.Classpath, "cp", "", "Specifies a list of directories, JAR files, and ZIP archives to search for class files.")
flag.BoolVar(&options.HelpFlag, "help", false, "Displays usage information and exit.")
flag.BoolVar(&options.HelpFlag, "h", false, "Displays usage information and exit.")
flag.BoolVar(&options.HelpFlag, "?", false, "Displays usage information and exit.")
flag.BoolVar(&options.VerboseClass, "verbose:class", false, "Displays information about each class loaded.")
flag.BoolVar(&options.VerboseInstr, "verbose:instr", false, "Displays information about each instruction executed.")
flag.BoolVar(&options.VerboseJNI, "verbose:jni", false, "Displays information about the use of native methods and other Java Native Interface (JNI) activity.")
flag.BoolVar(&options.VersionFlag, "version", false, "Displays version information and exit.")
flag.StringVar(&options.Xss, "Xss", "", "Sets the thread stack size.")
flag.BoolVar(&options.XUseJavaHome, "XuseJavaHome", false, "Uses JAVA_HOME")
flag.BoolVar(&options.XDebugInstr, "Xdebug:instr", false, "Displays executed instructions")
flag.StringVar(&options.XCPUProfile, "Xprofile:cpu", "", "")
flag.Parse()

options.Init()
return options, flag.Args()
}

func printUsage() {
fmt.Printf("usage: %s [-options] class [args...]\n", os.Args[0])
options.PrintDefaults()
flag.PrintDefaults()
}

func printVersion() {
fmt.Println("jvm.go 0.1.8.0")
}

func startJVM(opts options.Options, mainClass string, args []string) {
func startJVM(opts vm.Options, mainClass string, args []string) {
if opts.XCPUProfile != "" {
f, err := os.Create(opts.XCPUProfile)
if err != nil {
Expand All @@ -49,11 +71,11 @@ func startJVM(opts options.Options, mainClass string, args []string) {

mainClass = strings.ReplaceAll(mainClass, ".", "/")
mainThread := createMainThread(opts, mainClass, args)
interpreter.Loop(mainThread)
interpreter.KeepAlive()
cpu.Loop(mainThread)
cpu.KeepAlive()
}

func createMainThread(opts options.Options, className string, args []string) *rtda.Thread {
func createMainThread(opts vm.Options, className string, args []string) *rtda.Thread {
mainThread := rtda.NewThread(nil, opts)
bootMethod := rtda.ShimBootstrapMethod
bootArgs := []heap.Slot{heap.NewHackSlot(className), heap.NewHackSlot(args)}
Expand Down
33 changes: 23 additions & 10 deletions cmd/javap/main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package main

import (
"flag"
"fmt"
"strings"

"github.com/zxh0/jvm.go/classfile"
"github.com/zxh0/jvm.go/classpath"
"github.com/zxh0/jvm.go/options"
"github.com/zxh0/jvm.go/rtda/heap"
"github.com/zxh0/jvm.go/vm"
)

func main() {
opts, args := options.Parse()
if opts.HelpFlag || len(args) == 0 {
printUsage()
}
printClassInfo(opts, args[0])
}

var (
primitiveMap = map[string]string{
"B": "byte",
Expand All @@ -40,11 +33,31 @@ var (
}
)

func main() {
opts, args := parseOptions()
if opts.HelpFlag || len(args) == 0 {
printUsage()
}
printClassInfo(opts, args[0])
}

func parseOptions() (vm.Options, []string) {
options := vm.Options{}
flag.StringVar(&options.Classpath, "classpath", "", "Specifies a list of directories, JAR files, and ZIP archives to search for class files.")
flag.StringVar(&options.Classpath, "cp", "", "Specifies a list of directories, JAR files, and ZIP archives to search for class files.")
flag.BoolVar(&options.HelpFlag, "help", false, "Displays usage information and exit.")
flag.BoolVar(&options.HelpFlag, "h", false, "Displays usage information and exit.")
flag.BoolVar(&options.HelpFlag, "?", false, "Displays usage information and exit.")
flag.BoolVar(&options.VersionFlag, "version", false, "Displays version information and exit.")
flag.Parse()
return options, flag.Args()
}

func printUsage() {
fmt.Println("usage: javap [-options] class [args...]")
}

func printClassInfo(opts options.Options, className string) {
func printClassInfo(opts vm.Options, className string) {
cp := classpath.Parse(opts)
_, classData, err := cp.ReadClass(className)

Expand Down
6 changes: 0 additions & 6 deletions cmd/jimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (
"github.com/zxh0/jvm.go/jimage"
)

const (
OffsetWidth = 12
SizeWidth = 10
CompressedSizeWidth = 10
)

const (
version = "jimage.go 0.0.1"
usage = `jimage.
Expand Down
2 changes: 1 addition & 1 deletion interpreter/keep_alive.go → cpu/keep_alive.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package interpreter
package cpu

import (
"sync"
Expand Down
6 changes: 3 additions & 3 deletions interpreter/interpreter.go → cpu/loop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package interpreter
package cpu

import (
"fmt"
Expand All @@ -7,7 +7,7 @@ import (
"github.com/zxh0/jvm.go/instructions/base"
"github.com/zxh0/jvm.go/rtda"
"github.com/zxh0/jvm.go/rtda/heap"
"github.com/zxh0/jvm.go/vmerrors"
"github.com/zxh0/jvm.go/vm"
)

func ExecMethod(thread *rtda.Thread, method *heap.Method, args []heap.Slot) heap.Slot {
Expand Down Expand Up @@ -105,7 +105,7 @@ func fetchInstruction(method *heap.Method, pc int) (base.Instruction, int) {
// todo
func _catchErr(thread *rtda.Thread) {
if r := recover(); r != nil {
if err, ok := r.(vmerrors.ClassNotFoundError); ok {
if err, ok := r.(vm.ClassNotFoundError); ok {
thread.ThrowClassNotFoundException(err.Error())
_loop(thread)
return
Expand Down
4 changes: 2 additions & 2 deletions native/java/lang/Thread.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lang

import (
"github.com/zxh0/jvm.go/interpreter"
"github.com/zxh0/jvm.go/cpu"
"github.com/zxh0/jvm.go/rtda"
"github.com/zxh0/jvm.go/rtda/heap"
)
Expand Down Expand Up @@ -93,5 +93,5 @@ func start0(frame *rtda.Frame) {
this.Extra = newThread
this.UnlockState()

go interpreter.Loop(newThread)
go cpu.Loop(newThread)
}
4 changes: 2 additions & 2 deletions rtda/heap/class_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/zxh0/jvm.go/classfile"
"github.com/zxh0/jvm.go/classpath"
"github.com/zxh0/jvm.go/vmerrors"
"github.com/zxh0/jvm.go/vm"
)

const (
Expand Down Expand Up @@ -179,7 +179,7 @@ func (loader *ClassLoader) reallyLoadClass(name string) *Class {
func (loader *ClassLoader) readClassData(name string) (classpath.Entry, []byte) {
cpEntry, classData, err := loader.classPath.ReadClass(name)
if err != nil {
panic(vmerrors.NewClassNotFoundError(SlashToDot(name)))
panic(vm.NewClassNotFoundError(SlashToDot(name)))
}

return cpEntry, classData
Expand Down
6 changes: 3 additions & 3 deletions rtda/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"sync"

"github.com/zxh0/jvm.go/options"
"github.com/zxh0/jvm.go/rtda/heap"
"github.com/zxh0/jvm.go/vm"
)

/*
Expand All @@ -29,12 +29,12 @@ type Thread struct {
interruptedFlag bool
parkingFlag bool // used by Unsafe
unparkedFlag bool // used by Unsafe
VMOptions options.Options
VMOptions vm.Options
JNIEnv interface{}
// todo
}

func NewThread(jThread *heap.Object, opts options.Options) *Thread {
func NewThread(jThread *heap.Object, opts vm.Options) *Thread {
stack := newStack(uint(opts.ThreadStackSize))
thread := &Thread{
stack: stack,
Expand Down
2 changes: 1 addition & 1 deletion vmerrors/class_not_found.go → vm/err_class_not_found.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package vmerrors
package vm

type ClassNotFoundError struct {
name string
Expand Down
26 changes: 2 additions & 24 deletions options/options.go → vm/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package options
package vm

import (
"errors"
"flag"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -31,31 +30,10 @@ type Options struct {
ThreadStackSize int
}

func Parse() (Options, []string) {
options := Options{}
flag.StringVar(&options.Classpath, "classpath", "", "Specifies a list of directories, JAR files, and ZIP archives to search for class files.")
flag.StringVar(&options.Classpath, "cp", "", "Specifies a list of directories, JAR files, and ZIP archives to search for class files.")
flag.BoolVar(&options.HelpFlag, "help", false, "Displays usage information and exit.")
flag.BoolVar(&options.HelpFlag, "h", false, "Displays usage information and exit.")
flag.BoolVar(&options.HelpFlag, "?", false, "Displays usage information and exit.")
flag.BoolVar(&options.VerboseClass, "verbose:class", false, "Displays information about each class loaded.")
flag.BoolVar(&options.VerboseInstr, "verbose:instr", false, "Displays information about each instruction executed.")
flag.BoolVar(&options.VerboseJNI, "verbose:jni", false, "Displays information about the use of native methods and other Java Native Interface (JNI) activity.")
flag.BoolVar(&options.VersionFlag, "version", false, "Displays version information and exit.")
flag.StringVar(&options.Xss, "Xss", "", "Sets the thread stack size.")
flag.BoolVar(&options.XUseJavaHome, "XuseJavaHome", false, "Uses JAVA_HOME")
flag.BoolVar(&options.XDebugInstr, "Xdebug:instr", false, "Displays executed instructions")
flag.StringVar(&options.XCPUProfile, "Xprofile:cpu", "", "")
flag.Parse()

func (options *Options) Init() {
options.AbsJavaHome = getJavaHome(options.XUseJavaHome)
options.AbsJreLib = filepath.Join(options.AbsJavaHome, "lib")
options.ThreadStackSize = parseXss(options.Xss)
return options, flag.Args()
}

func PrintDefaults() {
flag.PrintDefaults()
}

func getJavaHome(useOsEnv bool) string {
Expand Down

0 comments on commit 26de814

Please sign in to comment.