add flat assembler toolchain

This commit is contained in:
2024-11-24 23:13:28 -05:00
parent 99e8e4072b
commit dbfd94ea40
302 changed files with 145599 additions and 0 deletions

View File

@ -0,0 +1,134 @@
; This example uses a very simple set of macroinstructions to generate
; the basic structures of JVM class file.
; Please refer to "The Java Virtual Machine Specification" for the detailed
; information on this file format.
include 'jclass.inc'
format binary as 'class'
u4 0xcafebabe ; magic
u2 0,49 ; minor and major version
constant_pool
_Code constant_utf8 'Code'
_init constant_utf8 '<init>'
_main constant_utf8 'main'
_void_arrstr constant_utf8 '([Ljava/lang/String;)V'
Test_class constant_class _Test
_Test constant_utf8 'Test'
Object_init constant_methodref Object_class,init_method
Object_class constant_class _Object
_Object constant_utf8 'java/lang/Object'
init_method constant_nameandtype _init,_void
_void constant_utf8 '()V'
System.out constant_fieldref System_class,out_field
System_class constant_class _System
_System constant_utf8 'java/lang/System'
out_field constant_nameandtype _out,PrintStream_type
_out constant_utf8 'out'
PrintStream_type constant_utf8 'Ljava/io/PrintStream;'
PrintStream_println constant_methodref PrintStream_class,println_method
PrintStream_class constant_class _PrintStream
_PrintStream constant_utf8 'java/io/PrintStream'
println_method constant_nameandtype _println,_void_str
_println constant_utf8 'println'
_void_str constant_utf8 '(Ljava/lang/String;)V'
Integer_toString constant_methodref Integer_class,toString_method
Integer_class constant_class _Integer
_Integer constant_utf8 'java/lang/Integer'
toString_method constant_nameandtype _toString,_str_int
_toString constant_utf8 'toString'
_str_int constant_utf8 '(I)Ljava/lang/String;'
number constant_integer 17
end constant_pool
u2 ACC_PUBLIC+ACC_SUPER ; access flags
u2 Test_class ; this class
u2 Object_class ; super class
interfaces
end interfaces
fields
end fields
methods
method_info ACC_PUBLIC, _init, _void ; public void Test()
attribute _Code
u2 1 ; max_stack
u2 1 ; max_locals
bytecode
aload 0
invokespecial Object_init
return
end bytecode
exceptions
end exceptions
attributes
end attributes
end attribute
end method_info
method_info ACC_PUBLIC+ACC_STATIC, _main, _void_arrstr ; public static void main(String[] args)
attribute _Code
u2 3 ; max_stack
u2 2 ; max_locals
bytecode
ldc number
istore 1
example_loop:
iload 1
dup
imul
invokestatic Integer_toString
getstatic System.out
swap
invokevirtual PrintStream_println
iinc 1,-1
iload 1
ifne example_loop
return
end bytecode
exceptions
end exceptions
attributes
end attributes
end attribute
end method_info
end methods
attributes
end attributes

View File

@ -0,0 +1,797 @@
T_BOOLEAN = 4
T_CHAR = 5
T_FLOAT = 6
T_DOUBLE = 7
T_BYTE = 8
T_SHORT = 9
T_INT = 10
T_LONG = 11
macro aaload
db 0x32
end macro
macro aastore
db 0x53
end macro
macro aconst_null
db 0x01
end macro
macro aload index
if index<0
err "invalid index"
else if index<=3
db 0x2a+index
else if index<100h
db 0x19,index
else
db 0xc4,0x19,(index) shr 8,(index) and 0FFh
end if
end macro
macro anewarray class
db 0xbd,(class) shr 8,(class) and 0FFh
end macro
macro areturn
db 0xb0
end macro
macro arraylength
db 0xbe
end macro
macro astore index
if index<0
err "invalid index"
else if index<=3
db 0x4b+index
else if index<100h
db 0x3a,index
else
db 0xc4,0x3a,(index) shr 8,(index) and 0FFh
end if
end macro
macro athrow
db 0xbf
end macro
macro baload
db 0x33
end macro
macro bastore
db 0x54
end macro
macro bipush byte
if byte>-1 & byte<=5
db 0x03+byte
else
db 0x10,byte
end if
end macro
macro caload
db 0x34
end macro
macro castore
db 0x55
end macro
macro checkcast class
db 0xc0,(class) shr 8,(class) and 0FFh
end macro
macro d2f
db 0x90
end macro
macro d2i
db 0x8e
end macro
macro d2l
db 0x8f
end macro
macro dadd
db 0x63
end macro
macro daload
db 0x31
end macro
macro dastore
db 0x52
end macro
macro dcmpg
db 0x98
end macro
macro dcmpl
db 0x97
end macro
macro dconst_0
db 0x0e
end macro
macro dconst_1
db 0x0f
end macro
macro ddiv
db 0x6f
end macro
macro dload index
if index<0
err "invalid index"
else if index<=3
db 0x26+index
else if index<100h
db 0x18,index
else
db 0xc4,0x18,(index) shr 8,(index) and 0FFh
end if
end macro
macro dmul
db 0x6b
end macro
macro dneg
db 0x77
end macro
macro drem
db 0x73
end macro
macro dreturn
db 0xaf
end macro
macro dstore index
if index<0
err "invalid index"
else if index<=3
db 0x47+index
else if index<100h
db 0x39,index
else
db 0xc4,0x39,(index) shr 8,(index) and 0FFh
end if
end macro
macro dsub
db 0x67
end macro
macro dup
db 0x59
end macro
macro dup_x1
db 0x5a
end macro
macro dup_x2
db 0x5b
end macro
macro dup2
db 0x5c
end macro
macro dup2_x1
db 0x5d
end macro
macro dup2_x2
db 0x5e
end macro
macro f2d
db 0x8d
end macro
macro f2i
db 0x8b
end macro
macro f2l
db 0x8c
end macro
macro fadd
db 0x62
end macro
macro faload
db 0x30
end macro
macro fastore
db 0x51
end macro
macro fcmpg
db 0x96
end macro
macro fcmpl
db 0x95
end macro
macro fconst_0
db 0x0b
end macro
macro fconst_1
db 0x0c
end macro
macro fconst_2
db 0x0d
end macro
macro fdiv
db 0x6e
end macro
macro fload index
if index<0
err "invalid index"
else if index<=3
db 0x22+index
else if index<100h
db 0x17,index
else
db 0xc4,0x17,(index) shr 8,(index) and 0FFh
end if
end macro
macro fmul
db 0x6a
end macro
macro fneg
db 0x76
end macro
macro frem
db 0x72
end macro
macro freturn
db 0xae
end macro
macro fstore index
if index<0
err "invalid index"
else if index<=3
db 0x43+index
else if index<100h
db 0x38,index
else
db 0xc4,0x38,(index) shr 8,(index) and 0FFh
end if
end macro
macro fsub
db 0x66
end macro
macro getfield index
db 0xb4,(index) shr 8,(index) and 0FFh
end macro
macro getstatic index
db 0xb2,(index) shr 8,(index) and 0FFh
end macro
macro goto branch
offset = branch-$
if offset>=-8000h & offset<8000h
db 0xa7,offset shr 8,offset and 0FFh
else
db 0xc8,offset shr 24,(offset shr 16) and 0FFh,(offset shr 8) and 0FFh,offset and 0FFh
end if
end macro
macro goto_w branch
offset = branch-$
db 0xc8,offset shr 24,(offset shr 16) and 0FFh,(offset shr 8) and 0FFh,offset and 0FFh
end macro
macro i2b
db 0x91
end macro
macro i2c
db 0x92
end macro
macro i2d
db 0x87
end macro
macro i2f
db 0x86
end macro
macro i2l
db 0x85
end macro
macro i2s
db 0x93
end macro
macro iadd
db 0x60
end macro
macro iaload
db 0x2e
end macro
macro iand
db 0x7e
end macro
macro iastore
db 0x4f
end macro
macro iconst_m1
db 0x02
end macro
macro iconst_0
db 0x03
end macro
macro iconst_1
db 0x04
end macro
macro iconst_2
db 0x05
end macro
macro iconst_3
db 0x06
end macro
macro iconst_4
db 0x07
end macro
macro iconst_5
db 0x08
end macro
macro idiv
db 0x6c
end macro
macro if_acmpeq branch
offset = branch-$
db 0xa5,offset shr 8,offset and 0FFh
end macro
macro if_acmpne branch
offset = branch-$
db 0xa6,offset shr 8,offset and 0FFh
end macro
macro if_icmpeq branch
offset = branch-$
db 0x9f,offset shr 8,offset and 0FFh
end macro
macro if_icmpne branch
offset = branch-$
db 0xa0,offset shr 8,offset and 0FFh
end macro
macro if_icmplt branch
offset = branch-$
db 0xa1,offset shr 8,offset and 0FFh
end macro
macro if_icmpge branch
offset = branch-$
db 0xa2,offset shr 8,offset and 0FFh
end macro
macro if_icmpgt branch
offset = branch-$
db 0xa3,offset shr 8,offset and 0FFh
end macro
macro if_icmple branch
offset = branch-$
db 0xa4,offset shr 8,offset and 0FFh
end macro
macro ifeq branch
offset = branch-$
db 0x99,offset shr 8,offset and 0FFh
end macro
macro ifne branch
offset = branch-$
db 0x9a,offset shr 8,offset and 0FFh
end macro
macro iflt branch
offset = branch-$
db 0x9b,offset shr 8,offset and 0FFh
end macro
macro ifge branch
offset = branch-$
db 0x9c,offset shr 8,offset and 0FFh
end macro
macro ifgt branch
offset = branch-$
db 0x9d,offset shr 8,offset and 0FFh
end macro
macro ifle branch
offset = branch-$
db 0x9e,offset shr 8,offset and 0FFh
end macro
macro ifnonnull branch
offset = branch-$
db 0xc7,offset shr 8,offset and 0FFh
end macro
macro ifnull branch
offset = branch-$
db 0xc6,offset shr 8,offset and 0FFh
end macro
macro iinc index,const
if index<0
err "invalid index"
else if index<100h & const<80h & const>=-80h
db 0x84,index,const
else
db 0xc4,0x84,(index) shr 8,(index) and 0FFh,(const) shr 8,(const) and 0FFh
end if
end macro
macro iload index
if index<0
err "invalid index"
else if index<=3
db 0x1a+index
else if index<100h
db 0x15,index
else
db 0xc4,0x15,(index) shr 8,(index) and 0FFh
end if
end macro
macro imul
db 0x68
end macro
macro ineg
db 0x74
end macro
macro instanceof index
db 0xc1,(index) shr 8,(index) and 0FFh
end macro
macro invokedynamic index
db 0xba,(index) shr 8,(index) and 0FFh,0,0
end macro
macro invokeinterface index,count
db 0xb9,(index) shr 8,(index) and 0FFh,count
end macro
macro invokespecial index
db 0xb7,(index) shr 8,(index) and 0FFh
end macro
macro invokestatic index
db 0xb8,(index) shr 8,(index) and 0FFh
end macro
macro invokevirtual index
db 0xb6,(index) shr 8,(index) and 0FFh
end macro
macro ior
db 0x80
end macro
macro irem
db 0x70
end macro
macro ireturn
db 0xac
end macro
macro ishl
db 0x78
end macro
macro ishr
db 0x7a
end macro
macro istore index
if index<0
err "invalid index"
else if index<=3
db 0x3b+index
else if index<100h
db 0x36,index
else
db 0xc4,0x36,(index) shr 8,(index) and 0FFh
end if
end macro
macro isub
db 0x64
end macro
macro iushr
db 0x7c
end macro
macro ixor
db 0x82
end macro
macro jsr branch
offset = branch-$
if offset>=-8000h & offset<8000h
db 0xa8,offset shr 8,offset and 0FFh
else
db 0xc9,offset shr 24,(offset shr 16) and 0FFh,(offset shr 8) and 0FFh,offset and 0FFh
end if
end macro
macro jsr_w branch
offset = branch-$
db 0xc9,offset shr 24,(offset shr 16) and 0FFh,(offset shr 8) and 0FFh,offset and 0FFh
end macro
macro l2d
db 0x8a
end macro
macro l2f
db 0x89
end macro
macro l2i
db 0x88
end macro
macro ladd
db 0x61
end macro
macro laload
db 0x2f
end macro
macro land
db 0x7f
end macro
macro lastore
db 0x50
end macro
macro lcmp
db 0x94
end macro
macro lconst_0
db 0x09
end macro
macro lconst_1
db 0x0a
end macro
macro ldc index
if index<0
err "invalid index"
else if index<100h
db 0x12,index
else
db 0x13,(index) shr 8,(index) and 0FFh
end if
end macro
macro ldc_w index
db 0x13,(index) shr 8,(index) and 0FFh
end macro
macro ldc2_w index
db 0x14,(index) shr 8,(index) and 0FFh
end macro
macro ldiv
db 0x6d
end macro
macro lload index
if index<0
err "invalid index"
else if index<=3
db 0x1e+index
else if index<100h
db 0x16,index
else
db 0xc4,0x16,(index) shr 8,(index) and 0FFh
end if
end macro
macro lmul
db 0x69
end macro
macro lneg
db 0x75
end macro
macro lookupswitch
; db 0xab,...
err "not implemented yet"
end macro
macro lor
db 0x81
end macro
macro lrem
db 0x71
end macro
macro lreturn
db 0xad
end macro
macro lshl
db 0x79
end macro
macro lshr
db 0x7b
end macro
macro lstore index
if index<0
err "invalid index"
else if index<=3
db 0x3f+index
else if index<100h
db 0x37,index
else
db 0xc4,0x37,(index) shr 8,(index) and 0FFh
end if
end macro
macro lsub
db 0x65
end macro
macro lushr
db 0x7d
end macro
macro lxor
db 0x83
end macro
macro monitorenter
db 0xc2
end macro
macro monitorexit
db 0xc3
end macro
macro multianewarray index,dimensions
db 0xc5,(index) shr 8,(index) and 0FFh,dimensions
end macro
macro new index
db 0xbb,(index) shr 8,(index) and 0FFh
end macro
macro newarray atype
db 0xbc,atype
end macro
macro nop
db 0x00
end macro
macro pop
db 0x57
end macro
macro pop2
db 0x58
end macro
macro putfield index
db 0xb5,(index) shr 8,(index) and 0FFh
end macro
macro putstatic index
db 0xb3,(index) shr 8,(index) and 0FFh
end macro
macro ret index
if index<0
err "invalid index"
else if index<100h
db 0xa9,index
else
db 0xc4,0xa9,(index) shr 8,(index) and 0FFh
end if
end macro
macro return
db 0xb1
end macro
macro saload
db 0x35
end macro
macro sastore
db 0x56
end macro
macro sipush short
db 0x11,(short) shr 8,(short) and 0FFh
end macro
macro swap
db 0x5f
end macro
macro tableswitch
; db 0xaa,...
err "not implemented yet"
end macro
macro breakpoint
db 0xca
end macro
macro impdep1
db 0xfe
end macro
macro impdep2
db 0xff
end macro

View File

@ -0,0 +1,254 @@
ACC_PUBLIC = 0x0001
ACC_PRIVATE = 0x0002
ACC_PROTECTED = 0x0004
ACC_STATIC = 0x0008
ACC_FINAL = 0x0010
ACC_SUPER = 0x0020
ACC_SYNCHRONIZED = 0x0020
ACC_NATIVE = 0x0200
ACC_INTERFACE = 0x0200
ACC_ABSTRACT = 0x0400
ACC_STRICT = 0x0800
macro u1 values&
irp v,values
db v
end irp
end macro
macro u2 values&
irp v,values
db (v) bswap 2
end irp
end macro
macro u4 values&
irp v,values
db (v) bswap 4
end irp
end macro
macro constant_pool
u2 constant_pool_count
constant_pool_counter = 1
struc constant_utf8 string&
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
local data,length
u1 1
u2 length
data: db string
length = $ - data
end struc
struc constant_integer value
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 3
u4 value
end struc
struc constant_float value
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 4
u4 value
end struc
struc constant_long value
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 5
u4 value shr 32,value and 0FFFFFFFFh
end struc
struc constant_double value
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 6
u4 value shr 32,value and 0FFFFFFFFh
end struc
struc constant_class name_index
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 7
u2 name_index
end struc
struc constant_string string_index
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 8
u2 string_index
end struc
struc constant_fieldref class_index,name_and_type_index
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 9
u2 class_index
u2 name_and_type_index
end struc
struc constant_methodref class_index,name_and_type_index
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 10
u2 class_index
u2 name_and_type_index
end struc
struc constant_interfacemethodref class_index,name_and_type_index
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 11
u2 class_index
u2 name_and_type_index
end struc
struc constant_nameandtype name_index,descriptor_index
. = constant_pool_counter
constant_pool_counter = constant_pool_counter + 1
u1 12
u2 name_index
u2 descriptor_index
end struc
end macro
macro end?.constant_pool
constant_pool_count = constant_pool_counter
restruc constant_utf8,constant_integer,constant_float,constant_long,constant_double
restruc constant_class,constant_string
restruc constant_fieldref,constant_methodref,constant_interfacemethodref,constant_nameandtype
end macro
macro interfaces
u2 interfaces_count
interfaces_counter = 0
macro interface interface
interfaces_counter = interfaces_counter + 1
u2 interface
end macro
end macro
macro end?.interfaces
interfaces_count = interfaces_counter
purge interface
end macro
macro attributes
local count,counter
u2 count
counter = 0
attributes_count equ count
attributes_counter equ counter
macro attribute attribute_name_index
match sym,attributes_counter
sym = sym + 1
end match
u2 attribute_name_index
local start,length
u4 length
start = $
attribute_start equ start
attribute_length equ length
end macro
macro end?.attribute
match sym,attribute_length
sym = $ - attribute_start
end match
restore atribute_start,attribute_length
end macro
end macro
macro end?.attributes
match sym,attributes_count
sym = attributes_counter
end match
restore attributes_count,attributes_counter
purge attribute
end macro
macro fields
u2 fields_count
fields_counter = 0
macro field_info access_flags,name_index,descriptor_index
fields_counter = fields_counter + 1
u2 access_flags
u2 name_index
u2 descriptor_index
attributes
end macro
macro end?.field_info
end?.attributes
end macro
end macro
macro end?.fields
fields_count = fields_counter
purge field_info,end?.field_info
end macro
macro methods
u2 methods_count
methods_counter = 0
macro method_info access_flags,name_index,descriptor_index
methods_counter = methods_counter + 1
u2 access_flags
u2 name_index
u2 descriptor_index
attributes
end macro
macro end?.method_info
end?.attributes
end macro
end macro
macro end?.methods
methods_count = methods_counter
purge method_info,end?.method_info
end macro
macro bytecode
local length
bytecode_length equ length
u4 length
bytecode_offset = $
org 0
end macro
macro end?.bytecode
match sym,bytecode_length
sym = $
end match
org bytecode_offset+bytecode_length
restore bytecode_length
end macro
macro exceptions
local length
exception_table_length equ length
u2 length
exception_counter = 0
macro exception start_pc,end_pc,handler_pc,catch_type
exception_counter = exception_counter + 1
u2 start_pc
u2 end_pc
u2 handler_pc
u2 catch_type
end macro
end macro
macro end?.exceptions
match sym,exception_table_length
sym = exception_counter
end match
restore exception_table_length
end macro
include 'bytecode.inc'

View File

@ -0,0 +1 @@
fasmg Test.asm Test.class