Jasmin-Java汇编语言
(http://blog.csdn.net/kingspider/)
(http://blog.sina.com.cn/u/1163490885)
Jasmin是Java汇编语言,以文本方式来描述JVM的指令集以及Java Class的结构,Jasmin编译器可以把汇编语言转换成二进制的字节码,使JVM可以调入执行。
Jasmin最初是由Jon Meyer和Troy Downing编纂《Java Virtual Machine》时设计的范例,虽然该书不再出版,但是Jasmin成为了事实上的Java汇编语言标准,并作为开源项目得到发展:http://jasmin.sourceforge.net/。
Jasmin在Java class方面的处理非常灵活,可以不受Java语法的一些限定,变量名、方法名等可以是Java源码中的非法单词,例如在Jasmin中可以定义变量名为if,但是在Java源码中是不行的。真是鉴于这样一个特点,Jasmin对于某些混淆处理过的Java代码仍然可以进行修改,可以对Java软件的License控制造成了一定程度的威胁,这也是Jasmin曾经受到抵制的原因。
我本人对Jasmin比较感兴趣,曾经根据Jasmin的语法,自己用JavaCC编写了一个功能更加强大的编译器,主要是对Jon Meyer提供的语法进行了一点扩展。
下面是用JavaCC导出的Jasmin BNF范式:
BNF for JasminParser.jj
NON-TERMINALS
jas_file | ::= | source_spec class_spec super_spec implements_list fields methods |
source_spec | ::= | ( <DSOURCE> <Word> <SEP> )? |
class_spec | ::= | <DCLASS> access classname <SEP> |
| | <DINTERFACE> access classname <SEP> | |
classname | ::= | <Word> |
access | ::= | access_list |
access_list | ::= | ( access_item )* |
access_item | ::= | <PUBLIC> |
| | <PRIVATE> | |
| | <PROTECTED> | |
| | <STATIC> | |
| | <FINAL> | |
| | <SYNCHRONIZED> | |
| | <VOLATILE> | |
| | <TRANSIENT> | |
| | <NATIVE> | |
| | <INTERFACE> | |
| | <ABSTRACT> | |
super_spec | ::= | <DSUPER> classname <SEP> |
implements_list | ::= | ( implements_spec | <SEP> )* |
implements_spec | ::= | <DIMPLEMENTS> classname <SEP> |
fields | ::= | ( field_spec )* |
field_spec | ::= | <DFIELD> access <Word> <Word> optional_default <SEP> |
optional_default | ::= | ( <EQ> item )? |
item | ::= | <Int> |
| | <Num> | |
| | <Str> | |
methods | ::= | ( method_spec )* |
method_spec | ::= | defmethod statements endmethod |
defmethod | ::= | <DMETHOD> access <Word> <SEP> |
endmethod | ::= | <DEND> <METHOD> <SEP> |
statements | ::= | ( statement )* |
statement | ::= | stmnt <SEP> |
stmnt | ::= | ( label | directive | instruction )? |
label | ::= | <Word> <COLON> |
directive | ::= | <DVAR> var_expr |
| | <DLIMIT> limit_expr | |
| | <DLINE> line_expr | |
| | <DTHROWS> throws_expr | |
| | <DCATCH> catch_expr | |
| | <DSET> set_expr | |
var_expr | ::= | <Int> <IS> <Word> <Word> ( <FROM> <Word> <TO> <Word> )? |
limit_expr | ::= | <Word> <Int> |
line_expr | ::= | <Int> |
throws_expr | ::= | classname |
catch_expr | ::= | classname <FROM> <Word> <TO> <Word> <USING> <Word> |
set_expr | ::= | <Word> <Int> |
| | <Word> <Word> | |
| | <Word> <Num> | |
| | <Word> <Str> | |
instruction | ::= | simple_instruction |
| | complex_instruction | |
simple_instruction | ::= | <Word> <Int> <Int> |
| | <Word> <Word> <Int> | |
| | <Word> <Word> <Word> | |
| | <Word> <Int> | |
| | <Word> <Num> | |
| | <Word> <Word> | |
| | <Word> <Str> | |
| | <Word> | |
complex_instruction | ::= | <LOOKUPSWITCH> lookup |
| | <TABLESWITCH> table | |
lookup | ::= | lookup_args lookup_list lookup_default |
lookup_args | ::= | <SEP> |
lookup_list | ::= | ( lookup_entry )+ |
lookup_entry | ::= | <Int> <COLON> <Word> <SEP> |
lookup_default | ::= | <DEFAULT_BRANCH> <COLON> <Word> |
table | ::= | table_args table_list table_default |
table_args | ::= | <Int> <SEP> |
| | <Int> <Int> <SEP> | |
table_list | ::= | ( table_entry )+ |
table_entry | ::= | <Word> <SEP> |
table_default | ::= | <DEFAULT_BRANCH> <COLON> <Word> |