-
Notifications
You must be signed in to change notification settings - Fork 0
/
javaclass.h
132 lines (117 loc) · 3.63 KB
/
javaclass.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* javaclass.h
*
* Java ClassFile struct
*
*
* Created by Robert van Engelen on 1/2/05.
* Copyright 2005 Robert van Engelen. All rights reserved.
*
*/
#ifndef JAVACLASS_H
#define JAVACLASS_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// The value of the access_flags item is a mask of flags used to denote access permissions to and properties of a class, interface, field, or method.
enum access_flags
{
ACC_PUBLIC = 0x0001, // Declared public; may be accessed from outside its package.
ACC_PRIVATE = 0x0002, // Declared private; usable only within the defining class.
ACC_PROTECTED = 0x0004, // Declared protected; may be accessed within subclasses.
ACC_STATIC = 0x0008, // Declared static.
ACC_FINAL = 0x0010, // Declared final; no subclasses allowed or no further assignment after initialization.
ACC_SUPER = 0x0020, // Treat superclass methods specially when invoked by the invokespecial instruction.
ACC_VOLATILE = 0x0040, // Declared volatile; cannot be cached.
ACC_TRANSIENT = 0x0080, // Declared transient; not written or read by a persistent object manager.
ACC_INTERFACE = 0x0200, // Is an interface, not a class
ACC_ABSTRACT = 0x0400, // Declared abstract; may not be instantiated.
};
// Each item in the constant_pool table must begin with a 1-byte tag constant_type indicating the kind of cp_info entry.
enum constant_type
{
CONSTANT_Utf8 = 1,
CONSTANT_Integer = 3,
CONSTANT_Float = 4,
CONSTANT_Long = 5,
CONSTANT_Double = 6,
CONSTANT_Class = 7,
CONSTANT_String = 8,
CONSTANT_Fieldref = 9,
CONSTANT_Methodref = 10,
CONSTANT_InterfaceMethodref = 11,
CONSTANT_NameAndType = 12,
};
typedef unsigned char u1;
typedef unsigned short u2;
typedef unsigned int u4;
// The Java Class File (in simplified form)
struct ClassFile
{
enum access_flags access;
char *name;
u2 constant_pool_count;
struct ConstantPool *constant_pool;
u2 field_count;
struct FieldInfo *fields;
u2 method_count;
struct MethodInfo *methods;
};
// The constant pool may contain pairs of references
struct Pair
{
u2 i, j;
};
// The Java class constant pool (in simplified form)
struct ConstantPool
{
u1 tag;
union
{
char *u; // utf8 string
u4 i; // int
float f;
// long long // for future extensions
// double // for future extensions
u2 r; // reference (pool index)
struct Pair t; // tuple of references
} data;
struct ConstantPool *next;
};
// Java class field information
struct FieldInfo
{
enum access_flags access;
char *name;
char *descriptor;
};
// Java class method information
struct MethodInfo
{
enum access_flags access;
char *name;
char *descriptor;
u2 max_stack;
u2 max_locals;
u4 code_length;
u1 *code;
};
// Bytecode emitter's global pc
extern int pc;
void init_code();
u1 *copy_code();
int emit(u1 opcode);
int emit2(u1 opcode, u1 operand);
int emit3(u1 opcode, u2 operand);
int emit32(u1 opcode, u1 operand1, u1 operand2);
int backpatch(u2 loc, int offset);
int init_ClassFile(struct ClassFile *cf);
int save_classFile(struct ClassFile *cf);
int constant_pool_add_Utf8(struct ClassFile *cf, const char *name);
int constant_pool_add_Integer(struct ClassFile *cf, u4 i);
int constant_pool_add_Float(struct ClassFile *cf, float f);
int constant_pool_add_Class(struct ClassFile *cf, const char *name);
int constant_pool_add_Fieldref(struct ClassFile *cf, const char *class_name, const char *field_name, const char *descriptor);
int constant_pool_add_Methodref(struct ClassFile *cf, const char *class_name, const char *method_name, const char *descriptor);
int constant_pool_add_NameAndType(struct ClassFile *cf, const char *name, const char *descriptor);
#endif