Skip to content

Instantly share code, notes, and snippets.

@Pabloader
Created September 9, 2016 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pabloader/aa52421a0a2cfa0f49f2cd90894c2186 to your computer and use it in GitHub Desktop.
Save Pabloader/aa52421a0a2cfa0f49f2cd90894c2186 to your computer and use it in GitHub Desktop.
format binary as 'class'
include 'java.inc'
; --- FILE ---
magic: u4 0xCAFEBABE
version: u4 51
ConstantPool
Class this, 'java'
Class super, 'java/lang/Object'
UTF8 main, 'main'
UTF8 main_sig, '([Ljava/lang/String;)V'
Field o, 'java/lang/System', 'out', 'Ljava/io/PrintStream;'
Method println, 'java/io/PrintStream', 'println', '(Ljava/lang/String;)V'
Method printlnInt, 'java/io/PrintStream', 'println', '(I)V'
String hello, 'Hello World!'
ConstantPoolEnd
u2 PUBLIC, this, super, 0, 0
Methods
MethodStart PUBLIC or STATIC, main, main_sig, 2, 1
getstatic o
ldc hello
invokevirtual println
getstatic o
bipush 42
invokevirtual printlnInt
return
MethodEnd
MethodsEnd
u2 0
; --- DATATYPES ---
PUBLIC equ 0x01
STATIC equ 0x08
u1 equ db
macro u2 [data] {
forward
u1 (((data) shr 8) and 0xFF)
u1 ((data) and 0xFF)
}
macro u4 [data] {
forward
u2 (((data) shr 16) and 0xFFFF)
u2 ((data) and 0xFFFF)
}
; --- MACROS ---
const_count = 0
macro const name* {
const_#name: const_count = const_count + 1
name = const_count
}
macro ConstantPool {
u2 const_count_end + 1
}
macro ConstantPoolEnd {
UTF8 code_attr, 'Code'
const_count_end = const_count
}
methods_count = 0
macro Methods {
u2 methods_count_end
}
macro MethodsEnd {
methods_count_end = methods_count
}
macro MethodStart access*, name*, type*, stack*, locals* {
method#name:
u2 access
u2 name
u2 type
u2 1 ; attributes count
u2 code_attr
u4 .end - .start
.start:
u2 stack
u2 locals
u4 .code_end - .code_start
.code_start:
}
macro MethodEnd {
.code_end:
u2 0, 0
.end:
methods_count = methods_count + 1
}
macro UTF8 name, text {
const name
u1 1 ; tag
u2 .end - .start
.start: u1 text
.end:
}
macro String name, text {
UTF8 string_#name, text
const name
u1 8 ; tag
u2 string_#name
}
macro Class name, className {
UTF8 className_#name, className
const name
u1 7 ; tag
u2 className_#name
}
macro NameAndType name, nameText, typeText {
UTF8 name_#name, nameText
UTF8 type_#name, typeText
const name
u1 12 ; tag
u2 name_#name
u2 type_#name
}
macro Method name, className, methodName, methodType {
Class class_#name, className
NameAndType nameAndType_#name, methodName, methodType
const name
u1 10 ; tag
u2 class_#name
u2 nameAndType_#name
}
macro Field name, className, fieldName, fieldType {
Class class_#name, className
NameAndType nameAndType_#name, fieldName, fieldType
const name
u1 9 ; tag
u2 class_#name
u2 nameAndType_#name
}
; --- COMMANDS ---
macro getstatic field {
u1 0xb2
u2 field
}
macro ldc const {
u1 0x12
u1 const
}
macro bipush const {
u1 0x10
u1 const
}
macro invokevirtual method {
u1 0xb6
u2 method
}
macro return {
u1 0xb1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment