text
stringlengths 9
39.2M
| dir
stringlengths 25
226
| lang
stringclasses 163
values | created_date
timestamp[s] | updated_date
timestamp[s] | repo_name
stringclasses 751
values | repo_full_name
stringclasses 752
values | star
int64 1.01k
183k
| len_tokens
int64 1
18.5M
|
|---|---|---|---|---|---|---|---|---|
```go
package classfile
// Constant pool tags
const (
CONSTANT_Class = 7
CONSTANT_Fieldref = 9
CONSTANT_Methodref = 10
CONSTANT_InterfaceMethodref = 11
CONSTANT_String = 8
CONSTANT_Integer = 3
CONSTANT_Float = 4
CONSTANT_Long = 5
CONSTANT_Double = 6
CONSTANT_NameAndType = 12
CONSTANT_Utf8 = 1
CONSTANT_MethodHandle = 15
CONSTANT_MethodType = 16
CONSTANT_InvokeDynamic = 18
)
/*
cp_info {
u1 tag;
u1 info[];
}
*/
type ConstantInfo interface {
readInfo(reader *ClassReader)
}
func readConstantInfo(reader *ClassReader, cp ConstantPool) ConstantInfo {
tag := reader.readUint8()
c := newConstantInfo(tag, cp)
c.readInfo(reader)
return c
}
// todo ugly code
func newConstantInfo(tag uint8, cp ConstantPool) ConstantInfo {
switch tag {
case CONSTANT_Integer:
return &ConstantIntegerInfo{}
case CONSTANT_Float:
return &ConstantFloatInfo{}
case CONSTANT_Long:
return &ConstantLongInfo{}
case CONSTANT_Double:
return &ConstantDoubleInfo{}
case CONSTANT_Utf8:
return &ConstantUtf8Info{}
case CONSTANT_String:
return &ConstantStringInfo{cp: cp}
case CONSTANT_Class:
return &ConstantClassInfo{cp: cp}
case CONSTANT_Fieldref:
return &ConstantFieldrefInfo{ConstantMemberrefInfo{cp: cp}}
case CONSTANT_Methodref:
return &ConstantMethodrefInfo{ConstantMemberrefInfo{cp: cp}}
case CONSTANT_InterfaceMethodref:
return &ConstantInterfaceMethodrefInfo{ConstantMemberrefInfo{cp: cp}}
case CONSTANT_NameAndType:
return &ConstantNameAndTypeInfo{}
case CONSTANT_MethodType:
return &ConstantMethodTypeInfo{}
case CONSTANT_MethodHandle:
return &ConstantMethodHandleInfo{}
case CONSTANT_InvokeDynamic:
return &ConstantInvokeDynamicInfo{}
default:
panic("java.lang.ClassFormatError: constant pool tag!")
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/constant_info.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 468
|
```go
package classfile
import "math"
/*
CONSTANT_Integer_info {
u1 tag;
u4 bytes;
}
*/
type ConstantIntegerInfo struct {
val int32
}
func (self *ConstantIntegerInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint32()
self.val = int32(bytes)
}
func (self *ConstantIntegerInfo) Value() int32 {
return self.val
}
/*
CONSTANT_Float_info {
u1 tag;
u4 bytes;
}
*/
type ConstantFloatInfo struct {
val float32
}
func (self *ConstantFloatInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint32()
self.val = math.Float32frombits(bytes)
}
func (self *ConstantFloatInfo) Value() float32 {
return self.val
}
/*
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
*/
type ConstantLongInfo struct {
val int64
}
func (self *ConstantLongInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint64()
self.val = int64(bytes)
}
func (self *ConstantLongInfo) Value() int64 {
return self.val
}
/*
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
*/
type ConstantDoubleInfo struct {
val float64
}
func (self *ConstantDoubleInfo) readInfo(reader *ClassReader) {
bytes := reader.readUint64()
self.val = math.Float64frombits(bytes)
}
func (self *ConstantDoubleInfo) Value() float64 {
return self.val
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/cp_numeric.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 341
|
```go
package classfile
/*
SourceFile_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 sourcefile_index;
}
*/
type SourceFileAttribute struct {
cp ConstantPool
sourceFileIndex uint16
}
func (self *SourceFileAttribute) readInfo(reader *ClassReader) {
self.sourceFileIndex = reader.readUint16()
}
func (self *SourceFileAttribute) FileName() string {
return self.cp.getUtf8(self.sourceFileIndex)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attr_source_file.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 101
|
```go
package classfile
import "fmt"
/*
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
*/
type ClassFile struct {
//magic uint32
minorVersion uint16
majorVersion uint16
constantPool ConstantPool
accessFlags uint16
thisClass uint16
superClass uint16
interfaces []uint16
fields []*MemberInfo
methods []*MemberInfo
attributes []AttributeInfo
}
func Parse(classData []byte) (cf *ClassFile, err error) {
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}
}
}()
cr := &ClassReader{classData}
cf = &ClassFile{}
cf.read(cr)
return
}
func (self *ClassFile) read(reader *ClassReader) {
self.readAndCheckMagic(reader)
self.readAndCheckVersion(reader)
self.constantPool = readConstantPool(reader)
self.accessFlags = reader.readUint16()
self.thisClass = reader.readUint16()
self.superClass = reader.readUint16()
self.interfaces = reader.readUint16s()
self.fields = readMembers(reader, self.constantPool)
self.methods = readMembers(reader, self.constantPool)
self.attributes = readAttributes(reader, self.constantPool)
}
func (self *ClassFile) readAndCheckMagic(reader *ClassReader) {
magic := reader.readUint32()
if magic != 0xCAFEBABE {
panic("java.lang.ClassFormatError: magic!")
}
}
func (self *ClassFile) readAndCheckVersion(reader *ClassReader) {
self.minorVersion = reader.readUint16()
self.majorVersion = reader.readUint16()
switch self.majorVersion {
case 45:
return
case 46, 47, 48, 49, 50, 51, 52:
if self.minorVersion == 0 {
return
}
}
panic("java.lang.UnsupportedClassVersionError!")
}
func (self *ClassFile) MinorVersion() uint16 {
return self.minorVersion
}
func (self *ClassFile) MajorVersion() uint16 {
return self.majorVersion
}
func (self *ClassFile) ConstantPool() ConstantPool {
return self.constantPool
}
func (self *ClassFile) AccessFlags() uint16 {
return self.accessFlags
}
func (self *ClassFile) Fields() []*MemberInfo {
return self.fields
}
func (self *ClassFile) Methods() []*MemberInfo {
return self.methods
}
func (self *ClassFile) ClassName() string {
return self.constantPool.getClassName(self.thisClass)
}
func (self *ClassFile) SuperClassName() string {
if self.superClass > 0 {
return self.constantPool.getClassName(self.superClass)
}
return ""
}
func (self *ClassFile) InterfaceNames() []string {
interfaceNames := make([]string, len(self.interfaces))
for i, cpIndex := range self.interfaces {
interfaceNames[i] = self.constantPool.getClassName(cpIndex)
}
return interfaceNames
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/class_file.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 787
|
```go
package classfile
/*
ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 constantvalue_index;
}
*/
type ConstantValueAttribute struct {
constantValueIndex uint16
}
func (self *ConstantValueAttribute) readInfo(reader *ClassReader) {
self.constantValueIndex = reader.readUint16()
}
func (self *ConstantValueAttribute) ConstantValueIndex() uint16 {
return self.constantValueIndex
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attr_constant_value.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 95
|
```go
package classfile
/*
LocalVariableTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_table_length;
{ u2 start_pc;
u2 length;
u2 name_index;
u2 descriptor_index;
u2 index;
} local_variable_table[local_variable_table_length];
}
*/
type LocalVariableTableAttribute struct {
localVariableTable []*LocalVariableTableEntry
}
type LocalVariableTableEntry struct {
startPc uint16
length uint16
nameIndex uint16
descriptorIndex uint16
index uint16
}
func (self *LocalVariableTableAttribute) readInfo(reader *ClassReader) {
localVariableTableLength := reader.readUint16()
self.localVariableTable = make([]*LocalVariableTableEntry, localVariableTableLength)
for i := range self.localVariableTable {
self.localVariableTable[i] = &LocalVariableTableEntry{
startPc: reader.readUint16(),
length: reader.readUint16(),
nameIndex: reader.readUint16(),
descriptorIndex: reader.readUint16(),
index: reader.readUint16(),
}
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attr_local_variable_table.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 251
|
```go
package classfile
import "encoding/binary"
type ClassReader struct {
data []byte
}
// u1
func (self *ClassReader) readUint8() uint8 {
val := self.data[0]
self.data = self.data[1:]
return val
}
// u2
func (self *ClassReader) readUint16() uint16 {
val := binary.BigEndian.Uint16(self.data)
self.data = self.data[2:]
return val
}
// u4
func (self *ClassReader) readUint32() uint32 {
val := binary.BigEndian.Uint32(self.data)
self.data = self.data[4:]
return val
}
func (self *ClassReader) readUint64() uint64 {
val := binary.BigEndian.Uint64(self.data)
self.data = self.data[8:]
return val
}
func (self *ClassReader) readUint16s() []uint16 {
n := self.readUint16()
s := make([]uint16, n)
for i := range s {
s[i] = self.readUint16()
}
return s
}
func (self *ClassReader) readBytes(n uint32) []byte {
bytes := self.data[:n]
self.data = self.data[n:]
return bytes
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/class_reader.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 262
|
```go
package classfile
/*
InnerClasses_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_classes;
{ u2 inner_class_info_index;
u2 outer_class_info_index;
u2 inner_name_index;
u2 inner_class_access_flags;
} classes[number_of_classes];
}
*/
type InnerClassesAttribute struct {
classes []*InnerClassInfo
}
type InnerClassInfo struct {
innerClassInfoIndex uint16
outerClassInfoIndex uint16
innerNameIndex uint16
innerClassAccessFlags uint16
}
func (self *InnerClassesAttribute) readInfo(reader *ClassReader) {
numberOfClasses := reader.readUint16()
self.classes = make([]*InnerClassInfo, numberOfClasses)
for i := range self.classes {
self.classes[i] = &InnerClassInfo{
innerClassInfoIndex: reader.readUint16(),
outerClassInfoIndex: reader.readUint16(),
innerNameIndex: reader.readUint16(),
innerClassAccessFlags: reader.readUint16(),
}
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attr_inner_classes.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 238
|
```go
package classfile
/*
Signature_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 signature_index;
}
*/
type SignatureAttribute struct {
cp ConstantPool
signatureIndex uint16
}
func (self *SignatureAttribute) readInfo(reader *ClassReader) {
self.signatureIndex = reader.readUint16()
}
func (self *SignatureAttribute) Signature() string {
return self.cp.getUtf8(self.signatureIndex)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attr_signature.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 94
|
```go
package classfile
import "fmt"
import "unicode/utf16"
/*
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}
*/
type ConstantUtf8Info struct {
str string
}
func (self *ConstantUtf8Info) readInfo(reader *ClassReader) {
length := uint32(reader.readUint16())
bytes := reader.readBytes(length)
self.str = decodeMUTF8(bytes)
}
func (self *ConstantUtf8Info) Str() string {
return self.str
}
/*
func decodeMUTF8(bytes []byte) string {
return string(bytes) // not correct!
}
*/
// mutf8 -> utf16 -> utf32 -> string
// see java.io.DataInputStream.readUTF(DataInput)
func decodeMUTF8(bytearr []byte) string {
utflen := len(bytearr)
chararr := make([]uint16, utflen)
var c, char2, char3 uint16
count := 0
chararr_count := 0
for count < utflen {
c = uint16(bytearr[count])
if c > 127 {
break
}
count++
chararr[chararr_count] = c
chararr_count++
}
for count < utflen {
c = uint16(bytearr[count])
switch c >> 4 {
case 0, 1, 2, 3, 4, 5, 6, 7:
/* 0xxxxxxx*/
count++
chararr[chararr_count] = c
chararr_count++
case 12, 13:
/* 110x xxxx 10xx xxxx*/
count += 2
if count > utflen {
panic("malformed input: partial character at end")
}
char2 = uint16(bytearr[count-1])
if char2&0xC0 != 0x80 {
panic(fmt.Errorf("malformed input around byte %v", count))
}
chararr[chararr_count] = c&0x1F<<6 | char2&0x3F
chararr_count++
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx*/
count += 3
if count > utflen {
panic("malformed input: partial character at end")
}
char2 = uint16(bytearr[count-2])
char3 = uint16(bytearr[count-1])
if char2&0xC0 != 0x80 || char3&0xC0 != 0x80 {
panic(fmt.Errorf("malformed input around byte %v", (count - 1)))
}
chararr[chararr_count] = c&0x0F<<12 | char2&0x3F<<6 | char3&0x3F<<0
chararr_count++
default:
/* 10xx xxxx, 1111 xxxx */
panic(fmt.Errorf("malformed input around byte %v", count))
}
}
// The number of chars produced may be less than utflen
chararr = chararr[0:chararr_count]
runes := utf16.Decode(chararr)
return string(runes)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/cp_utf8.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 702
|
```go
package classfile
/*
Code_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 max_stack;
u2 max_locals;
u4 code_length;
u1 code[code_length];
u2 exception_table_length;
{ u2 start_pc;
u2 end_pc;
u2 handler_pc;
u2 catch_type;
} exception_table[exception_table_length];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
*/
type CodeAttribute struct {
cp ConstantPool
maxStack uint16
maxLocals uint16
code []byte
exceptionTable []*ExceptionTableEntry
attributes []AttributeInfo
}
func (self *CodeAttribute) readInfo(reader *ClassReader) {
self.maxStack = reader.readUint16()
self.maxLocals = reader.readUint16()
codeLength := reader.readUint32()
self.code = reader.readBytes(codeLength)
self.exceptionTable = readExceptionTable(reader)
self.attributes = readAttributes(reader, self.cp)
}
func (self *CodeAttribute) MaxStack() uint {
return uint(self.maxStack)
}
func (self *CodeAttribute) MaxLocals() uint {
return uint(self.maxLocals)
}
func (self *CodeAttribute) Code() []byte {
return self.code
}
func (self *CodeAttribute) ExceptionTable() []*ExceptionTableEntry {
return self.exceptionTable
}
type ExceptionTableEntry struct {
startPc uint16
endPc uint16
handlerPc uint16
catchType uint16
}
func readExceptionTable(reader *ClassReader) []*ExceptionTableEntry {
exceptionTableLength := reader.readUint16()
exceptionTable := make([]*ExceptionTableEntry, exceptionTableLength)
for i := range exceptionTable {
exceptionTable[i] = &ExceptionTableEntry{
startPc: reader.readUint16(),
endPc: reader.readUint16(),
handlerPc: reader.readUint16(),
catchType: reader.readUint16(),
}
}
return exceptionTable
}
func (self *ExceptionTableEntry) StartPc() uint16 {
return self.startPc
}
func (self *ExceptionTableEntry) EndPc() uint16 {
return self.endPc
}
func (self *ExceptionTableEntry) HandlerPc() uint16 {
return self.handlerPc
}
func (self *ExceptionTableEntry) CatchType() uint16 {
return self.catchType
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attr_code.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 524
|
```go
package classfile
/*
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
CONSTANT_InterfaceMethodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
}
*/
type ConstantFieldrefInfo struct{ ConstantMemberrefInfo }
type ConstantMethodrefInfo struct{ ConstantMemberrefInfo }
type ConstantInterfaceMethodrefInfo struct{ ConstantMemberrefInfo }
type ConstantMemberrefInfo struct {
cp ConstantPool
classIndex uint16
nameAndTypeIndex uint16
}
func (self *ConstantMemberrefInfo) readInfo(reader *ClassReader) {
self.classIndex = reader.readUint16()
self.nameAndTypeIndex = reader.readUint16()
}
func (self *ConstantMemberrefInfo) ClassName() string {
return self.cp.getClassName(self.classIndex)
}
func (self *ConstantMemberrefInfo) NameAndDescriptor() (string, string) {
return self.cp.getNameAndType(self.nameAndTypeIndex)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/cp_member_ref.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 241
|
```go
package classfile
/*
CONSTANT_String_info {
u1 tag;
u2 string_index;
}
*/
type ConstantStringInfo struct {
cp ConstantPool
stringIndex uint16
}
func (self *ConstantStringInfo) readInfo(reader *ClassReader) {
self.stringIndex = reader.readUint16()
}
func (self *ConstantStringInfo) String() string {
return self.cp.getUtf8(self.stringIndex)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/cp_string.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 90
|
```go
package classfile
var (
_attrDeprecated = &DeprecatedAttribute{}
_attrSynthetic = &SyntheticAttribute{}
)
/*
attribute_info {
u2 attribute_name_index;
u4 attribute_length;
u1 info[attribute_length];
}
*/
type AttributeInfo interface {
readInfo(reader *ClassReader)
}
func readAttributes(reader *ClassReader, cp ConstantPool) []AttributeInfo {
attributesCount := reader.readUint16()
attributes := make([]AttributeInfo, attributesCount)
for i := range attributes {
attributes[i] = readAttribute(reader, cp)
}
return attributes
}
func readAttribute(reader *ClassReader, cp ConstantPool) AttributeInfo {
attrNameIndex := reader.readUint16()
attrName := cp.getUtf8(attrNameIndex)
attrLen := reader.readUint32()
attrInfo := newAttributeInfo(attrName, attrLen, cp)
attrInfo.readInfo(reader)
return attrInfo
}
func newAttributeInfo(attrName string, attrLen uint32, cp ConstantPool) AttributeInfo {
switch attrName {
// case "AnnotationDefault":
case "BootstrapMethods":
return &BootstrapMethodsAttribute{}
case "Code":
return &CodeAttribute{cp: cp}
case "ConstantValue":
return &ConstantValueAttribute{}
case "Deprecated":
return _attrDeprecated
case "EnclosingMethod":
return &EnclosingMethodAttribute{cp: cp}
case "Exceptions":
return &ExceptionsAttribute{}
case "InnerClasses":
return &InnerClassesAttribute{}
case "LineNumberTable":
return &LineNumberTableAttribute{}
case "LocalVariableTable":
return &LocalVariableTableAttribute{}
case "LocalVariableTypeTable":
return &LocalVariableTypeTableAttribute{}
// case "MethodParameters":
// case "RuntimeInvisibleAnnotations":
// case "RuntimeInvisibleParameterAnnotations":
// case "RuntimeInvisibleTypeAnnotations":
// case "RuntimeVisibleAnnotations":
// case "RuntimeVisibleParameterAnnotations":
// case "RuntimeVisibleTypeAnnotations":
case "Signature":
return &SignatureAttribute{cp: cp}
case "SourceFile":
return &SourceFileAttribute{cp: cp}
// case "SourceDebugExtension":
// case "StackMapTable":
case "Synthetic":
return _attrSynthetic
default:
return &UnparsedAttribute{attrName, attrLen, nil}
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attribute_info.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 496
|
```go
package rtda
import "jvmgo/ch07/rtda/heap"
/*
JVM
Thread
pc
Stack
Frame
LocalVars
OperandStack
*/
type Thread struct {
pc int // the address of the instruction currently being executed
stack *Stack
// todo
}
func NewThread() *Thread {
return &Thread{
stack: newStack(1024),
}
}
func (self *Thread) PC() int {
return self.pc
}
func (self *Thread) SetPC(pc int) {
self.pc = pc
}
func (self *Thread) PushFrame(frame *Frame) {
self.stack.push(frame)
}
func (self *Thread) PopFrame() *Frame {
return self.stack.pop()
}
func (self *Thread) CurrentFrame() *Frame {
return self.stack.top()
}
func (self *Thread) TopFrame() *Frame {
return self.stack.top()
}
func (self *Thread) IsStackEmpty() bool {
return self.stack.isEmpty()
}
func (self *Thread) NewFrame(method *heap.Method) *Frame {
return newFrame(self, method)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/thread.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 241
|
```go
package classfile
/*
LocalVariableTypeTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_type_table_length;
{ u2 start_pc;
u2 length;
u2 name_index;
u2 signature_index;
u2 index;
} local_variable_type_table[local_variable_type_table_length];
}
*/
type LocalVariableTypeTableAttribute struct {
localVariableTypeTable []*LocalVariableTypeTableEntry
}
type LocalVariableTypeTableEntry struct {
startPc uint16
length uint16
nameIndex uint16
signatureIndex uint16
index uint16
}
func (self *LocalVariableTypeTableAttribute) readInfo(reader *ClassReader) {
localVariableTypeTableLength := reader.readUint16()
self.localVariableTypeTable = make([]*LocalVariableTypeTableEntry, localVariableTypeTableLength)
for i := range self.localVariableTypeTable {
self.localVariableTypeTable[i] = &LocalVariableTypeTableEntry{
startPc: reader.readUint16(),
length: reader.readUint16(),
nameIndex: reader.readUint16(),
signatureIndex: reader.readUint16(),
index: reader.readUint16(),
}
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classfile/attr_local_variable_type_table.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 267
|
```go
package rtda
import "math"
import "jvmgo/ch07/rtda/heap"
type LocalVars []Slot
func newLocalVars(maxLocals uint) LocalVars {
if maxLocals > 0 {
return make([]Slot, maxLocals)
}
return nil
}
func (self LocalVars) SetInt(index uint, val int32) {
self[index].num = val
}
func (self LocalVars) GetInt(index uint) int32 {
return self[index].num
}
func (self LocalVars) SetFloat(index uint, val float32) {
bits := math.Float32bits(val)
self[index].num = int32(bits)
}
func (self LocalVars) GetFloat(index uint) float32 {
bits := uint32(self[index].num)
return math.Float32frombits(bits)
}
// long consumes two slots
func (self LocalVars) SetLong(index uint, val int64) {
self[index].num = int32(val)
self[index+1].num = int32(val >> 32)
}
func (self LocalVars) GetLong(index uint) int64 {
low := uint32(self[index].num)
high := uint32(self[index+1].num)
return int64(high)<<32 | int64(low)
}
// double consumes two slots
func (self LocalVars) SetDouble(index uint, val float64) {
bits := math.Float64bits(val)
self.SetLong(index, int64(bits))
}
func (self LocalVars) GetDouble(index uint) float64 {
bits := uint64(self.GetLong(index))
return math.Float64frombits(bits)
}
func (self LocalVars) SetRef(index uint, ref *heap.Object) {
self[index].ref = ref
}
func (self LocalVars) GetRef(index uint) *heap.Object {
return self[index].ref
}
func (self LocalVars) SetSlot(index uint, slot Slot) {
self[index] = slot
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/local_vars.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 415
|
```go
package rtda
import "math"
import "jvmgo/ch07/rtda/heap"
type OperandStack struct {
size uint
slots []Slot
}
func newOperandStack(maxStack uint) *OperandStack {
if maxStack > 0 {
return &OperandStack{
slots: make([]Slot, maxStack),
}
}
return nil
}
func (self *OperandStack) PushInt(val int32) {
self.slots[self.size].num = val
self.size++
}
func (self *OperandStack) PopInt() int32 {
self.size--
return self.slots[self.size].num
}
func (self *OperandStack) PushFloat(val float32) {
bits := math.Float32bits(val)
self.slots[self.size].num = int32(bits)
self.size++
}
func (self *OperandStack) PopFloat() float32 {
self.size--
bits := uint32(self.slots[self.size].num)
return math.Float32frombits(bits)
}
// long consumes two slots
func (self *OperandStack) PushLong(val int64) {
self.slots[self.size].num = int32(val)
self.slots[self.size+1].num = int32(val >> 32)
self.size += 2
}
func (self *OperandStack) PopLong() int64 {
self.size -= 2
low := uint32(self.slots[self.size].num)
high := uint32(self.slots[self.size+1].num)
return int64(high)<<32 | int64(low)
}
// double consumes two slots
func (self *OperandStack) PushDouble(val float64) {
bits := math.Float64bits(val)
self.PushLong(int64(bits))
}
func (self *OperandStack) PopDouble() float64 {
bits := uint64(self.PopLong())
return math.Float64frombits(bits)
}
func (self *OperandStack) PushRef(ref *heap.Object) {
self.slots[self.size].ref = ref
self.size++
}
func (self *OperandStack) PopRef() *heap.Object {
self.size--
ref := self.slots[self.size].ref
self.slots[self.size].ref = nil
return ref
}
func (self *OperandStack) PushSlot(slot Slot) {
self.slots[self.size] = slot
self.size++
}
func (self *OperandStack) PopSlot() Slot {
self.size--
return self.slots[self.size]
}
func (self *OperandStack) GetRefFromTop(n uint) *heap.Object {
return self.slots[self.size-1-n].ref
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/operand_stack.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 547
|
```go
package rtda
import "jvmgo/ch07/rtda/heap"
type Slot struct {
num int32
ref *heap.Object
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/slot.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 33
|
```go
package rtda
// jvm stack
type Stack struct {
maxSize uint
size uint
_top *Frame // stack is implemented as linked list
}
func newStack(maxSize uint) *Stack {
return &Stack{
maxSize: maxSize,
}
}
func (self *Stack) push(frame *Frame) {
if self.size >= self.maxSize {
panic("java.lang.StackOverflowError")
}
if self._top != nil {
frame.lower = self._top
}
self._top = frame
self.size++
}
func (self *Stack) pop() *Frame {
if self._top == nil {
panic("jvm stack is empty!")
}
top := self._top
self._top = top.lower
top.lower = nil
self.size--
return top
}
func (self *Stack) top() *Frame {
if self._top == nil {
panic("jvm stack is empty!")
}
return self._top
}
func (self *Stack) isEmpty() bool {
return self._top == nil
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/jvm_stack.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 225
|
```go
package rtda
import "jvmgo/ch07/rtda/heap"
// stack frame
type Frame struct {
lower *Frame // stack is implemented as linked list
localVars LocalVars
operandStack *OperandStack
thread *Thread
method *heap.Method
nextPC int // the next instruction after the call
}
func newFrame(thread *Thread, method *heap.Method) *Frame {
return &Frame{
thread: thread,
method: method,
localVars: newLocalVars(method.MaxLocals()),
operandStack: newOperandStack(method.MaxStack()),
}
}
// getters & setters
func (self *Frame) LocalVars() LocalVars {
return self.localVars
}
func (self *Frame) OperandStack() *OperandStack {
return self.operandStack
}
func (self *Frame) Thread() *Thread {
return self.thread
}
func (self *Frame) Method() *heap.Method {
return self.method
}
func (self *Frame) NextPC() int {
return self.nextPC
}
func (self *Frame) SetNextPC(nextPC int) {
self.nextPC = nextPC
}
func (self *Frame) RevertNextPC() {
self.nextPC = self.thread.pc
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/frame.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 272
|
```go
package heap
// symbolic reference
type SymRef struct {
cp *ConstantPool
className string
class *Class
}
func (self *SymRef) ResolvedClass() *Class {
if self.class == nil {
self.resolveClassRef()
}
return self.class
}
// jvms8 5.4.3.1
func (self *SymRef) resolveClassRef() {
d := self.cp.class
c := d.loader.LoadClass(self.className)
if !c.isAccessibleTo(d) {
panic("java.lang.IllegalAccessError")
}
self.class = c
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/cp_symref.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 129
|
```go
package heap
import "jvmgo/ch07/classfile"
type MethodRef struct {
MemberRef
method *Method
}
func newMethodRef(cp *ConstantPool, refInfo *classfile.ConstantMethodrefInfo) *MethodRef {
ref := &MethodRef{}
ref.cp = cp
ref.copyMemberRefInfo(&refInfo.ConstantMemberrefInfo)
return ref
}
func (self *MethodRef) ResolvedMethod() *Method {
if self.method == nil {
self.resolveMethodRef()
}
return self.method
}
// jvms8 5.4.3.3
func (self *MethodRef) resolveMethodRef() {
d := self.cp.class
c := self.ResolvedClass()
if c.IsInterface() {
panic("java.lang.IncompatibleClassChangeError")
}
method := lookupMethod(c, self.name, self.descriptor)
if method == nil {
panic("java.lang.NoSuchMethodError")
}
if !method.isAccessibleTo(d) {
panic("java.lang.IllegalAccessError")
}
self.method = method
}
func lookupMethod(class *Class, name, descriptor string) *Method {
method := LookupMethodInClass(class, name, descriptor)
if method == nil {
method = lookupMethodInInterfaces(class.interfaces, name, descriptor)
}
return method
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/cp_methodref.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 277
|
```go
package heap
import "strings"
import "jvmgo/ch07/classfile"
// name, superClassName and interfaceNames are all binary names(jvms8-4.2.1)
type Class struct {
accessFlags uint16
name string // thisClassName
superClassName string
interfaceNames []string
constantPool *ConstantPool
fields []*Field
methods []*Method
loader *ClassLoader
superClass *Class
interfaces []*Class
instanceSlotCount uint
staticSlotCount uint
staticVars Slots
initStarted bool
}
func newClass(cf *classfile.ClassFile) *Class {
class := &Class{}
class.accessFlags = cf.AccessFlags()
class.name = cf.ClassName()
class.superClassName = cf.SuperClassName()
class.interfaceNames = cf.InterfaceNames()
class.constantPool = newConstantPool(class, cf.ConstantPool())
class.fields = newFields(class, cf.Fields())
class.methods = newMethods(class, cf.Methods())
return class
}
func (self *Class) IsPublic() bool {
return 0 != self.accessFlags&ACC_PUBLIC
}
func (self *Class) IsFinal() bool {
return 0 != self.accessFlags&ACC_FINAL
}
func (self *Class) IsSuper() bool {
return 0 != self.accessFlags&ACC_SUPER
}
func (self *Class) IsInterface() bool {
return 0 != self.accessFlags&ACC_INTERFACE
}
func (self *Class) IsAbstract() bool {
return 0 != self.accessFlags&ACC_ABSTRACT
}
func (self *Class) IsSynthetic() bool {
return 0 != self.accessFlags&ACC_SYNTHETIC
}
func (self *Class) IsAnnotation() bool {
return 0 != self.accessFlags&ACC_ANNOTATION
}
func (self *Class) IsEnum() bool {
return 0 != self.accessFlags&ACC_ENUM
}
// getters
func (self *Class) Name() string {
return self.name
}
func (self *Class) ConstantPool() *ConstantPool {
return self.constantPool
}
func (self *Class) Fields() []*Field {
return self.fields
}
func (self *Class) Methods() []*Method {
return self.methods
}
func (self *Class) SuperClass() *Class {
return self.superClass
}
func (self *Class) StaticVars() Slots {
return self.staticVars
}
func (self *Class) InitStarted() bool {
return self.initStarted
}
func (self *Class) StartInit() {
self.initStarted = true
}
// jvms 5.4.4
func (self *Class) isAccessibleTo(other *Class) bool {
return self.IsPublic() ||
self.GetPackageName() == other.GetPackageName()
}
func (self *Class) GetPackageName() string {
if i := strings.LastIndex(self.name, "/"); i >= 0 {
return self.name[:i]
}
return ""
}
func (self *Class) GetMainMethod() *Method {
return self.getStaticMethod("main", "([Ljava/lang/String;)V")
}
func (self *Class) GetClinitMethod() *Method {
return self.getStaticMethod("<clinit>", "()V")
}
func (self *Class) getStaticMethod(name, descriptor string) *Method {
for _, method := range self.methods {
if method.IsStatic() &&
method.name == name &&
method.descriptor == descriptor {
return method
}
}
return nil
}
func (self *Class) NewObject() *Object {
return newObject(self)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/class.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 761
|
```go
package heap
import "fmt"
import "jvmgo/ch07/classfile"
type Constant interface{}
type ConstantPool struct {
class *Class
consts []Constant
}
func newConstantPool(class *Class, cfCp classfile.ConstantPool) *ConstantPool {
cpCount := len(cfCp)
consts := make([]Constant, cpCount)
rtCp := &ConstantPool{class, consts}
for i := 1; i < cpCount; i++ {
cpInfo := cfCp[i]
switch cpInfo.(type) {
case *classfile.ConstantIntegerInfo:
intInfo := cpInfo.(*classfile.ConstantIntegerInfo)
consts[i] = intInfo.Value()
case *classfile.ConstantFloatInfo:
floatInfo := cpInfo.(*classfile.ConstantFloatInfo)
consts[i] = floatInfo.Value()
case *classfile.ConstantLongInfo:
longInfo := cpInfo.(*classfile.ConstantLongInfo)
consts[i] = longInfo.Value()
i++
case *classfile.ConstantDoubleInfo:
doubleInfo := cpInfo.(*classfile.ConstantDoubleInfo)
consts[i] = doubleInfo.Value()
i++
case *classfile.ConstantStringInfo:
stringInfo := cpInfo.(*classfile.ConstantStringInfo)
consts[i] = stringInfo.String()
case *classfile.ConstantClassInfo:
classInfo := cpInfo.(*classfile.ConstantClassInfo)
consts[i] = newClassRef(rtCp, classInfo)
case *classfile.ConstantFieldrefInfo:
fieldrefInfo := cpInfo.(*classfile.ConstantFieldrefInfo)
consts[i] = newFieldRef(rtCp, fieldrefInfo)
case *classfile.ConstantMethodrefInfo:
methodrefInfo := cpInfo.(*classfile.ConstantMethodrefInfo)
consts[i] = newMethodRef(rtCp, methodrefInfo)
case *classfile.ConstantInterfaceMethodrefInfo:
methodrefInfo := cpInfo.(*classfile.ConstantInterfaceMethodrefInfo)
consts[i] = newInterfaceMethodRef(rtCp, methodrefInfo)
default:
// todo
}
}
return rtCp
}
func (self *ConstantPool) GetConstant(index uint) Constant {
if c := self.consts[index]; c != nil {
return c
}
panic(fmt.Sprintf("No constants at index %d", index))
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/constant_pool.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 517
|
```go
package heap
import "jvmgo/ch07/classfile"
type FieldRef struct {
MemberRef
field *Field
}
func newFieldRef(cp *ConstantPool, refInfo *classfile.ConstantFieldrefInfo) *FieldRef {
ref := &FieldRef{}
ref.cp = cp
ref.copyMemberRefInfo(&refInfo.ConstantMemberrefInfo)
return ref
}
func (self *FieldRef) ResolvedField() *Field {
if self.field == nil {
self.resolveFieldRef()
}
return self.field
}
// jvms 5.4.3.2
func (self *FieldRef) resolveFieldRef() {
d := self.cp.class
c := self.ResolvedClass()
field := lookupField(c, self.name, self.descriptor)
if field == nil {
panic("java.lang.NoSuchFieldError")
}
if !field.isAccessibleTo(d) {
panic("java.lang.IllegalAccessError")
}
self.field = field
}
func lookupField(c *Class, name, descriptor string) *Field {
for _, field := range c.fields {
if field.name == name && field.descriptor == descriptor {
return field
}
}
for _, iface := range c.interfaces {
if field := lookupField(iface, name, descriptor); field != nil {
return field
}
}
if c.superClass != nil {
return lookupField(c.superClass, name, descriptor)
}
return nil
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/cp_fieldref.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 308
|
```go
package heap
// jvms8 6.5.instanceof
// jvms8 6.5.checkcast
func (self *Class) isAssignableFrom(other *Class) bool {
s, t := other, self
if s == t {
return true
}
if !t.IsInterface() {
return s.IsSubClassOf(t)
} else {
return s.IsImplements(t)
}
}
// self extends c
func (self *Class) IsSubClassOf(other *Class) bool {
for c := self.superClass; c != nil; c = c.superClass {
if c == other {
return true
}
}
return false
}
// self implements iface
func (self *Class) IsImplements(iface *Class) bool {
for c := self; c != nil; c = c.superClass {
for _, i := range c.interfaces {
if i == iface || i.isSubInterfaceOf(iface) {
return true
}
}
}
return false
}
// self extends iface
func (self *Class) isSubInterfaceOf(iface *Class) bool {
for _, superInterface := range self.interfaces {
if superInterface == iface || superInterface.isSubInterfaceOf(iface) {
return true
}
}
return false
}
// c extends self
func (self *Class) IsSuperClassOf(other *Class) bool {
return other.IsSubClassOf(self)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/class_hierarchy.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 316
|
```go
package heap
import "jvmgo/ch07/classfile"
type ClassRef struct {
SymRef
}
func newClassRef(cp *ConstantPool, classInfo *classfile.ConstantClassInfo) *ClassRef {
ref := &ClassRef{}
ref.cp = cp
ref.className = classInfo.Name()
return ref
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/cp_classref.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 68
|
```go
package heap
import "strings"
type MethodDescriptorParser struct {
raw string
offset int
parsed *MethodDescriptor
}
func parseMethodDescriptor(descriptor string) *MethodDescriptor {
parser := &MethodDescriptorParser{}
return parser.parse(descriptor)
}
func (self *MethodDescriptorParser) parse(descriptor string) *MethodDescriptor {
self.raw = descriptor
self.parsed = &MethodDescriptor{}
self.startParams()
self.parseParamTypes()
self.endParams()
self.parseReturnType()
self.finish()
return self.parsed
}
func (self *MethodDescriptorParser) startParams() {
if self.readUint8() != '(' {
self.causePanic()
}
}
func (self *MethodDescriptorParser) endParams() {
if self.readUint8() != ')' {
self.causePanic()
}
}
func (self *MethodDescriptorParser) finish() {
if self.offset != len(self.raw) {
self.causePanic()
}
}
func (self *MethodDescriptorParser) causePanic() {
panic("BAD descriptor: " + self.raw)
}
func (self *MethodDescriptorParser) readUint8() uint8 {
b := self.raw[self.offset]
self.offset++
return b
}
func (self *MethodDescriptorParser) unreadUint8() {
self.offset--
}
func (self *MethodDescriptorParser) parseParamTypes() {
for {
t := self.parseFieldType()
if t != "" {
self.parsed.addParameterType(t)
} else {
break
}
}
}
func (self *MethodDescriptorParser) parseReturnType() {
if self.readUint8() == 'V' {
self.parsed.returnType = "V"
return
}
self.unreadUint8()
t := self.parseFieldType()
if t != "" {
self.parsed.returnType = t
return
}
self.causePanic()
}
func (self *MethodDescriptorParser) parseFieldType() string {
switch self.readUint8() {
case 'B':
return "B"
case 'C':
return "C"
case 'D':
return "D"
case 'F':
return "F"
case 'I':
return "I"
case 'J':
return "J"
case 'S':
return "S"
case 'Z':
return "Z"
case 'L':
return self.parseObjectType()
case '[':
return self.parseArrayType()
default:
self.unreadUint8()
return ""
}
}
func (self *MethodDescriptorParser) parseObjectType() string {
unread := self.raw[self.offset:]
semicolonIndex := strings.IndexRune(unread, ';')
if semicolonIndex == -1 {
self.causePanic()
return ""
} else {
objStart := self.offset - 1
objEnd := self.offset + semicolonIndex + 1
self.offset = objEnd
descriptor := self.raw[objStart:objEnd]
return descriptor
}
}
func (self *MethodDescriptorParser) parseArrayType() string {
arrStart := self.offset - 1
self.parseFieldType()
arrEnd := self.offset
descriptor := self.raw[arrStart:arrEnd]
return descriptor
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/method_descriptor_parser.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 675
|
```go
package heap
import "jvmgo/ch07/classfile"
type ClassMember struct {
accessFlags uint16
name string
descriptor string
class *Class
}
func (self *ClassMember) copyMemberInfo(memberInfo *classfile.MemberInfo) {
self.accessFlags = memberInfo.AccessFlags()
self.name = memberInfo.Name()
self.descriptor = memberInfo.Descriptor()
}
func (self *ClassMember) IsPublic() bool {
return 0 != self.accessFlags&ACC_PUBLIC
}
func (self *ClassMember) IsPrivate() bool {
return 0 != self.accessFlags&ACC_PRIVATE
}
func (self *ClassMember) IsProtected() bool {
return 0 != self.accessFlags&ACC_PROTECTED
}
func (self *ClassMember) IsStatic() bool {
return 0 != self.accessFlags&ACC_STATIC
}
func (self *ClassMember) IsFinal() bool {
return 0 != self.accessFlags&ACC_FINAL
}
func (self *ClassMember) IsSynthetic() bool {
return 0 != self.accessFlags&ACC_SYNTHETIC
}
// getters
func (self *ClassMember) Name() string {
return self.name
}
func (self *ClassMember) Descriptor() string {
return self.descriptor
}
func (self *ClassMember) Class() *Class {
return self.class
}
// jvms 5.4.4
func (self *ClassMember) isAccessibleTo(d *Class) bool {
if self.IsPublic() {
return true
}
c := self.class
if self.IsProtected() {
return d == c || d.IsSubClassOf(c) ||
c.GetPackageName() == d.GetPackageName()
}
if !self.IsPrivate() {
return c.GetPackageName() == d.GetPackageName()
}
return d == c
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/class_member.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 387
|
```go
package heap
type Object struct {
class *Class
fields Slots
}
func newObject(class *Class) *Object {
return &Object{
class: class,
fields: newSlots(class.instanceSlotCount),
}
}
// getters
func (self *Object) Class() *Class {
return self.class
}
func (self *Object) Fields() Slots {
return self.fields
}
func (self *Object) IsInstanceOf(class *Class) bool {
return class.isAssignableFrom(self.class)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/object.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 106
|
```go
package heap
import "jvmgo/ch07/classfile"
type MemberRef struct {
SymRef
name string
descriptor string
}
func (self *MemberRef) copyMemberRefInfo(refInfo *classfile.ConstantMemberrefInfo) {
self.className = refInfo.ClassName()
self.name, self.descriptor = refInfo.NameAndDescriptor()
}
func (self *MemberRef) Name() string {
return self.name
}
func (self *MemberRef) Descriptor() string {
return self.descriptor
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/cp_memberref.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 107
|
```go
package heap
type MethodDescriptor struct {
parameterTypes []string
returnType string
}
func (self *MethodDescriptor) addParameterType(t string) {
pLen := len(self.parameterTypes)
if pLen == cap(self.parameterTypes) {
s := make([]string, pLen, pLen+4)
copy(s, self.parameterTypes)
self.parameterTypes = s
}
self.parameterTypes = append(self.parameterTypes, t)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/method_descriptor.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 97
|
```go
package heap
import "jvmgo/ch07/classfile"
type Method struct {
ClassMember
maxStack uint
maxLocals uint
code []byte
argSlotCount uint
}
func newMethods(class *Class, cfMethods []*classfile.MemberInfo) []*Method {
methods := make([]*Method, len(cfMethods))
for i, cfMethod := range cfMethods {
methods[i] = &Method{}
methods[i].class = class
methods[i].copyMemberInfo(cfMethod)
methods[i].copyAttributes(cfMethod)
methods[i].calcArgSlotCount()
}
return methods
}
func (self *Method) copyAttributes(cfMethod *classfile.MemberInfo) {
if codeAttr := cfMethod.CodeAttribute(); codeAttr != nil {
self.maxStack = codeAttr.MaxStack()
self.maxLocals = codeAttr.MaxLocals()
self.code = codeAttr.Code()
}
}
func (self *Method) calcArgSlotCount() {
parsedDescriptor := parseMethodDescriptor(self.descriptor)
for _, paramType := range parsedDescriptor.parameterTypes {
self.argSlotCount++
if paramType == "J" || paramType == "D" {
self.argSlotCount++
}
}
if !self.IsStatic() {
self.argSlotCount++ // `this` reference
}
}
func (self *Method) IsSynchronized() bool {
return 0 != self.accessFlags&ACC_SYNCHRONIZED
}
func (self *Method) IsBridge() bool {
return 0 != self.accessFlags&ACC_BRIDGE
}
func (self *Method) IsVarargs() bool {
return 0 != self.accessFlags&ACC_VARARGS
}
func (self *Method) IsNative() bool {
return 0 != self.accessFlags&ACC_NATIVE
}
func (self *Method) IsAbstract() bool {
return 0 != self.accessFlags&ACC_ABSTRACT
}
func (self *Method) IsStrict() bool {
return 0 != self.accessFlags&ACC_STRICT
}
// getters
func (self *Method) MaxStack() uint {
return self.maxStack
}
func (self *Method) MaxLocals() uint {
return self.maxLocals
}
func (self *Method) Code() []byte {
return self.code
}
func (self *Method) ArgSlotCount() uint {
return self.argSlotCount
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/method.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 510
|
```go
package heap
const (
ACC_PUBLIC = 0x0001 // class field method
ACC_PRIVATE = 0x0002 // field method
ACC_PROTECTED = 0x0004 // field method
ACC_STATIC = 0x0008 // field method
ACC_FINAL = 0x0010 // class field method
ACC_SUPER = 0x0020 // class
ACC_SYNCHRONIZED = 0x0020 // method
ACC_VOLATILE = 0x0040 // field
ACC_BRIDGE = 0x0040 // method
ACC_TRANSIENT = 0x0080 // field
ACC_VARARGS = 0x0080 // method
ACC_NATIVE = 0x0100 // method
ACC_INTERFACE = 0x0200 // class
ACC_ABSTRACT = 0x0400 // class method
ACC_STRICT = 0x0800 // method
ACC_SYNTHETIC = 0x1000 // class field method
ACC_ANNOTATION = 0x2000 // class
ACC_ENUM = 0x4000 // class field
)
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/access_flags.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 278
|
```go
package heap
func LookupMethodInClass(class *Class, name, descriptor string) *Method {
for c := class; c != nil; c = c.superClass {
for _, method := range c.methods {
if method.name == name && method.descriptor == descriptor {
return method
}
}
}
return nil
}
func lookupMethodInInterfaces(ifaces []*Class, name, descriptor string) *Method {
for _, iface := range ifaces {
for _, method := range iface.methods {
if method.name == name && method.descriptor == descriptor {
return method
}
}
method := lookupMethodInInterfaces(iface.interfaces, name, descriptor)
if method != nil {
return method
}
}
return nil
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/method_lookup.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 164
|
```go
package heap
import "fmt"
import "jvmgo/ch07/classfile"
import "jvmgo/ch07/classpath"
/*
class names:
- primitive types: boolean, byte, int ...
- primitive arrays: [Z, [B, [I ...
- non-array classes: java/lang/Object ...
- array classes: [Ljava/lang/Object; ...
*/
type ClassLoader struct {
cp *classpath.Classpath
verboseFlag bool
classMap map[string]*Class // loaded classes
}
func NewClassLoader(cp *classpath.Classpath, verboseFlag bool) *ClassLoader {
return &ClassLoader{
cp: cp,
verboseFlag: verboseFlag,
classMap: make(map[string]*Class),
}
}
func (self *ClassLoader) LoadClass(name string) *Class {
if class, ok := self.classMap[name]; ok {
// already loaded
return class
}
return self.loadNonArrayClass(name)
}
func (self *ClassLoader) loadNonArrayClass(name string) *Class {
data, entry := self.readClass(name)
class := self.defineClass(data)
link(class)
if self.verboseFlag {
fmt.Printf("[Loaded %s from %s]\n", name, entry)
}
return class
}
func (self *ClassLoader) readClass(name string) ([]byte, classpath.Entry) {
data, entry, err := self.cp.ReadClass(name)
if err != nil {
panic("java.lang.ClassNotFoundException: " + name)
}
return data, entry
}
// jvms 5.3.5
func (self *ClassLoader) defineClass(data []byte) *Class {
class := parseClass(data)
class.loader = self
resolveSuperClass(class)
resolveInterfaces(class)
self.classMap[class.name] = class
return class
}
func parseClass(data []byte) *Class {
cf, err := classfile.Parse(data)
if err != nil {
//panic("java.lang.ClassFormatError")
panic(err)
}
return newClass(cf)
}
// jvms 5.4.3.1
func resolveSuperClass(class *Class) {
if class.name != "java/lang/Object" {
class.superClass = class.loader.LoadClass(class.superClassName)
}
}
func resolveInterfaces(class *Class) {
interfaceCount := len(class.interfaceNames)
if interfaceCount > 0 {
class.interfaces = make([]*Class, interfaceCount)
for i, interfaceName := range class.interfaceNames {
class.interfaces[i] = class.loader.LoadClass(interfaceName)
}
}
}
func link(class *Class) {
verify(class)
prepare(class)
}
func verify(class *Class) {
// todo
}
// jvms 5.4.2
func prepare(class *Class) {
calcInstanceFieldSlotIds(class)
calcStaticFieldSlotIds(class)
allocAndInitStaticVars(class)
}
func calcInstanceFieldSlotIds(class *Class) {
slotId := uint(0)
if class.superClass != nil {
slotId = class.superClass.instanceSlotCount
}
for _, field := range class.fields {
if !field.IsStatic() {
field.slotId = slotId
slotId++
if field.isLongOrDouble() {
slotId++
}
}
}
class.instanceSlotCount = slotId
}
func calcStaticFieldSlotIds(class *Class) {
slotId := uint(0)
for _, field := range class.fields {
if field.IsStatic() {
field.slotId = slotId
slotId++
if field.isLongOrDouble() {
slotId++
}
}
}
class.staticSlotCount = slotId
}
func allocAndInitStaticVars(class *Class) {
class.staticVars = newSlots(class.staticSlotCount)
for _, field := range class.fields {
if field.IsStatic() && field.IsFinal() {
initStaticFinalVar(class, field)
}
}
}
func initStaticFinalVar(class *Class, field *Field) {
vars := class.staticVars
cp := class.constantPool
cpIndex := field.ConstValueIndex()
slotId := field.SlotId()
if cpIndex > 0 {
switch field.Descriptor() {
case "Z", "B", "C", "S", "I":
val := cp.GetConstant(cpIndex).(int32)
vars.SetInt(slotId, val)
case "J":
val := cp.GetConstant(cpIndex).(int64)
vars.SetLong(slotId, val)
case "F":
val := cp.GetConstant(cpIndex).(float32)
vars.SetFloat(slotId, val)
case "D":
val := cp.GetConstant(cpIndex).(float64)
vars.SetDouble(slotId, val)
case "Ljava/lang/String;":
panic("todo")
}
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/class_loader.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 1,035
|
```go
package heap
import "jvmgo/ch07/classfile"
type InterfaceMethodRef struct {
MemberRef
method *Method
}
func newInterfaceMethodRef(cp *ConstantPool, refInfo *classfile.ConstantInterfaceMethodrefInfo) *InterfaceMethodRef {
ref := &InterfaceMethodRef{}
ref.cp = cp
ref.copyMemberRefInfo(&refInfo.ConstantMemberrefInfo)
return ref
}
func (self *InterfaceMethodRef) ResolvedInterfaceMethod() *Method {
if self.method == nil {
self.resolveInterfaceMethodRef()
}
return self.method
}
// jvms8 5.4.3.4
func (self *InterfaceMethodRef) resolveInterfaceMethodRef() {
d := self.cp.class
c := self.ResolvedClass()
if !c.IsInterface() {
panic("java.lang.IncompatibleClassChangeError")
}
method := lookupInterfaceMethod(c, self.name, self.descriptor)
if method == nil {
panic("java.lang.NoSuchMethodError")
}
if !method.isAccessibleTo(d) {
panic("java.lang.IllegalAccessError")
}
self.method = method
}
// todo
func lookupInterfaceMethod(iface *Class, name, descriptor string) *Method {
for _, method := range iface.methods {
if method.name == name && method.descriptor == descriptor {
return method
}
}
return lookupMethodInInterfaces(iface.interfaces, name, descriptor)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/cp_interface_methodref.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 300
|
```go
package heap
import "math"
type Slot struct {
num int32
ref *Object
}
type Slots []Slot
func newSlots(slotCount uint) Slots {
if slotCount > 0 {
return make([]Slot, slotCount)
}
return nil
}
func (self Slots) SetInt(index uint, val int32) {
self[index].num = val
}
func (self Slots) GetInt(index uint) int32 {
return self[index].num
}
func (self Slots) SetFloat(index uint, val float32) {
bits := math.Float32bits(val)
self[index].num = int32(bits)
}
func (self Slots) GetFloat(index uint) float32 {
bits := uint32(self[index].num)
return math.Float32frombits(bits)
}
// long consumes two slots
func (self Slots) SetLong(index uint, val int64) {
self[index].num = int32(val)
self[index+1].num = int32(val >> 32)
}
func (self Slots) GetLong(index uint) int64 {
low := uint32(self[index].num)
high := uint32(self[index+1].num)
return int64(high)<<32 | int64(low)
}
// double consumes two slots
func (self Slots) SetDouble(index uint, val float64) {
bits := math.Float64bits(val)
self.SetLong(index, int64(bits))
}
func (self Slots) GetDouble(index uint) float64 {
bits := uint64(self.GetLong(index))
return math.Float64frombits(bits)
}
func (self Slots) SetRef(index uint, ref *Object) {
self[index].ref = ref
}
func (self Slots) GetRef(index uint) *Object {
return self[index].ref
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/slots.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 373
|
```go
package heap
import "jvmgo/ch07/classfile"
type Field struct {
ClassMember
constValueIndex uint
slotId uint
}
func newFields(class *Class, cfFields []*classfile.MemberInfo) []*Field {
fields := make([]*Field, len(cfFields))
for i, cfField := range cfFields {
fields[i] = &Field{}
fields[i].class = class
fields[i].copyMemberInfo(cfField)
fields[i].copyAttributes(cfField)
}
return fields
}
func (self *Field) copyAttributes(cfField *classfile.MemberInfo) {
if valAttr := cfField.ConstantValueAttribute(); valAttr != nil {
self.constValueIndex = uint(valAttr.ConstantValueIndex())
}
}
func (self *Field) IsVolatile() bool {
return 0 != self.accessFlags&ACC_VOLATILE
}
func (self *Field) IsTransient() bool {
return 0 != self.accessFlags&ACC_TRANSIENT
}
func (self *Field) IsEnum() bool {
return 0 != self.accessFlags&ACC_ENUM
}
func (self *Field) ConstValueIndex() uint {
return self.constValueIndex
}
func (self *Field) SlotId() uint {
return self.slotId
}
func (self *Field) isLongOrDouble() bool {
return self.descriptor == "J" || self.descriptor == "D"
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/rtda/heap/field.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 296
|
```go
package classpath
import "os"
import "strings"
// :(linux/unix) or ;(windows)
const pathListSeparator = string(os.PathListSeparator)
type Entry interface {
// className: fully/qualified/ClassName.class
readClass(className string) ([]byte, Entry, error)
String() string
}
func newEntry(path string) Entry {
if strings.Contains(path, pathListSeparator) {
return newCompositeEntry(path)
}
if strings.HasSuffix(path, "*") {
return newWildcardEntry(path)
}
if strings.HasSuffix(path, ".jar") || strings.HasSuffix(path, ".JAR") ||
strings.HasSuffix(path, ".zip") || strings.HasSuffix(path, ".ZIP") {
return newZipEntry(path)
}
return newDirEntry(path)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classpath/entry.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 168
|
```go
package classpath
import "errors"
import "strings"
type CompositeEntry []Entry
func newCompositeEntry(pathList string) CompositeEntry {
compositeEntry := []Entry{}
for _, path := range strings.Split(pathList, pathListSeparator) {
entry := newEntry(path)
compositeEntry = append(compositeEntry, entry)
}
return compositeEntry
}
func (self CompositeEntry) readClass(className string) ([]byte, Entry, error) {
for _, entry := range self {
data, from, err := entry.readClass(className)
if err == nil {
return data, from, nil
}
}
return nil, nil, errors.New("class not found: " + className)
}
func (self CompositeEntry) String() string {
strs := make([]string, len(self))
for i, entry := range self {
strs[i] = entry.String()
}
return strings.Join(strs, pathListSeparator)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classpath/entry_composite.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 202
|
```go
package classpath
import "os"
import "path/filepath"
type Classpath struct {
bootClasspath Entry
extClasspath Entry
userClasspath Entry
}
func Parse(jreOption, cpOption string) *Classpath {
cp := &Classpath{}
cp.parseBootAndExtClasspath(jreOption)
cp.parseUserClasspath(cpOption)
return cp
}
func (self *Classpath) parseBootAndExtClasspath(jreOption string) {
jreDir := getJreDir(jreOption)
// jre/lib/*
jreLibPath := filepath.Join(jreDir, "lib", "*")
self.bootClasspath = newWildcardEntry(jreLibPath)
// jre/lib/ext/*
jreExtPath := filepath.Join(jreDir, "lib", "ext", "*")
self.extClasspath = newWildcardEntry(jreExtPath)
}
func getJreDir(jreOption string) string {
if jreOption != "" && exists(jreOption) {
return jreOption
}
if exists("./jre") {
return "./jre"
}
if jh := os.Getenv("JAVA_HOME"); jh != "" {
return filepath.Join(jh, "jre")
}
panic("Can not find jre folder!")
}
func exists(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func (self *Classpath) parseUserClasspath(cpOption string) {
if cpOption == "" {
cpOption = "."
}
self.userClasspath = newEntry(cpOption)
}
// className: fully/qualified/ClassName
func (self *Classpath) ReadClass(className string) ([]byte, Entry, error) {
className = className + ".class"
if data, entry, err := self.bootClasspath.readClass(className); err == nil {
return data, entry, err
}
if data, entry, err := self.extClasspath.readClass(className); err == nil {
return data, entry, err
}
return self.userClasspath.readClass(className)
}
func (self *Classpath) String() string {
return self.userClasspath.String()
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classpath/classpath.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 476
|
```go
package classpath
import "archive/zip"
import "errors"
import "io/ioutil"
import "path/filepath"
type ZipEntry struct {
absPath string
zipRC *zip.ReadCloser
}
func newZipEntry(path string) *ZipEntry {
absPath, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return &ZipEntry{absPath, nil}
}
func (self *ZipEntry) readClass(className string) ([]byte, Entry, error) {
if self.zipRC == nil {
err := self.openJar()
if err != nil {
return nil, nil, err
}
}
classFile := self.findClass(className)
if classFile == nil {
return nil, nil, errors.New("class not found: " + className)
}
data, err := readClass(classFile)
return data, self, err
}
// todo: close zip
func (self *ZipEntry) openJar() error {
r, err := zip.OpenReader(self.absPath)
if err == nil {
self.zipRC = r
}
return err
}
func (self *ZipEntry) findClass(className string) *zip.File {
for _, f := range self.zipRC.File {
if f.Name == className {
return f
}
}
return nil
}
func readClass(classFile *zip.File) ([]byte, error) {
rc, err := classFile.Open()
if err != nil {
return nil, err
}
// read class data
data, err := ioutil.ReadAll(rc)
rc.Close()
if err != nil {
return nil, err
}
return data, nil
}
func (self *ZipEntry) String() string {
return self.absPath
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classpath/entry_zip.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 374
|
```go
package classpath
import "os"
import "path/filepath"
import "strings"
func newWildcardEntry(path string) CompositeEntry {
baseDir := path[:len(path)-1] // remove *
compositeEntry := []Entry{}
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && path != baseDir {
return filepath.SkipDir
}
if strings.HasSuffix(path, ".jar") || strings.HasSuffix(path, ".JAR") {
jarEntry := newZipEntry(path)
compositeEntry = append(compositeEntry, jarEntry)
}
return nil
}
filepath.Walk(baseDir, walkFn)
return compositeEntry
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classpath/entry_wildcard.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 168
|
```go
package classpath
import "io/ioutil"
import "path/filepath"
type DirEntry struct {
absDir string
}
func newDirEntry(path string) *DirEntry {
absDir, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return &DirEntry{absDir}
}
func (self *DirEntry) readClass(className string) ([]byte, Entry, error) {
fileName := filepath.Join(self.absDir, className)
data, err := ioutil.ReadFile(fileName)
return data, self, err
}
func (self *DirEntry) String() string {
return self.absDir
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch07/classpath/entry_dir.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 131
|
```go
package main
import "flag"
import "fmt"
import "os"
// java [-options] class [args...]
type Cmd struct {
helpFlag bool
versionFlag bool
cpOption string
XjreOption string
class string
args []string
}
func parseCmd() *Cmd {
cmd := &Cmd{}
flag.Usage = printUsage
flag.BoolVar(&cmd.helpFlag, "help", false, "print help message")
flag.BoolVar(&cmd.helpFlag, "?", false, "print help message")
flag.BoolVar(&cmd.versionFlag, "version", false, "print version and exit")
flag.StringVar(&cmd.cpOption, "classpath", "", "classpath")
flag.StringVar(&cmd.cpOption, "cp", "", "classpath")
flag.StringVar(&cmd.XjreOption, "Xjre", "", "path to jre")
flag.Parse()
args := flag.Args()
if len(args) > 0 {
cmd.class = args[0]
cmd.args = args[1:]
}
return cmd
}
func printUsage() {
fmt.Printf("Usage: %s [-options] class [args...]\n", os.Args[0])
//flag.PrintDefaults()
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/cmd.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 254
|
```go
package main
import "fmt"
import "strings"
import "jvmgo/ch05/classfile"
import "jvmgo/ch05/classpath"
func main() {
cmd := parseCmd()
if cmd.versionFlag {
fmt.Println("version 0.0.1")
} else if cmd.helpFlag || cmd.class == "" {
printUsage()
} else {
startJVM(cmd)
}
}
func startJVM(cmd *Cmd) {
cp := classpath.Parse(cmd.XjreOption, cmd.cpOption)
className := strings.Replace(cmd.class, ".", "/", -1)
cf := loadClass(className, cp)
mainMethod := getMainMethod(cf)
if mainMethod != nil {
interpret(mainMethod)
} else {
fmt.Printf("Main method not found in class %s\n", cmd.class)
}
}
func loadClass(className string, cp *classpath.Classpath) *classfile.ClassFile {
classData, _, err := cp.ReadClass(className)
if err != nil {
panic(err)
}
cf, err := classfile.Parse(classData)
if err != nil {
panic(err)
}
return cf
}
func getMainMethod(cf *classfile.ClassFile) *classfile.MemberInfo {
for _, m := range cf.Methods() {
if m.Name() == "main" && m.Descriptor() == "([Ljava/lang/String;)V" {
return m
}
}
return nil
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/main.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 305
|
```go
package main
import "fmt"
import "jvmgo/ch05/classfile"
import "jvmgo/ch05/instructions"
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
func interpret(methodInfo *classfile.MemberInfo) {
codeAttr := methodInfo.CodeAttribute()
maxLocals := codeAttr.MaxLocals()
maxStack := codeAttr.MaxStack()
bytecode := codeAttr.Code()
thread := rtda.NewThread()
frame := thread.NewFrame(maxLocals, maxStack)
thread.PushFrame(frame)
defer catchErr(frame)
loop(thread, bytecode)
}
func catchErr(frame *rtda.Frame) {
if r := recover(); r != nil {
fmt.Printf("LocalVars:%v\n", frame.LocalVars())
fmt.Printf("OperandStack:%v\n", frame.OperandStack())
panic(r)
}
}
func loop(thread *rtda.Thread, bytecode []byte) {
frame := thread.PopFrame()
reader := &base.BytecodeReader{}
for {
pc := frame.NextPC()
thread.SetPC(pc)
// decode
reader.Reset(bytecode, pc)
opcode := reader.ReadUint8()
inst := instructions.NewInstruction(opcode)
inst.FetchOperands(reader)
frame.SetNextPC(reader.PC())
// execute
fmt.Printf("pc:%2d inst:%T %v\n", pc, inst, inst)
inst.Execute(frame)
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/interpreter.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 309
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Boolean AND int
type IAND struct{ base.NoOperandsInstruction }
func (self *IAND) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
result := v1 & v2
stack.PushInt(result)
}
// Boolean AND long
type LAND struct{ base.NoOperandsInstruction }
func (self *LAND) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
result := v1 & v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/and.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 158
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Add double
type DADD struct{ base.NoOperandsInstruction }
func (self *DADD) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v1 := stack.PopDouble()
v2 := stack.PopDouble()
result := v1 + v2
stack.PushDouble(result)
}
// Add float
type FADD struct{ base.NoOperandsInstruction }
func (self *FADD) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopFloat()
v1 := stack.PopFloat()
result := v1 + v2
stack.PushFloat(result)
}
// Add int
type IADD struct{ base.NoOperandsInstruction }
func (self *IADD) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
result := v1 + v2
stack.PushInt(result)
}
// Add long
type LADD struct{ base.NoOperandsInstruction }
func (self *LADD) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
result := v1 + v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/add.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 290
|
```go
package math
import "math"
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Remainder double
type DREM struct{ base.NoOperandsInstruction }
func (self *DREM) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopDouble()
v1 := stack.PopDouble()
result := math.Mod(v1, v2) // todo
stack.PushDouble(result)
}
// Remainder float
type FREM struct{ base.NoOperandsInstruction }
func (self *FREM) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopFloat()
v1 := stack.PopFloat()
result := float32(math.Mod(float64(v1), float64(v2))) // todo
stack.PushFloat(result)
}
// Remainder int
type IREM struct{ base.NoOperandsInstruction }
func (self *IREM) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
if v2 == 0 {
panic("java.lang.ArithmeticException: / by zero")
}
result := v1 % v2
stack.PushInt(result)
}
// Remainder long
type LREM struct{ base.NoOperandsInstruction }
func (self *LREM) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
if v2 == 0 {
panic("java.lang.ArithmeticException: / by zero")
}
result := v1 % v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/rem.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 358
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Shift left int
type ISHL struct{ base.NoOperandsInstruction }
func (self *ISHL) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
s := uint32(v2) & 0x1f
result := v1 << s
stack.PushInt(result)
}
// Arithmetic shift right int
type ISHR struct{ base.NoOperandsInstruction }
func (self *ISHR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
s := uint32(v2) & 0x1f
result := v1 >> s
stack.PushInt(result)
}
// Logical shift right int
type IUSHR struct{ base.NoOperandsInstruction }
func (self *IUSHR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
s := uint32(v2) & 0x1f
result := int32(uint32(v1) >> s)
stack.PushInt(result)
}
// Shift left long
type LSHL struct{ base.NoOperandsInstruction }
func (self *LSHL) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopLong()
s := uint32(v2) & 0x3f
result := v1 << s
stack.PushLong(result)
}
// Arithmetic shift right long
type LSHR struct{ base.NoOperandsInstruction }
func (self *LSHR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopLong()
s := uint32(v2) & 0x3f
result := v1 >> s
stack.PushLong(result)
}
// Logical shift right long
type LUSHR struct{ base.NoOperandsInstruction }
func (self *LUSHR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopLong()
s := uint32(v2) & 0x3f
result := int64(uint64(v1) >> s)
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/sh.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 526
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Boolean OR int
type IOR struct{ base.NoOperandsInstruction }
func (self *IOR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
result := v1 | v2
stack.PushInt(result)
}
// Boolean OR long
type LOR struct{ base.NoOperandsInstruction }
func (self *LOR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
result := v1 | v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/or.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 159
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Divide double
type DDIV struct{ base.NoOperandsInstruction }
func (self *DDIV) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopDouble()
v1 := stack.PopDouble()
result := v1 / v2
stack.PushDouble(result)
}
// Divide float
type FDIV struct{ base.NoOperandsInstruction }
func (self *FDIV) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopFloat()
v1 := stack.PopFloat()
result := v1 / v2
stack.PushFloat(result)
}
// Divide int
type IDIV struct{ base.NoOperandsInstruction }
func (self *IDIV) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
if v2 == 0 {
panic("java.lang.ArithmeticException: / by zero")
}
result := v1 / v2
stack.PushInt(result)
}
// Divide long
type LDIV struct{ base.NoOperandsInstruction }
func (self *LDIV) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
if v2 == 0 {
panic("java.lang.ArithmeticException: / by zero")
}
result := v1 / v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/div.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 334
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Subtract double
type DSUB struct{ base.NoOperandsInstruction }
func (self *DSUB) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopDouble()
v1 := stack.PopDouble()
result := v1 - v2
stack.PushDouble(result)
}
// Subtract float
type FSUB struct{ base.NoOperandsInstruction }
func (self *FSUB) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopFloat()
v1 := stack.PopFloat()
result := v1 - v2
stack.PushFloat(result)
}
// Subtract int
type ISUB struct{ base.NoOperandsInstruction }
func (self *ISUB) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
result := v1 - v2
stack.PushInt(result)
}
// Subtract long
type LSUB struct{ base.NoOperandsInstruction }
func (self *LSUB) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
result := v1 - v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/sub.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 290
|
```go
package instructions
import "fmt"
import "jvmgo/ch05/instructions/base"
import . "jvmgo/ch05/instructions/comparisons"
import . "jvmgo/ch05/instructions/constants"
import . "jvmgo/ch05/instructions/control"
import . "jvmgo/ch05/instructions/conversions"
import . "jvmgo/ch05/instructions/extended"
import . "jvmgo/ch05/instructions/loads"
import . "jvmgo/ch05/instructions/math"
import . "jvmgo/ch05/instructions/stack"
import . "jvmgo/ch05/instructions/stores"
// NoOperandsInstruction singletons
var (
nop = &NOP{}
aconst_null = &ACONST_NULL{}
iconst_m1 = &ICONST_M1{}
iconst_0 = &ICONST_0{}
iconst_1 = &ICONST_1{}
iconst_2 = &ICONST_2{}
iconst_3 = &ICONST_3{}
iconst_4 = &ICONST_4{}
iconst_5 = &ICONST_5{}
lconst_0 = &LCONST_0{}
lconst_1 = &LCONST_1{}
fconst_0 = &FCONST_0{}
fconst_1 = &FCONST_1{}
fconst_2 = &FCONST_2{}
dconst_0 = &DCONST_0{}
dconst_1 = &DCONST_1{}
iload_0 = &ILOAD_0{}
iload_1 = &ILOAD_1{}
iload_2 = &ILOAD_2{}
iload_3 = &ILOAD_3{}
lload_0 = &LLOAD_0{}
lload_1 = &LLOAD_1{}
lload_2 = &LLOAD_2{}
lload_3 = &LLOAD_3{}
fload_0 = &FLOAD_0{}
fload_1 = &FLOAD_1{}
fload_2 = &FLOAD_2{}
fload_3 = &FLOAD_3{}
dload_0 = &DLOAD_0{}
dload_1 = &DLOAD_1{}
dload_2 = &DLOAD_2{}
dload_3 = &DLOAD_3{}
aload_0 = &ALOAD_0{}
aload_1 = &ALOAD_1{}
aload_2 = &ALOAD_2{}
aload_3 = &ALOAD_3{}
// iaload = &IALOAD{}
// laload = &LALOAD{}
// faload = &FALOAD{}
// daload = &DALOAD{}
// aaload = &AALOAD{}
// baload = &BALOAD{}
// caload = &CALOAD{}
// saload = &SALOAD{}
istore_0 = &ISTORE_0{}
istore_1 = &ISTORE_1{}
istore_2 = &ISTORE_2{}
istore_3 = &ISTORE_3{}
lstore_0 = &LSTORE_0{}
lstore_1 = &LSTORE_1{}
lstore_2 = &LSTORE_2{}
lstore_3 = &LSTORE_3{}
fstore_0 = &FSTORE_0{}
fstore_1 = &FSTORE_1{}
fstore_2 = &FSTORE_2{}
fstore_3 = &FSTORE_3{}
dstore_0 = &DSTORE_0{}
dstore_1 = &DSTORE_1{}
dstore_2 = &DSTORE_2{}
dstore_3 = &DSTORE_3{}
astore_0 = &ASTORE_0{}
astore_1 = &ASTORE_1{}
astore_2 = &ASTORE_2{}
astore_3 = &ASTORE_3{}
// iastore = &IASTORE{}
// lastore = &LASTORE{}
// fastore = &FASTORE{}
// dastore = &DASTORE{}
// aastore = &AASTORE{}
// bastore = &BASTORE{}
// castore = &CASTORE{}
// sastore = &SASTORE{}
pop = &POP{}
pop2 = &POP2{}
dup = &DUP{}
dup_x1 = &DUP_X1{}
dup_x2 = &DUP_X2{}
dup2 = &DUP2{}
dup2_x1 = &DUP2_X1{}
dup2_x2 = &DUP2_X2{}
swap = &SWAP{}
iadd = &IADD{}
ladd = &LADD{}
fadd = &FADD{}
dadd = &DADD{}
isub = &ISUB{}
lsub = &LSUB{}
fsub = &FSUB{}
dsub = &DSUB{}
imul = &IMUL{}
lmul = &LMUL{}
fmul = &FMUL{}
dmul = &DMUL{}
idiv = &IDIV{}
ldiv = &LDIV{}
fdiv = &FDIV{}
ddiv = &DDIV{}
irem = &IREM{}
lrem = &LREM{}
frem = &FREM{}
drem = &DREM{}
ineg = &INEG{}
lneg = &LNEG{}
fneg = &FNEG{}
dneg = &DNEG{}
ishl = &ISHL{}
lshl = &LSHL{}
ishr = &ISHR{}
lshr = &LSHR{}
iushr = &IUSHR{}
lushr = &LUSHR{}
iand = &IAND{}
land = &LAND{}
ior = &IOR{}
lor = &LOR{}
ixor = &IXOR{}
lxor = &LXOR{}
i2l = &I2L{}
i2f = &I2F{}
i2d = &I2D{}
l2i = &L2I{}
l2f = &L2F{}
l2d = &L2D{}
f2i = &F2I{}
f2l = &F2L{}
f2d = &F2D{}
d2i = &D2I{}
d2l = &D2L{}
d2f = &D2F{}
i2b = &I2B{}
i2c = &I2C{}
i2s = &I2S{}
lcmp = &LCMP{}
fcmpl = &FCMPL{}
fcmpg = &FCMPG{}
dcmpl = &DCMPL{}
dcmpg = &DCMPG{}
// ireturn = &IRETURN{}
// lreturn = &LRETURN{}
// freturn = &FRETURN{}
// dreturn = &DRETURN{}
// areturn = &ARETURN{}
// _return = &RETURN{}
// arraylength = &ARRAY_LENGTH{}
// athrow = &ATHROW{}
// monitorenter = &MONITOR_ENTER{}
// monitorexit = &MONITOR_EXIT{}
// invoke_native = &INVOKE_NATIVE{}
)
func NewInstruction(opcode byte) base.Instruction {
switch opcode {
case 0x00:
return nop
case 0x01:
return aconst_null
case 0x02:
return iconst_m1
case 0x03:
return iconst_0
case 0x04:
return iconst_1
case 0x05:
return iconst_2
case 0x06:
return iconst_3
case 0x07:
return iconst_4
case 0x08:
return iconst_5
case 0x09:
return lconst_0
case 0x0a:
return lconst_1
case 0x0b:
return fconst_0
case 0x0c:
return fconst_1
case 0x0d:
return fconst_2
case 0x0e:
return dconst_0
case 0x0f:
return dconst_1
case 0x10:
return &BIPUSH{}
case 0x11:
return &SIPUSH{}
// case 0x12:
// return &LDC{}
// case 0x13:
// return &LDC_W{}
// case 0x14:
// return &LDC2_W{}
case 0x15:
return &ILOAD{}
case 0x16:
return &LLOAD{}
case 0x17:
return &FLOAD{}
case 0x18:
return &DLOAD{}
case 0x19:
return &ALOAD{}
case 0x1a:
return iload_0
case 0x1b:
return iload_1
case 0x1c:
return iload_2
case 0x1d:
return iload_3
case 0x1e:
return lload_0
case 0x1f:
return lload_1
case 0x20:
return lload_2
case 0x21:
return lload_3
case 0x22:
return fload_0
case 0x23:
return fload_1
case 0x24:
return fload_2
case 0x25:
return fload_3
case 0x26:
return dload_0
case 0x27:
return dload_1
case 0x28:
return dload_2
case 0x29:
return dload_3
case 0x2a:
return aload_0
case 0x2b:
return aload_1
case 0x2c:
return aload_2
case 0x2d:
return aload_3
// case 0x2e:
// return iaload
// case 0x2f:
// return laload
// case 0x30:
// return faload
// case 0x31:
// return daload
// case 0x32:
// return aaload
// case 0x33:
// return baload
// case 0x34:
// return caload
// case 0x35:
// return saload
case 0x36:
return &ISTORE{}
case 0x37:
return &LSTORE{}
case 0x38:
return &FSTORE{}
case 0x39:
return &DSTORE{}
case 0x3a:
return &ASTORE{}
case 0x3b:
return istore_0
case 0x3c:
return istore_1
case 0x3d:
return istore_2
case 0x3e:
return istore_3
case 0x3f:
return lstore_0
case 0x40:
return lstore_1
case 0x41:
return lstore_2
case 0x42:
return lstore_3
case 0x43:
return fstore_0
case 0x44:
return fstore_1
case 0x45:
return fstore_2
case 0x46:
return fstore_3
case 0x47:
return dstore_0
case 0x48:
return dstore_1
case 0x49:
return dstore_2
case 0x4a:
return dstore_3
case 0x4b:
return astore_0
case 0x4c:
return astore_1
case 0x4d:
return astore_2
case 0x4e:
return astore_3
// case 0x4f:
// return iastore
// case 0x50:
// return lastore
// case 0x51:
// return fastore
// case 0x52:
// return dastore
// case 0x53:
// return aastore
// case 0x54:
// return bastore
// case 0x55:
// return castore
// case 0x56:
// return sastore
case 0x57:
return pop
case 0x58:
return pop2
case 0x59:
return dup
case 0x5a:
return dup_x1
case 0x5b:
return dup_x2
case 0x5c:
return dup2
case 0x5d:
return dup2_x1
case 0x5e:
return dup2_x2
case 0x5f:
return swap
case 0x60:
return iadd
case 0x61:
return ladd
case 0x62:
return fadd
case 0x63:
return dadd
case 0x64:
return isub
case 0x65:
return lsub
case 0x66:
return fsub
case 0x67:
return dsub
case 0x68:
return imul
case 0x69:
return lmul
case 0x6a:
return fmul
case 0x6b:
return dmul
case 0x6c:
return idiv
case 0x6d:
return ldiv
case 0x6e:
return fdiv
case 0x6f:
return ddiv
case 0x70:
return irem
case 0x71:
return lrem
case 0x72:
return frem
case 0x73:
return drem
case 0x74:
return ineg
case 0x75:
return lneg
case 0x76:
return fneg
case 0x77:
return dneg
case 0x78:
return ishl
case 0x79:
return lshl
case 0x7a:
return ishr
case 0x7b:
return lshr
case 0x7c:
return iushr
case 0x7d:
return lushr
case 0x7e:
return iand
case 0x7f:
return land
case 0x80:
return ior
case 0x81:
return lor
case 0x82:
return ixor
case 0x83:
return lxor
case 0x84:
return &IINC{}
case 0x85:
return i2l
case 0x86:
return i2f
case 0x87:
return i2d
case 0x88:
return l2i
case 0x89:
return l2f
case 0x8a:
return l2d
case 0x8b:
return f2i
case 0x8c:
return f2l
case 0x8d:
return f2d
case 0x8e:
return d2i
case 0x8f:
return d2l
case 0x90:
return d2f
case 0x91:
return i2b
case 0x92:
return i2c
case 0x93:
return i2s
case 0x94:
return lcmp
case 0x95:
return fcmpl
case 0x96:
return fcmpg
case 0x97:
return dcmpl
case 0x98:
return dcmpg
case 0x99:
return &IFEQ{}
case 0x9a:
return &IFNE{}
case 0x9b:
return &IFLT{}
case 0x9c:
return &IFGE{}
case 0x9d:
return &IFGT{}
case 0x9e:
return &IFLE{}
case 0x9f:
return &IF_ICMPEQ{}
case 0xa0:
return &IF_ICMPNE{}
case 0xa1:
return &IF_ICMPLT{}
case 0xa2:
return &IF_ICMPGE{}
case 0xa3:
return &IF_ICMPGT{}
case 0xa4:
return &IF_ICMPLE{}
case 0xa5:
return &IF_ACMPEQ{}
case 0xa6:
return &IF_ACMPNE{}
case 0xa7:
return &GOTO{}
// case 0xa8:
// return &JSR{}
// case 0xa9:
// return &RET{}
case 0xaa:
return &TABLE_SWITCH{}
case 0xab:
return &LOOKUP_SWITCH{}
// case 0xac:
// return ireturn
// case 0xad:
// return lreturn
// case 0xae:
// return freturn
// case 0xaf:
// return dreturn
// case 0xb0:
// return areturn
// case 0xb1:
// return _return
// case 0xb2:
// return &GET_STATIC{}
// case 0xb3:
// return &PUT_STATIC{}
// case 0xb4:
// return &GET_FIELD{}
// case 0xb5:
// return &PUT_FIELD{}
// case 0xb6:
// return &INVOKE_VIRTUAL{}
// case 0xb7:
// return &INVOKE_SPECIAL{}
// case 0xb8:
// return &INVOKE_STATIC{}
// case 0xb9:
// return &INVOKE_INTERFACE{}
// case 0xba:
// return &INVOKE_DYNAMIC{}
// case 0xbb:
// return &NEW{}
// case 0xbc:
// return &NEW_ARRAY{}
// case 0xbd:
// return &ANEW_ARRAY{}
// case 0xbe:
// return arraylength
// case 0xbf:
// return athrow
// case 0xc0:
// return &CHECK_CAST{}
// case 0xc1:
// return &INSTANCE_OF{}
// case 0xc2:
// return monitorenter
// case 0xc3:
// return monitorexit
case 0xc4:
return &WIDE{}
// case 0xc5:
// return &MULTI_ANEW_ARRAY{}
case 0xc6:
return &IFNULL{}
case 0xc7:
return &IFNONNULL{}
case 0xc8:
return &GOTO_W{}
// case 0xc9:
// return &JSR_W{}
// case 0xca: breakpoint
// case 0xfe: impdep1
// case 0xff: impdep2
default:
panic(fmt.Errorf("Unsupported opcode: 0x%x!", opcode))
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/factory.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 4,403
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Negate double
type DNEG struct{ base.NoOperandsInstruction }
func (self *DNEG) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
val := stack.PopDouble()
stack.PushDouble(-val)
}
// Negate float
type FNEG struct{ base.NoOperandsInstruction }
func (self *FNEG) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
val := stack.PopFloat()
stack.PushFloat(-val)
}
// Negate int
type INEG struct{ base.NoOperandsInstruction }
func (self *INEG) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
val := stack.PopInt()
stack.PushInt(-val)
}
// Negate long
type LNEG struct{ base.NoOperandsInstruction }
func (self *LNEG) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
val := stack.PopLong()
stack.PushLong(-val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/neg.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 234
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Increment local variable by constant
type IINC struct {
Index uint
Const int32
}
func (self *IINC) FetchOperands(reader *base.BytecodeReader) {
self.Index = uint(reader.ReadUint8())
self.Const = int32(reader.ReadInt8())
}
func (self *IINC) Execute(frame *rtda.Frame) {
localVars := frame.LocalVars()
val := localVars.GetInt(self.Index)
val += self.Const
localVars.SetInt(self.Index, val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/iinc.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 132
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Boolean XOR int
type IXOR struct{ base.NoOperandsInstruction }
func (self *IXOR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v1 := stack.PopInt()
v2 := stack.PopInt()
result := v1 ^ v2
stack.PushInt(result)
}
// Boolean XOR long
type LXOR struct{ base.NoOperandsInstruction }
func (self *LXOR) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v1 := stack.PopLong()
v2 := stack.PopLong()
result := v1 ^ v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/xor.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 161
|
```go
package math
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Multiply double
type DMUL struct{ base.NoOperandsInstruction }
func (self *DMUL) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopDouble()
v1 := stack.PopDouble()
result := v1 * v2
stack.PushDouble(result)
}
// Multiply float
type FMUL struct{ base.NoOperandsInstruction }
func (self *FMUL) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopFloat()
v1 := stack.PopFloat()
result := v1 * v2
stack.PushFloat(result)
}
// Multiply int
type IMUL struct{ base.NoOperandsInstruction }
func (self *IMUL) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopInt()
v1 := stack.PopInt()
result := v1 * v2
stack.PushInt(result)
}
// Multiply long
type LMUL struct{ base.NoOperandsInstruction }
func (self *LMUL) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
result := v1 * v2
stack.PushLong(result)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/math/mul.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 290
|
```go
package stores
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Store float into local variable
type FSTORE struct{ base.Index8Instruction }
func (self *FSTORE) Execute(frame *rtda.Frame) {
_fstore(frame, uint(self.Index))
}
type FSTORE_0 struct{ base.NoOperandsInstruction }
func (self *FSTORE_0) Execute(frame *rtda.Frame) {
_fstore(frame, 0)
}
type FSTORE_1 struct{ base.NoOperandsInstruction }
func (self *FSTORE_1) Execute(frame *rtda.Frame) {
_fstore(frame, 1)
}
type FSTORE_2 struct{ base.NoOperandsInstruction }
func (self *FSTORE_2) Execute(frame *rtda.Frame) {
_fstore(frame, 2)
}
type FSTORE_3 struct{ base.NoOperandsInstruction }
func (self *FSTORE_3) Execute(frame *rtda.Frame) {
_fstore(frame, 3)
}
func _fstore(frame *rtda.Frame, index uint) {
val := frame.OperandStack().PopFloat()
frame.LocalVars().SetFloat(index, val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stores/fstore.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 265
|
```go
package stores
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Store double into local variable
type DSTORE struct{ base.Index8Instruction }
func (self *DSTORE) Execute(frame *rtda.Frame) {
_dstore(frame, uint(self.Index))
}
type DSTORE_0 struct{ base.NoOperandsInstruction }
func (self *DSTORE_0) Execute(frame *rtda.Frame) {
_dstore(frame, 0)
}
type DSTORE_1 struct{ base.NoOperandsInstruction }
func (self *DSTORE_1) Execute(frame *rtda.Frame) {
_dstore(frame, 1)
}
type DSTORE_2 struct{ base.NoOperandsInstruction }
func (self *DSTORE_2) Execute(frame *rtda.Frame) {
_dstore(frame, 2)
}
type DSTORE_3 struct{ base.NoOperandsInstruction }
func (self *DSTORE_3) Execute(frame *rtda.Frame) {
_dstore(frame, 3)
}
func _dstore(frame *rtda.Frame, index uint) {
val := frame.OperandStack().PopDouble()
frame.LocalVars().SetDouble(index, val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stores/dstore.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 265
|
```go
package stores
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Store reference into local variable
type ASTORE struct{ base.Index8Instruction }
func (self *ASTORE) Execute(frame *rtda.Frame) {
_astore(frame, uint(self.Index))
}
type ASTORE_0 struct{ base.NoOperandsInstruction }
func (self *ASTORE_0) Execute(frame *rtda.Frame) {
_astore(frame, 0)
}
type ASTORE_1 struct{ base.NoOperandsInstruction }
func (self *ASTORE_1) Execute(frame *rtda.Frame) {
_astore(frame, 1)
}
type ASTORE_2 struct{ base.NoOperandsInstruction }
func (self *ASTORE_2) Execute(frame *rtda.Frame) {
_astore(frame, 2)
}
type ASTORE_3 struct{ base.NoOperandsInstruction }
func (self *ASTORE_3) Execute(frame *rtda.Frame) {
_astore(frame, 3)
}
func _astore(frame *rtda.Frame, index uint) {
ref := frame.OperandStack().PopRef()
frame.LocalVars().SetRef(index, ref)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stores/astore.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 265
|
```go
package stores
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Store long into local variable
type LSTORE struct{ base.Index8Instruction }
func (self *LSTORE) Execute(frame *rtda.Frame) {
_lstore(frame, uint(self.Index))
}
type LSTORE_0 struct{ base.NoOperandsInstruction }
func (self *LSTORE_0) Execute(frame *rtda.Frame) {
_lstore(frame, 0)
}
type LSTORE_1 struct{ base.NoOperandsInstruction }
func (self *LSTORE_1) Execute(frame *rtda.Frame) {
_lstore(frame, 1)
}
type LSTORE_2 struct{ base.NoOperandsInstruction }
func (self *LSTORE_2) Execute(frame *rtda.Frame) {
_lstore(frame, 2)
}
type LSTORE_3 struct{ base.NoOperandsInstruction }
func (self *LSTORE_3) Execute(frame *rtda.Frame) {
_lstore(frame, 3)
}
func _lstore(frame *rtda.Frame, index uint) {
val := frame.OperandStack().PopLong()
frame.LocalVars().SetLong(index, val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stores/lstore.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 265
|
```go
package stores
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Store int into local variable
type ISTORE struct{ base.Index8Instruction }
func (self *ISTORE) Execute(frame *rtda.Frame) {
_istore(frame, uint(self.Index))
}
type ISTORE_0 struct{ base.NoOperandsInstruction }
func (self *ISTORE_0) Execute(frame *rtda.Frame) {
_istore(frame, 0)
}
type ISTORE_1 struct{ base.NoOperandsInstruction }
func (self *ISTORE_1) Execute(frame *rtda.Frame) {
_istore(frame, 1)
}
type ISTORE_2 struct{ base.NoOperandsInstruction }
func (self *ISTORE_2) Execute(frame *rtda.Frame) {
_istore(frame, 2)
}
type ISTORE_3 struct{ base.NoOperandsInstruction }
func (self *ISTORE_3) Execute(frame *rtda.Frame) {
_istore(frame, 3)
}
func _istore(frame *rtda.Frame, index uint) {
val := frame.OperandStack().PopInt()
frame.LocalVars().SetInt(index, val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stores/istore.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 270
|
```go
package base
import "jvmgo/ch05/rtda"
func Branch(frame *rtda.Frame, offset int) {
pc := frame.Thread().PC()
nextPC := pc + offset
frame.SetNextPC(nextPC)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/base/branch_logic.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 50
|
```go
package base
type BytecodeReader struct {
code []byte // bytecodes
pc int
}
func (self *BytecodeReader) Reset(code []byte, pc int) {
self.code = code
self.pc = pc
}
func (self *BytecodeReader) PC() int {
return self.pc
}
func (self *BytecodeReader) ReadInt8() int8 {
return int8(self.ReadUint8())
}
func (self *BytecodeReader) ReadUint8() uint8 {
i := self.code[self.pc]
self.pc++
return i
}
func (self *BytecodeReader) ReadInt16() int16 {
return int16(self.ReadUint16())
}
func (self *BytecodeReader) ReadUint16() uint16 {
byte1 := uint16(self.ReadUint8())
byte2 := uint16(self.ReadUint8())
return (byte1 << 8) | byte2
}
func (self *BytecodeReader) ReadInt32() int32 {
byte1 := int32(self.ReadUint8())
byte2 := int32(self.ReadUint8())
byte3 := int32(self.ReadUint8())
byte4 := int32(self.ReadUint8())
return (byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4
}
// used by lookupswitch and tableswitch
func (self *BytecodeReader) ReadInt32s(n int32) []int32 {
ints := make([]int32, n)
for i := range ints {
ints[i] = self.ReadInt32()
}
return ints
}
// used by lookupswitch and tableswitch
func (self *BytecodeReader) SkipPadding() {
for self.pc%4 != 0 {
self.ReadUint8()
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/base/bytecode_reader.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 376
|
```go
package loads
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Load reference from local variable
type ALOAD struct{ base.Index8Instruction }
func (self *ALOAD) Execute(frame *rtda.Frame) {
_aload(frame, self.Index)
}
type ALOAD_0 struct{ base.NoOperandsInstruction }
func (self *ALOAD_0) Execute(frame *rtda.Frame) {
_aload(frame, 0)
}
type ALOAD_1 struct{ base.NoOperandsInstruction }
func (self *ALOAD_1) Execute(frame *rtda.Frame) {
_aload(frame, 1)
}
type ALOAD_2 struct{ base.NoOperandsInstruction }
func (self *ALOAD_2) Execute(frame *rtda.Frame) {
_aload(frame, 2)
}
type ALOAD_3 struct{ base.NoOperandsInstruction }
func (self *ALOAD_3) Execute(frame *rtda.Frame) {
_aload(frame, 3)
}
func _aload(frame *rtda.Frame, index uint) {
ref := frame.LocalVars().GetRef(index)
frame.OperandStack().PushRef(ref)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/loads/aload.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 262
|
```go
package base
import "jvmgo/ch05/rtda"
type Instruction interface {
FetchOperands(reader *BytecodeReader)
Execute(frame *rtda.Frame)
}
type NoOperandsInstruction struct {
// empty
}
func (self *NoOperandsInstruction) FetchOperands(reader *BytecodeReader) {
// nothing to do
}
type BranchInstruction struct {
Offset int
}
func (self *BranchInstruction) FetchOperands(reader *BytecodeReader) {
self.Offset = int(reader.ReadInt16())
}
type Index8Instruction struct {
Index uint
}
func (self *Index8Instruction) FetchOperands(reader *BytecodeReader) {
self.Index = uint(reader.ReadUint8())
}
type Index16Instruction struct {
Index uint
}
func (self *Index16Instruction) FetchOperands(reader *BytecodeReader) {
self.Index = uint(reader.ReadUint16())
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/base/instruction.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 191
|
```go
package loads
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Load double from local variable
type DLOAD struct{ base.Index8Instruction }
func (self *DLOAD) Execute(frame *rtda.Frame) {
_dload(frame, self.Index)
}
type DLOAD_0 struct{ base.NoOperandsInstruction }
func (self *DLOAD_0) Execute(frame *rtda.Frame) {
_dload(frame, 0)
}
type DLOAD_1 struct{ base.NoOperandsInstruction }
func (self *DLOAD_1) Execute(frame *rtda.Frame) {
_dload(frame, 1)
}
type DLOAD_2 struct{ base.NoOperandsInstruction }
func (self *DLOAD_2) Execute(frame *rtda.Frame) {
_dload(frame, 2)
}
type DLOAD_3 struct{ base.NoOperandsInstruction }
func (self *DLOAD_3) Execute(frame *rtda.Frame) {
_dload(frame, 3)
}
func _dload(frame *rtda.Frame, index uint) {
val := frame.LocalVars().GetDouble(index)
frame.OperandStack().PushDouble(val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/loads/dload.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 263
|
```go
package loads
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Load float from local variable
type FLOAD struct{ base.Index8Instruction }
func (self *FLOAD) Execute(frame *rtda.Frame) {
_fload(frame, self.Index)
}
type FLOAD_0 struct{ base.NoOperandsInstruction }
func (self *FLOAD_0) Execute(frame *rtda.Frame) {
_fload(frame, 0)
}
type FLOAD_1 struct{ base.NoOperandsInstruction }
func (self *FLOAD_1) Execute(frame *rtda.Frame) {
_fload(frame, 1)
}
type FLOAD_2 struct{ base.NoOperandsInstruction }
func (self *FLOAD_2) Execute(frame *rtda.Frame) {
_fload(frame, 2)
}
type FLOAD_3 struct{ base.NoOperandsInstruction }
func (self *FLOAD_3) Execute(frame *rtda.Frame) {
_fload(frame, 3)
}
func _fload(frame *rtda.Frame, index uint) {
val := frame.LocalVars().GetFloat(index)
frame.OperandStack().PushFloat(val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/loads/fload.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 263
|
```go
package loads
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Load int from local variable
type ILOAD struct{ base.Index8Instruction }
func (self *ILOAD) Execute(frame *rtda.Frame) {
_iload(frame, self.Index)
}
type ILOAD_0 struct{ base.NoOperandsInstruction }
func (self *ILOAD_0) Execute(frame *rtda.Frame) {
_iload(frame, 0)
}
type ILOAD_1 struct{ base.NoOperandsInstruction }
func (self *ILOAD_1) Execute(frame *rtda.Frame) {
_iload(frame, 1)
}
type ILOAD_2 struct{ base.NoOperandsInstruction }
func (self *ILOAD_2) Execute(frame *rtda.Frame) {
_iload(frame, 2)
}
type ILOAD_3 struct{ base.NoOperandsInstruction }
func (self *ILOAD_3) Execute(frame *rtda.Frame) {
_iload(frame, 3)
}
func _iload(frame *rtda.Frame, index uint) {
val := frame.LocalVars().GetInt(index)
frame.OperandStack().PushInt(val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/loads/iload.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 262
|
```go
package loads
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Load long from local variable
type LLOAD struct{ base.Index8Instruction }
func (self *LLOAD) Execute(frame *rtda.Frame) {
_lload(frame, self.Index)
}
type LLOAD_0 struct{ base.NoOperandsInstruction }
func (self *LLOAD_0) Execute(frame *rtda.Frame) {
_lload(frame, 0)
}
type LLOAD_1 struct{ base.NoOperandsInstruction }
func (self *LLOAD_1) Execute(frame *rtda.Frame) {
_lload(frame, 1)
}
type LLOAD_2 struct{ base.NoOperandsInstruction }
func (self *LLOAD_2) Execute(frame *rtda.Frame) {
_lload(frame, 2)
}
type LLOAD_3 struct{ base.NoOperandsInstruction }
func (self *LLOAD_3) Execute(frame *rtda.Frame) {
_lload(frame, 3)
}
func _lload(frame *rtda.Frame, index uint) {
val := frame.LocalVars().GetLong(index)
frame.OperandStack().PushLong(val)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/loads/lload.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 263
|
```go
package extended
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Branch if reference is null
type IFNULL struct{ base.BranchInstruction }
func (self *IFNULL) Execute(frame *rtda.Frame) {
ref := frame.OperandStack().PopRef()
if ref == nil {
base.Branch(frame, self.Offset)
}
}
// Branch if reference not null
type IFNONNULL struct{ base.BranchInstruction }
func (self *IFNONNULL) Execute(frame *rtda.Frame) {
ref := frame.OperandStack().PopRef()
if ref != nil {
base.Branch(frame, self.Offset)
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/extended/ifnull.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 144
|
```go
package extended
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Branch always (wide index)
type GOTO_W struct {
offset int
}
func (self *GOTO_W) FetchOperands(reader *base.BytecodeReader) {
self.offset = int(reader.ReadInt32())
}
func (self *GOTO_W) Execute(frame *rtda.Frame) {
base.Branch(frame, self.offset)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/extended/goto_w.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 96
|
```go
package extended
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/instructions/loads"
import "jvmgo/ch05/instructions/math"
import "jvmgo/ch05/instructions/stores"
import "jvmgo/ch05/rtda"
// Extend local variable index by additional bytes
type WIDE struct {
modifiedInstruction base.Instruction
}
func (self *WIDE) FetchOperands(reader *base.BytecodeReader) {
opcode := reader.ReadUint8()
switch opcode {
case 0x15:
inst := &loads.ILOAD{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x16:
inst := &loads.LLOAD{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x17:
inst := &loads.FLOAD{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x18:
inst := &loads.DLOAD{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x19:
inst := &loads.ALOAD{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x36:
inst := &stores.ISTORE{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x37:
inst := &stores.LSTORE{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x38:
inst := &stores.FSTORE{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x39:
inst := &stores.DSTORE{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x3a:
inst := &stores.ASTORE{}
inst.Index = uint(reader.ReadUint16())
self.modifiedInstruction = inst
case 0x84:
inst := &math.IINC{}
inst.Index = uint(reader.ReadUint16())
inst.Const = int32(reader.ReadInt16())
self.modifiedInstruction = inst
case 0xa9: // ret
panic("Unsupported opcode: 0xa9!")
}
}
func (self *WIDE) Execute(frame *rtda.Frame) {
self.modifiedInstruction.Execute(frame)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/extended/wide.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 511
|
```go
package comparisons
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Compare long
type LCMP struct{ base.NoOperandsInstruction }
func (self *LCMP) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
v2 := stack.PopLong()
v1 := stack.PopLong()
if v1 > v2 {
stack.PushInt(1)
} else if v1 == v2 {
stack.PushInt(0)
} else {
stack.PushInt(-1)
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/comparisons/lcmp.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 123
|
```go
package comparisons
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Compare float
type FCMPG struct{ base.NoOperandsInstruction }
func (self *FCMPG) Execute(frame *rtda.Frame) {
_fcmp(frame, true)
}
type FCMPL struct{ base.NoOperandsInstruction }
func (self *FCMPL) Execute(frame *rtda.Frame) {
_fcmp(frame, false)
}
func _fcmp(frame *rtda.Frame, gFlag bool) {
stack := frame.OperandStack()
v2 := stack.PopFloat()
v1 := stack.PopFloat()
if v1 > v2 {
stack.PushInt(1)
} else if v1 == v2 {
stack.PushInt(0)
} else if v1 < v2 {
stack.PushInt(-1)
} else if gFlag {
stack.PushInt(1)
} else {
stack.PushInt(-1)
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/comparisons/fcmp.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 217
|
```go
package comparisons
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Branch if int comparison with zero succeeds
type IFEQ struct{ base.BranchInstruction }
func (self *IFEQ) Execute(frame *rtda.Frame) {
val := frame.OperandStack().PopInt()
if val == 0 {
base.Branch(frame, self.Offset)
}
}
type IFNE struct{ base.BranchInstruction }
func (self *IFNE) Execute(frame *rtda.Frame) {
val := frame.OperandStack().PopInt()
if val != 0 {
base.Branch(frame, self.Offset)
}
}
type IFLT struct{ base.BranchInstruction }
func (self *IFLT) Execute(frame *rtda.Frame) {
val := frame.OperandStack().PopInt()
if val < 0 {
base.Branch(frame, self.Offset)
}
}
type IFLE struct{ base.BranchInstruction }
func (self *IFLE) Execute(frame *rtda.Frame) {
val := frame.OperandStack().PopInt()
if val <= 0 {
base.Branch(frame, self.Offset)
}
}
type IFGT struct{ base.BranchInstruction }
func (self *IFGT) Execute(frame *rtda.Frame) {
val := frame.OperandStack().PopInt()
if val > 0 {
base.Branch(frame, self.Offset)
}
}
type IFGE struct{ base.BranchInstruction }
func (self *IFGE) Execute(frame *rtda.Frame) {
val := frame.OperandStack().PopInt()
if val >= 0 {
base.Branch(frame, self.Offset)
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/comparisons/ifcond.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 348
|
```go
package comparisons
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Compare double
type DCMPG struct{ base.NoOperandsInstruction }
func (self *DCMPG) Execute(frame *rtda.Frame) {
_dcmp(frame, true)
}
type DCMPL struct{ base.NoOperandsInstruction }
func (self *DCMPL) Execute(frame *rtda.Frame) {
_dcmp(frame, false)
}
func _dcmp(frame *rtda.Frame, gFlag bool) {
stack := frame.OperandStack()
v2 := stack.PopDouble()
v1 := stack.PopDouble()
if v1 > v2 {
stack.PushInt(1)
} else if v1 == v2 {
stack.PushInt(0)
} else if v1 < v2 {
stack.PushInt(-1)
} else if gFlag {
stack.PushInt(1)
} else {
stack.PushInt(-1)
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/comparisons/dcmp.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 217
|
```go
package comparisons
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Branch if reference comparison succeeds
type IF_ACMPEQ struct{ base.BranchInstruction }
func (self *IF_ACMPEQ) Execute(frame *rtda.Frame) {
if _acmp(frame) {
base.Branch(frame, self.Offset)
}
}
type IF_ACMPNE struct{ base.BranchInstruction }
func (self *IF_ACMPNE) Execute(frame *rtda.Frame) {
if !_acmp(frame) {
base.Branch(frame, self.Offset)
}
}
func _acmp(frame *rtda.Frame) bool {
stack := frame.OperandStack()
ref2 := stack.PopRef()
ref1 := stack.PopRef()
return ref1 == ref2 // todo
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/comparisons/if_acmp.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 173
|
```go
package constants
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Do nothing
type NOP struct{ base.NoOperandsInstruction }
func (self *NOP) Execute(frame *rtda.Frame) {
// really do nothing
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/constants/nop.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 62
|
```go
package comparisons
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Branch if int comparison succeeds
type IF_ICMPEQ struct{ base.BranchInstruction }
func (self *IF_ICMPEQ) Execute(frame *rtda.Frame) {
if val1, val2 := _icmpPop(frame); val1 == val2 {
base.Branch(frame, self.Offset)
}
}
type IF_ICMPNE struct{ base.BranchInstruction }
func (self *IF_ICMPNE) Execute(frame *rtda.Frame) {
if val1, val2 := _icmpPop(frame); val1 != val2 {
base.Branch(frame, self.Offset)
}
}
type IF_ICMPLT struct{ base.BranchInstruction }
func (self *IF_ICMPLT) Execute(frame *rtda.Frame) {
if val1, val2 := _icmpPop(frame); val1 < val2 {
base.Branch(frame, self.Offset)
}
}
type IF_ICMPLE struct{ base.BranchInstruction }
func (self *IF_ICMPLE) Execute(frame *rtda.Frame) {
if val1, val2 := _icmpPop(frame); val1 <= val2 {
base.Branch(frame, self.Offset)
}
}
type IF_ICMPGT struct{ base.BranchInstruction }
func (self *IF_ICMPGT) Execute(frame *rtda.Frame) {
if val1, val2 := _icmpPop(frame); val1 > val2 {
base.Branch(frame, self.Offset)
}
}
type IF_ICMPGE struct{ base.BranchInstruction }
func (self *IF_ICMPGE) Execute(frame *rtda.Frame) {
if val1, val2 := _icmpPop(frame); val1 >= val2 {
base.Branch(frame, self.Offset)
}
}
func _icmpPop(frame *rtda.Frame) (val1, val2 int32) {
stack := frame.OperandStack()
val2 = stack.PopInt()
val1 = stack.PopInt()
return
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/comparisons/if_icmp.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 430
|
```go
package constants
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Push byte
type BIPUSH struct {
val int8
}
func (self *BIPUSH) FetchOperands(reader *base.BytecodeReader) {
self.val = reader.ReadInt8()
}
func (self *BIPUSH) Execute(frame *rtda.Frame) {
i := int32(self.val)
frame.OperandStack().PushInt(i)
}
// Push short
type SIPUSH struct {
val int16
}
func (self *SIPUSH) FetchOperands(reader *base.BytecodeReader) {
self.val = reader.ReadInt16()
}
func (self *SIPUSH) Execute(frame *rtda.Frame) {
i := int32(self.val)
frame.OperandStack().PushInt(i)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/constants/ipush.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 177
|
```go
package constants
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Push null
type ACONST_NULL struct{ base.NoOperandsInstruction }
func (self *ACONST_NULL) Execute(frame *rtda.Frame) {
frame.OperandStack().PushRef(nil)
}
// Push double
type DCONST_0 struct{ base.NoOperandsInstruction }
func (self *DCONST_0) Execute(frame *rtda.Frame) {
frame.OperandStack().PushDouble(0.0)
}
type DCONST_1 struct{ base.NoOperandsInstruction }
func (self *DCONST_1) Execute(frame *rtda.Frame) {
frame.OperandStack().PushDouble(1.0)
}
// Push float
type FCONST_0 struct{ base.NoOperandsInstruction }
func (self *FCONST_0) Execute(frame *rtda.Frame) {
frame.OperandStack().PushFloat(0.0)
}
type FCONST_1 struct{ base.NoOperandsInstruction }
func (self *FCONST_1) Execute(frame *rtda.Frame) {
frame.OperandStack().PushFloat(1.0)
}
type FCONST_2 struct{ base.NoOperandsInstruction }
func (self *FCONST_2) Execute(frame *rtda.Frame) {
frame.OperandStack().PushFloat(2.0)
}
// Push int constant
type ICONST_M1 struct{ base.NoOperandsInstruction }
func (self *ICONST_M1) Execute(frame *rtda.Frame) {
frame.OperandStack().PushInt(-1)
}
type ICONST_0 struct{ base.NoOperandsInstruction }
func (self *ICONST_0) Execute(frame *rtda.Frame) {
frame.OperandStack().PushInt(0)
}
type ICONST_1 struct{ base.NoOperandsInstruction }
func (self *ICONST_1) Execute(frame *rtda.Frame) {
frame.OperandStack().PushInt(1)
}
type ICONST_2 struct{ base.NoOperandsInstruction }
func (self *ICONST_2) Execute(frame *rtda.Frame) {
frame.OperandStack().PushInt(2)
}
type ICONST_3 struct{ base.NoOperandsInstruction }
func (self *ICONST_3) Execute(frame *rtda.Frame) {
frame.OperandStack().PushInt(3)
}
type ICONST_4 struct{ base.NoOperandsInstruction }
func (self *ICONST_4) Execute(frame *rtda.Frame) {
frame.OperandStack().PushInt(4)
}
type ICONST_5 struct{ base.NoOperandsInstruction }
func (self *ICONST_5) Execute(frame *rtda.Frame) {
frame.OperandStack().PushInt(5)
}
// Push long constant
type LCONST_0 struct{ base.NoOperandsInstruction }
func (self *LCONST_0) Execute(frame *rtda.Frame) {
frame.OperandStack().PushLong(0)
}
type LCONST_1 struct{ base.NoOperandsInstruction }
func (self *LCONST_1) Execute(frame *rtda.Frame) {
frame.OperandStack().PushLong(1)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/constants/const.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 688
|
```go
package stack
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Swap the top two operand stack values
type SWAP struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]
\/
/\
V V
[...][c][a][b]
*/
func (self *SWAP) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
slot1 := stack.PopSlot()
slot2 := stack.PopSlot()
stack.PushSlot(slot1)
stack.PushSlot(slot2)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stack/swap.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 131
|
```go
package stack
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Pop the top operand stack value
type POP struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]
|
V
[...][c][b]
*/
func (self *POP) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
stack.PopSlot()
}
// Pop the top one or two operand stack values
type POP2 struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]
| |
V V
[...][c]
*/
func (self *POP2) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
stack.PopSlot()
stack.PopSlot()
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stack/pop.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 181
|
```go
package conversions
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Convert double to float
type D2F struct{ base.NoOperandsInstruction }
func (self *D2F) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
d := stack.PopDouble()
f := float32(d)
stack.PushFloat(f)
}
// Convert double to int
type D2I struct{ base.NoOperandsInstruction }
func (self *D2I) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
d := stack.PopDouble()
i := int32(d)
stack.PushInt(i)
}
// Convert double to long
type D2L struct{ base.NoOperandsInstruction }
func (self *D2L) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
d := stack.PopDouble()
l := int64(d)
stack.PushLong(l)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/conversions/d2x.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 206
|
```go
package stack
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Duplicate the top operand stack value
type DUP struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]
\_
|
V
[...][c][b][a][a]
*/
func (self *DUP) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
slot := stack.PopSlot()
stack.PushSlot(slot)
stack.PushSlot(slot)
}
// Duplicate the top operand stack value and insert two values down
type DUP_X1 struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]
__/
|
V
[...][c][a][b][a]
*/
func (self *DUP_X1) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
slot1 := stack.PopSlot()
slot2 := stack.PopSlot()
stack.PushSlot(slot1)
stack.PushSlot(slot2)
stack.PushSlot(slot1)
}
// Duplicate the top operand stack value and insert two or three values down
type DUP_X2 struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]
_____/
|
V
[...][a][c][b][a]
*/
func (self *DUP_X2) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
slot1 := stack.PopSlot()
slot2 := stack.PopSlot()
slot3 := stack.PopSlot()
stack.PushSlot(slot1)
stack.PushSlot(slot3)
stack.PushSlot(slot2)
stack.PushSlot(slot1)
}
// Duplicate the top one or two operand stack values
type DUP2 struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]____
\____ |
| |
V V
[...][c][b][a][b][a]
*/
func (self *DUP2) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
slot1 := stack.PopSlot()
slot2 := stack.PopSlot()
stack.PushSlot(slot2)
stack.PushSlot(slot1)
stack.PushSlot(slot2)
stack.PushSlot(slot1)
}
// Duplicate the top one or two operand stack values and insert two or three values down
type DUP2_X1 struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][c][b][a]
_/ __/
| |
V V
[...][b][a][c][b][a]
*/
func (self *DUP2_X1) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
slot1 := stack.PopSlot()
slot2 := stack.PopSlot()
slot3 := stack.PopSlot()
stack.PushSlot(slot2)
stack.PushSlot(slot1)
stack.PushSlot(slot3)
stack.PushSlot(slot2)
stack.PushSlot(slot1)
}
// Duplicate the top one or two operand stack values and insert two, three, or four values down
type DUP2_X2 struct{ base.NoOperandsInstruction }
/*
bottom -> top
[...][d][c][b][a]
____/ __/
| __/
V V
[...][b][a][d][c][b][a]
*/
func (self *DUP2_X2) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
slot1 := stack.PopSlot()
slot2 := stack.PopSlot()
slot3 := stack.PopSlot()
slot4 := stack.PopSlot()
stack.PushSlot(slot2)
stack.PushSlot(slot1)
stack.PushSlot(slot4)
stack.PushSlot(slot3)
stack.PushSlot(slot2)
stack.PushSlot(slot1)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/stack/dup.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 819
|
```go
package conversions
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Convert int to byte
type I2B struct{ base.NoOperandsInstruction }
func (self *I2B) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
i := stack.PopInt()
b := int32(int8(i))
stack.PushInt(b)
}
// Convert int to char
type I2C struct{ base.NoOperandsInstruction }
func (self *I2C) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
i := stack.PopInt()
c := int32(uint16(i))
stack.PushInt(c)
}
// Convert int to short
type I2S struct{ base.NoOperandsInstruction }
func (self *I2S) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
i := stack.PopInt()
s := int32(int16(i))
stack.PushInt(s)
}
// Convert int to long
type I2L struct{ base.NoOperandsInstruction }
func (self *I2L) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
i := stack.PopInt()
l := int64(i)
stack.PushLong(l)
}
// Convert int to float
type I2F struct{ base.NoOperandsInstruction }
func (self *I2F) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
i := stack.PopInt()
f := float32(i)
stack.PushFloat(f)
}
// Convert int to double
type I2D struct{ base.NoOperandsInstruction }
func (self *I2D) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
i := stack.PopInt()
d := float64(i)
stack.PushDouble(d)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/conversions/i2x.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 392
|
```go
package conversions
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Convert long to double
type L2D struct{ base.NoOperandsInstruction }
func (self *L2D) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
l := stack.PopLong()
d := float64(l)
stack.PushDouble(d)
}
// Convert long to float
type L2F struct{ base.NoOperandsInstruction }
func (self *L2F) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
l := stack.PopLong()
f := float32(l)
stack.PushFloat(f)
}
// Convert long to int
type L2I struct{ base.NoOperandsInstruction }
func (self *L2I) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
l := stack.PopLong()
i := int32(l)
stack.PushInt(i)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/conversions/l2x.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 206
|
```go
package conversions
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Convert float to double
type F2D struct{ base.NoOperandsInstruction }
func (self *F2D) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
f := stack.PopFloat()
d := float64(f)
stack.PushDouble(d)
}
// Convert float to int
type F2I struct{ base.NoOperandsInstruction }
func (self *F2I) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
f := stack.PopFloat()
i := int32(f)
stack.PushInt(i)
}
// Convert float to long
type F2L struct{ base.NoOperandsInstruction }
func (self *F2L) Execute(frame *rtda.Frame) {
stack := frame.OperandStack()
f := stack.PopFloat()
l := int64(f)
stack.PushLong(l)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/conversions/f2x.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 206
|
```go
package control
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
// Branch always
type GOTO struct{ base.BranchInstruction }
func (self *GOTO) Execute(frame *rtda.Frame) {
base.Branch(frame, self.Offset)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/control/goto.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 63
|
```go
package control
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
/*
tableswitch
<0-3 byte pad>
defaultbyte1
defaultbyte2
defaultbyte3
defaultbyte4
lowbyte1
lowbyte2
lowbyte3
lowbyte4
highbyte1
highbyte2
highbyte3
highbyte4
jump offsets...
*/
// Access jump table by index and jump
type TABLE_SWITCH struct {
defaultOffset int32
low int32
high int32
jumpOffsets []int32
}
func (self *TABLE_SWITCH) FetchOperands(reader *base.BytecodeReader) {
reader.SkipPadding()
self.defaultOffset = reader.ReadInt32()
self.low = reader.ReadInt32()
self.high = reader.ReadInt32()
jumpOffsetsCount := self.high - self.low + 1
self.jumpOffsets = reader.ReadInt32s(jumpOffsetsCount)
}
func (self *TABLE_SWITCH) Execute(frame *rtda.Frame) {
index := frame.OperandStack().PopInt()
var offset int
if index >= self.low && index <= self.high {
offset = int(self.jumpOffsets[index-self.low])
} else {
offset = int(self.defaultOffset)
}
base.Branch(frame, offset)
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/control/tableswitch.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 274
|
```go
package control
import "jvmgo/ch05/instructions/base"
import "jvmgo/ch05/rtda"
/*
lookupswitch
<0-3 byte pad>
defaultbyte1
defaultbyte2
defaultbyte3
defaultbyte4
npairs1
npairs2
npairs3
npairs4
match-offset pairs...
*/
// Access jump table by key match and jump
type LOOKUP_SWITCH struct {
defaultOffset int32
npairs int32
matchOffsets []int32
}
func (self *LOOKUP_SWITCH) FetchOperands(reader *base.BytecodeReader) {
reader.SkipPadding()
self.defaultOffset = reader.ReadInt32()
self.npairs = reader.ReadInt32()
self.matchOffsets = reader.ReadInt32s(self.npairs * 2)
}
func (self *LOOKUP_SWITCH) Execute(frame *rtda.Frame) {
key := frame.OperandStack().PopInt()
for i := int32(0); i < self.npairs*2; i += 2 {
if self.matchOffsets[i] == key {
offset := self.matchOffsets[i+1]
base.Branch(frame, int(offset))
return
}
}
base.Branch(frame, int(self.defaultOffset))
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/instructions/control/lookupswitch.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 259
|
```go
package classfile
/*
EnclosingMethod_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 class_index;
u2 method_index;
}
*/
type EnclosingMethodAttribute struct {
cp ConstantPool
classIndex uint16
methodIndex uint16
}
func (self *EnclosingMethodAttribute) readInfo(reader *ClassReader) {
self.classIndex = reader.readUint16()
self.methodIndex = reader.readUint16()
}
func (self *EnclosingMethodAttribute) ClassName() string {
return self.cp.getClassName(self.classIndex)
}
func (self *EnclosingMethodAttribute) MethodNameAndDescriptor() (string, string) {
if self.methodIndex > 0 {
return self.cp.getNameAndType(self.methodIndex)
} else {
return "", ""
}
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/classfile/attr_enclosing_method.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 170
|
```go
package classfile
/*
field_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
method_info {
u2 access_flags;
u2 name_index;
u2 descriptor_index;
u2 attributes_count;
attribute_info attributes[attributes_count];
}
*/
type MemberInfo struct {
cp ConstantPool
accessFlags uint16
nameIndex uint16
descriptorIndex uint16
attributes []AttributeInfo
}
// read field or method table
func readMembers(reader *ClassReader, cp ConstantPool) []*MemberInfo {
memberCount := reader.readUint16()
members := make([]*MemberInfo, memberCount)
for i := range members {
members[i] = readMember(reader, cp)
}
return members
}
func readMember(reader *ClassReader, cp ConstantPool) *MemberInfo {
return &MemberInfo{
cp: cp,
accessFlags: reader.readUint16(),
nameIndex: reader.readUint16(),
descriptorIndex: reader.readUint16(),
attributes: readAttributes(reader, cp),
}
}
func (self *MemberInfo) AccessFlags() uint16 {
return self.accessFlags
}
func (self *MemberInfo) Name() string {
return self.cp.getUtf8(self.nameIndex)
}
func (self *MemberInfo) Descriptor() string {
return self.cp.getUtf8(self.descriptorIndex)
}
func (self *MemberInfo) CodeAttribute() *CodeAttribute {
for _, attrInfo := range self.attributes {
switch attrInfo.(type) {
case *CodeAttribute:
return attrInfo.(*CodeAttribute)
}
}
return nil
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/classfile/member_info.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 380
|
```go
package classfile
/*
CONSTANT_MethodHandle_info {
u1 tag;
u1 reference_kind;
u2 reference_index;
}
*/
type ConstantMethodHandleInfo struct {
referenceKind uint8
referenceIndex uint16
}
func (self *ConstantMethodHandleInfo) readInfo(reader *ClassReader) {
self.referenceKind = reader.readUint8()
self.referenceIndex = reader.readUint16()
}
/*
CONSTANT_MethodType_info {
u1 tag;
u2 descriptor_index;
}
*/
type ConstantMethodTypeInfo struct {
descriptorIndex uint16
}
func (self *ConstantMethodTypeInfo) readInfo(reader *ClassReader) {
self.descriptorIndex = reader.readUint16()
}
/*
CONSTANT_InvokeDynamic_info {
u1 tag;
u2 bootstrap_method_attr_index;
u2 name_and_type_index;
}
*/
type ConstantInvokeDynamicInfo struct {
bootstrapMethodAttrIndex uint16
nameAndTypeIndex uint16
}
func (self *ConstantInvokeDynamicInfo) readInfo(reader *ClassReader) {
self.bootstrapMethodAttrIndex = reader.readUint16()
self.nameAndTypeIndex = reader.readUint16()
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/classfile/cp_invoke_dynamic.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 241
|
```go
package classfile
/*
BootstrapMethods_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 num_bootstrap_methods;
{ u2 bootstrap_method_ref;
u2 num_bootstrap_arguments;
u2 bootstrap_arguments[num_bootstrap_arguments];
} bootstrap_methods[num_bootstrap_methods];
}
*/
type BootstrapMethodsAttribute struct {
bootstrapMethods []*BootstrapMethod
}
func (self *BootstrapMethodsAttribute) readInfo(reader *ClassReader) {
numBootstrapMethods := reader.readUint16()
self.bootstrapMethods = make([]*BootstrapMethod, numBootstrapMethods)
for i := range self.bootstrapMethods {
self.bootstrapMethods[i] = &BootstrapMethod{
bootstrapMethodRef: reader.readUint16(),
bootstrapArguments: reader.readUint16s(),
}
}
}
type BootstrapMethod struct {
bootstrapMethodRef uint16
bootstrapArguments []uint16
}
```
|
/content/code_sandbox/v1/code/go/src/jvmgo/ch05/classfile/attr_bootstrap_methods.go
|
go
| 2016-01-06T13:13:45
| 2024-08-16T13:42:58
|
jvmgo-book
|
zxh0/jvmgo-book
| 1,605
| 184
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.