-
Notifications
You must be signed in to change notification settings - Fork 0
/
JVM_core.c
213 lines (173 loc) · 7.43 KB
/
JVM_core.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "macros.h"
#include "JVM_core.h"
#include "LoadClass_core.h"
#include "Execution_Core.h"
#include "LoadClass_ui.h"
/**
* @brief Inicializa a JVM; Aloca memória para as estruturas principais;
Chama o carregador para a primeira classe; inicializa a primeira classe
*
* @param classHeap
* @param objectHeap
* @param stackFrame
* @param classPathF_ptr
*/
void jvmStartup(ClassFile *classHeap_ptr, Object *objectHeap_ptr, Frame *stackFrame_ptr, FILE *classPathF_ptr, dataMSize_t *dmSize_ptr){
classHeap_ptr = malloc( CLSHEAP_MAX*sizeof( ClassFile ) );
objectHeap_ptr = malloc( OBJHEAP_MAX*sizeof( Object_t ) );
stackFrame_ptr = malloc( STKFRAME_MAX*sizeof( Frame ) );
dmSize_ptr->clsHeap_size = 0;
dmSize_ptr->objHeap_size = 0;
dmSize_ptr->stkHeap_size = 0;
//Carrega a classe inicial
//OK!
loadClass(classPathF_ptr, classHeap_ptr, dmSize_ptr);
printf("\nstatic_values_size %d", classHeap_ptr->static_values_size);
for(int i = 0; i < classHeap_ptr->static_values_size; i++)
printf("\nname %s", classHeap_ptr->static_values[i].field_name);
//Checa a consistência da classe
printf("\n\nConteudo do .class");
printf("\n--------------------------------");
print_ClassFile(classHeap_ptr);
//Inicializa a classe inicial, roda clinit
//OK!
initializeClass(classHeap_ptr, stackFrame_ptr, dmSize_ptr, classHeap_ptr);
//initializeClass(classHeap_ptr, stackFrame_ptr, &dmSize_ptr->stkHeap_size); //Sei que o primeiro elemento da classHeap é a classe inicial
//Chamo o método main
callMethod(classHeap_ptr, stackFrame_ptr, dmSize_ptr, classHeap_ptr, "main", "([Ljava/lang/String;)V");
}
/**
* @brief
*
* @param class_ptr
* @param stkFrame_ptr
* @param stkFrameTop_ptr
*/
void initializeClass(ClassFile *class_ptr, Frame *stkFrame_ptr, dataMSize_t *dmSize_ptr, ClassFile *classHeap_ptr){
u2 method_idx = seekMethodInClass( class_ptr, "<clinit>", "()V" );
method_info *method_ptr = &class_ptr->methods[method_idx];
//Quem cria deleta.
createFrame(method_ptr, class_ptr, stkFrame_ptr, &dmSize_ptr->stkHeap_size);//Cria o frame para o método <clinit> da classe.
u2 aux_idx = dmSize_ptr->stkHeap_size - 1; // o stkFrameTop_ptr na verdade é o stack frame size, que indica a qtd de frames na stkframe.
//Teste
Execute(stkFrame_ptr, classHeap_ptr, dmSize_ptr);
//Deleta o frame.
deleteFrame(&stkFrame_ptr[aux_idx], &dmSize_ptr->stkHeap_size);
}
/**
* @brief
*
* @param class_ptr
* @param stkFrame_ptr
* @param dmSize_ptr
* @param classHeap_ptr
* @param mth_name
* @param mth_descriptor
*/
void callMethod(ClassFile *class_ptr, Frame *stkFrame_ptr, dataMSize_t *dmSize_ptr, ClassFile *classHeap_ptr, \
char *mth_name, char *mth_descriptor){
u2 method_idx = seekMethodInClass( class_ptr, mth_name, mth_descriptor );
if( method_idx == SEEK_NOTFOUND){
exit(1);
}
method_info *method_ptr = &class_ptr->methods[method_idx];
//Quem cria deleta.
createFrame(method_ptr, class_ptr, stkFrame_ptr, &dmSize_ptr->stkHeap_size);//Cria o frame para o método <clinit> da classe.
u2 aux_idx = dmSize_ptr->stkHeap_size - 1; // o stkFrameTop_ptr na verdade é o stack frame size, que indica a qtd de frames na stkframe.
//Teste
Execute(stkFrame_ptr, classHeap_ptr, dmSize_ptr);
//Deleta o frame.
deleteFrame(&stkFrame_ptr[aux_idx], &dmSize_ptr->stkHeap_size);
}
/** OK!
* @brief Busca no classFile por um métodos específico e retorna o indice para a tabela de métodos
*
* @param class_ptr ponteiro para um classFile
* @param methName_str nome do método que se deseja buscar
* @param methDescriptor_str descritor do método, ()V por exemplo
*
* @return retorna o índice na tabela de métodos do classFile
*/
u2 seekMethodInClass(ClassFile *class_ptr, char *methName_str, char *methDescriptor_str){
char *methodN;
char *methodD;
//char *bytes;
//u2 str_size;
for(int i = 0; i < class_ptr->methods_count; i++){
//str_size = class_ptr->constant_pool[class_ptr->methods[i].name_inex - 1].info.CONSTANT_Utf8_info.length;
//methodN = malloc( (str_size + 1)*sizeof(char) );
//bytes = class_ptr->constant_pool[class_ptr->methods[i].name_index - 1].info.CONSTANT_Utf8_info_bytes;
methodN = class_ptr->constant_pool[class_ptr->methods[i].name_index - 1].info.CONSTANT_Utf8_info.bytes;
methodD = class_ptr->constant_pool[class_ptr->methods[i].descriptor_index - 1].info.CONSTANT_Utf8_info.bytes;
if( !strcmp(methodN, methName_str) && !strcmp(methodD, methDescriptor_str) ){
return i;
}
/*for(int j = 0; j < str_size; j++){
methodN[j] = bytes[j];
}
methodN[str_size
*/
}
return SEEK_NOTFOUND;
}
/*SAMUEL - início*/
int findClass(ClassFile *classHeap_ptr, dataMSize_t dmSize, char* ClassName){
u2 clsHeapSize = dmSize.stkHeap_size;
u2 index;
for(int i=0; i < clsHeapSize; i++) {
index = classHeap_ptr[i].this_class;
if(strcmp(classHeap_ptr[i].constant_pool[index-1].info.CONSTANT_Utf8_info.bytes, ClassName) == 0) {
return i;
}
}
return SEEK_NOTFOUND;
}
u2 findCode(method_info *method) {
u2 i = 0;
while(method->attribute[i].tag != 1){
i++;
}
return i;
}
//Lembrar de enviar o dataMSize->stkHeap_size para o numFrames
void createFrame(method_info *method, ClassFile *Class, Frame *frame_ptr, u2 *numFrames) {
u2 i = *numFrames;
u2 codeIndex = 0;
codeIndex = findCode(method);
if (i < STKFRAME_MAX - 1) {
frame_ptr[i].pClass = Class;
frame_ptr[i].pMethod = method;
frame_ptr[i].code_length = method->attribute[codeIndex].info.Code_attribute.code_length;
frame_ptr[i].code = method->attribute[codeIndex].info.Code_attribute.code;
frame_ptr[i].pc = 0;
frame_ptr[i].sp = 0;
frame_ptr[i].stack_size = method->attribute[codeIndex].info.Code_attribute.max_stack;
frame_ptr[i].local_size = method->attribute[codeIndex].info.Code_attribute.max_locals;
frame_ptr[i].stack = malloc(frame_ptr[i].stack_size * sizeof(u4));
frame_ptr[i].local = malloc(frame_ptr[i].local_size * sizeof(u4));
/*TESTE*/
// frame_ptr[i - 1].pClass = Class;
// frame_ptr[i -1].pMethod = method;
// frame_ptr[i -1].code_length = Class->methods->attribute[codeIndex].info.Code_attribute.code_length;
// frame_ptr[i - 1].code = malloc(frame_ptr[i].code_length * sizeof(u1));
//
// frame_ptr[i - 1].pc = 0;
// frame_ptr[i - 1].sp = 0;
//
// frame_ptr[i - 1].stack_size = Class->methods->attribute[codeIndex].info.Code_attribute.max_stack;
// frame_ptr[i - 1].local_size = Class->methods->attribute[codeIndex].info.Code_attribute.max_locals;
// frame_ptr[i - 1].stack = malloc(frame_ptr[i].stack_size * sizeof(u4));
// frame_ptr[i - 1].local = malloc(frame_ptr[i].local_size * sizeof(u4));
//
(*numFrames)++;
} else {
printf("Frame não pode ser alocado, tamanho máximo atingido");
exit(1);
}
}
//Lembrar de enviar o dataMSize->stkHeap_size para o numFrames
void deleteFrame(Frame *frame_ptr, u2 *numFrames) {
//free(frame_ptr);
(*numFrames)--;
}
/*SAMUEL - FIM*/