-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjvm.c
530 lines (454 loc) · 17.3 KB
/
jvm.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include "structs.h"
#include "opcode.h"
#include "frame_stack.h"
#include "loader.h"
#include "linker.h"
#include "initializer.h"
#define MAX_NUM_OF_CLASSES 65535
pc_t jvm_pc = {0, 0, 0, 0};
frame_stack_t *jvm_stack = NULL;
extern void (*jvm_opcode[])(void);
// Array com as classes na memória
int jvm_number_of_classes = 0;
class_t *jvm_classes[MAX_NUM_OF_CLASSES];
/** \addtogroup JVM-Core
* @{
*/
/**
* @brief Cria a estrutura class_t da classe com nome class_name
*
* @param Nome da classe em UTF8
* @return Estrutura da classe criada
*/
class_t *createClass(Utf8_info_t* class_name) {
int index = jvm_number_of_classes;
jvm_number_of_classes++;
jvm_classes[index] = (class_t*) malloc(sizeof(class_t));
jvm_classes[index]->class_name = class_name;
jvm_classes[index]->status = CLASSE_NAO_CARREGADA;
return jvm_classes[index];
}
/**
* @brief Retorna a classe com nome class_name
*
* @param Nome da classe em UTF8
* @return Classe procurada
*
* @see compare_utf8, createClass
*/
class_t *getClass(Utf8_info_t* class_name) {
DEBUG_PRINT("got into getClass with arguments: %s\n", utf8_to_string(class_name));
if(compare_utf8(class_name, string_to_utf8("java/lang/Object")) == 0) {
return NULL;
}
int i = 0;
for (i = 0; i < jvm_number_of_classes; ++i) {
if(compare_utf8(jvm_classes[i]->class_name, class_name) == 0) {
return jvm_classes[i];
}
}
return createClass(class_name);
}
/**
* @brief Retorna a classe pai da classe sub_class
*
* @param Classe subclasse
* @return Classe superclasse
*
* @see getClass
*/
class_t* getSuperClass(class_t* sub_class) {
DEBUG_PRINT("got into getSuperClass with arguments: %s\n", utf8_to_string(sub_class->class_name));
u2 class_name_index = sub_class->class_file.constant_pool[sub_class->class_file.super_class].info.Class.name_index;
return getClass(&(sub_class->class_file.constant_pool[class_name_index].info.Utf8));
}
/**
* @brief Retorna se as classes são as mesmas
*
* @param Classe
* @param Classe
* @return 1 ou 0
*
* @see compare_utf8
*/
int isSameClass(class_t* a, class_t* b) {
return (compare_utf8(a->class_name, b->class_name) == 0);
}
/**
* @brief Retorna se a classe super_class é uma classe ancestral de sub_class
*
* @param Classe
* @param Classe
* @return 1 ou 0
*
* @see string_to_utf8, compare_utf8, getSuperClass, isSameClass
*/
int isSuperClassOf(class_t* super_class, class_t* sub_class) {
class_t* class = sub_class;
while(compare_utf8(string_to_utf8("java/lang/Object"), class->class_name) != 0) {
if(isSameClass(super_class, getSuperClass(class))) {
return 1;
}
class = getSuperClass(class);
}
return 0;
}
// CLASS_T STUFF - END
// METHOD STUFF - BEGIN
/**
* @brief Retorna Code attribute do método method da classe class.
*
* @param Classe do método
* @param Método do code attribute
* @return Code attribute
*/
code_attribute_t* getCodeAttribute(class_t* class, method_info_t* method) {
/*Utf8_info_t* method_name = &(class->class_file.constant_pool[method->name_index].info.Utf8);*/
/*DEBUG_PRINT("got into getCodeAttribute with arguments: %s , %s\n", utf8_to_string(class->class_name), utf8_to_string(method_name));*/
int i = 0;
for (i = 0; method->attributes_count; i++) {
attribute_info_t* attribute = &(method->attributes[i]);
u1* b = class->class_file.constant_pool[attribute->attribute_name_index].info.Utf8.bytes;
u2 length = class->class_file.constant_pool[attribute->attribute_name_index].info.Utf8.length;
if (strncmp("Code", (char*) b, length) == 0) {
return &(attribute->info.code);
}
}
printf("ERROR: Could not find Code attribute\n");
exit(1);
}
/**
* @brief Retorna se método retorna algo
*
* @param Classe do método
* @param Método
* @return 1 ou 0
*/
int hasReturnValue(class_t* class, method_info_t* method) {
u1* b = class->class_file.constant_pool[method->descriptor_index].info.Utf8.bytes;
u2 length = class->class_file.constant_pool[method->descriptor_index].info.Utf8.length;
int i = 0;
for (i = 0; i < length; i++) {
if (b[i] == 'V') {
return 0;
}
}
return 1;
}
/**
* @brief Retorna método com nome method_name da classe sem ser recursivo
*
* @param Classe do método
* @param Nome do método em utf8
* @param Descriptor do método em utf8
* @return Método
*
* @see compare_utf8
*/
method_info_t* getMethodOnThisClass(class_t* class, Utf8_info_t* method_name, Utf8_info_t* descriptor) {
DEBUG_PRINT("got into getMethodOnThisClass with arguments: %s, %s, %s\n", utf8_to_string(class->class_name), utf8_to_string(method_name), utf8_to_string(descriptor));
int i = 0;
for (i = 0; i < class->class_file.methods_count; i++) {
method_info_t* method = &(class->class_file.methods[i]);
Utf8_info_t* method_name_aux = &(class->class_file.constant_pool[method->name_index].info.Utf8);
if (compare_utf8(method_name, method_name_aux) == 0) {
Utf8_info_t* descriptor_aux = &(class->class_file.constant_pool[method->descriptor_index].info.Utf8);
if (compare_utf8(descriptor, descriptor_aux) == 0) {
DEBUG_PRINT("Done with getMethodOnThisClass with arguments: %s, %s, %s. Found method.\n", utf8_to_string(class->class_name), utf8_to_string(method_name), utf8_to_string(descriptor));
return method;
}
}
}
DEBUG_PRINT("Done with getMethodOnThisClass with arguments: %s, %s, %s. DID NOT Found method.\n", utf8_to_string(class->class_name), utf8_to_string(method_name), utf8_to_string(descriptor));
return NULL;
}
/**
* @brief Retorna método com nome method_name da classe fazendo resolução completa
*
* @param Classe do método
* @param Nome do método em utf8
* @param Descriptor do método em utf8
* @return Método
*
* @see getSuperClass, getMethodOnThisClass
*/
method_info_t* getMethod(class_t* class, Utf8_info_t* method_name, Utf8_info_t* descriptor) {
DEBUG_PRINT("got into getMethod with arguments: %s, %s, %s\n", utf8_to_string(class->class_name), utf8_to_string(method_name), utf8_to_string(descriptor));
if (class->status == CLASSE_NAO_CARREGADA) {
loadClass(class);
}
if (class->status == CLASSE_NAO_LINKADA) {
linkClass(class);
}
if (class->status == CLASSE_NAO_INICIALIZADA) {
initializeClass(class);
}
method_info_t* method = getMethodOnThisClass(class, method_name, descriptor);
while (method == NULL) {
class = getSuperClass(class);
if (class == NULL) {
printf("ERROR: Method %s not found\n", utf8_to_string(method_name));
exit(1);
}
if ((method->access_flags & ACC_PRIVATE) == ACC_PRIVATE) {
printf("ERROR: Founded method is private\n");
exit(1);
}
}
DEBUG_PRINT("Done with getMethod with arguments: %s, %s, %s\n", utf8_to_string(class->class_name), utf8_to_string(method_name), utf8_to_string(descriptor));
return method;
}
/**
* @brief Retorna o número de argumentos que o método precisa
*
* @param Classe do método
* @param Método
* @return Número de argumentos
*/
int getNumberOfArguments(class_t* class, method_info_t* method) {
u1* b = class->class_file.constant_pool[method->descriptor_index].info.Utf8.bytes;
u2 length = class->class_file.constant_pool[method->descriptor_index].info.Utf8.length;
int counter = 0;
if ((method->access_flags & ACC_STATIC) != ACC_STATIC) {
counter++;
}
int i = 1;
for (i = 1; i < length; i++) {
switch (b[i]) {
case 'B': //byte
case 'C': //char
case 'D': //double
case 'F': //float
case 'I': //integer
case 'J': //long
case 'S': //short
case 'Z': //boolean
counter++;
break;
case 'L': //reference
for(;i < length && b[i] != ';'; i++); //go until ';'
counter++;
break;
case '[': //reference - array
break;
case ')':
return counter;
default:
printf("Unexpected char on method descriptor: %c\n", b[i]);
exit(1);
}
}
printf("ERROR: Could not find ')' in method description");
exit(1);
}
// METHOD STUFF - END
// JVM OPERATION STUFF - START
/**
* @brief Levanta uma exceção
*
* @param Classe da exceção
*
* @see getClass, isSameClass, isSuperClassOf, getCodeAttribute
*/
void throwException(class_t* exception_class) {
// check for handlers
code_attribute_t* code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u2 exception_table_length = code_attribute->exception_table_length;
u2 i = 0;
for (i = 0; i< exception_table_length; i++) {
u2 catch_exception_class_index = jvm_pc.currentClass->class_file.constant_pool[code_attribute->exception_table[i].catch_type].info.Class.name_index;
class_t* catch_exception_class = getClass(&(jvm_pc.currentClass->class_file.constant_pool[catch_exception_class_index].info.Utf8));
if (code_attribute->exception_table[i].catch_type == 0 || (isSameClass(exception_class, catch_exception_class) || isSuperClassOf(catch_exception_class, exception_class))) {
if (code_attribute->exception_table[i].start_pc >= jvm_pc.code_pc && code_attribute->exception_table[i].end_pc < jvm_pc.code_pc) {
// Executar catch code
jvm_pc.code_pc = code_attribute->exception_table[i].handler_pc;
return;
}
}
}
//Exception não foi tratada
// pop stack
frame_t *frame = pop_frame_stack(&jvm_stack);
if (frame == NULL) {
printf("Exception não foi tratada. JVM será terminada.\n");
exit(1);
}
// change PC
jvm_pc = frame->return_address;
// re-throw exception
throwException(exception_class);
free(frame);
}
/**
* @brief Retorna da função atual
*
* @see hasReturnValue
*/
void returnFromFunction() {
DEBUG_PRINT("got into returnFromFunction\n");
// pop stack
frame_t *frame = pop_frame_stack(&jvm_stack);
Utf8_info_t* current_method_name = &(frame->current_class->class_file.constant_pool[frame->current_method->name_index].info.Utf8);
if (compare_utf8(current_method_name, string_to_utf8("<clinit>")) == 0) {
// Executou clinit da classe da funcao main
return;
} else if (compare_utf8(current_method_name, string_to_utf8("main")) == 0) {
// Execução retornou da função main. Programa executado com sucesso
exit(0);
}
// change PC
jvm_pc = frame->return_address;
DEBUG_PRINT("head=%d\n", frame->operand_stack.head);
// put return value on the new frame's operand stack if any
if(hasReturnValue(frame->current_class, frame->current_method)) {
frame_t *invokerFrame = peek_frame_stack(jvm_stack);
any_type_t *operand = pop_operand_stack(&(frame->operand_stack));
push_operand_stack(&(invokerFrame->operand_stack), operand);
}
free(frame);
DEBUG_PRINT("done with returnFromFunction %s\n", utf8_to_string(current_method_name));
}
/**
* @brief Chama método
*
* @param Classe do método
* @param Método a ser chamado
*
* @see getCodeAttribute, getNumberOfArguments
*/
void callMethod(class_t* class, method_info_t* method) {
Utf8_info_t* method_name = &(class->class_file.constant_pool[method->name_index].info.Utf8);
DEBUG_PRINT("got into callMethod with arguments: %s , %s\n", utf8_to_string(class->class_name), utf8_to_string(method_name));
if (class->status == CLASSE_NAO_CARREGADA) {
loadClass(class);
}
if (class->status == CLASSE_NAO_LINKADA) {
linkClass(class);
}
if (class->status == CLASSE_NAO_INICIALIZADA && compare_utf8(method_name, string_to_utf8("<clinit>")) != 0) {
initializeClass(class);
}
frame_t *invokerFrame = peek_frame_stack(jvm_stack);
//get number of arguments from classfile
int number_of_arguments = getNumberOfArguments(class, method);
DEBUG_PRINT("number_of_arguments = %d\n", number_of_arguments);
// pop arguments from operand stack and
// insert them on local_var
int i = 0;
int local_var_index = 0;
operand_stack_t aux_operand_stack;
aux_operand_stack.depth = 0;
aux_operand_stack.head = -1;
aux_operand_stack.size = number_of_arguments;
aux_operand_stack.operand = (any_type_t**) malloc(number_of_arguments * sizeof(any_type_t**));
any_type_t* aux_objref = NULL;
for (i = 0; i < number_of_arguments; i++) {
any_type_t *operand = pop_operand_stack(&(invokerFrame->operand_stack));
push_operand_stack(&(aux_operand_stack), operand);
aux_objref = operand;
}
code_attribute_t* code_attribute = NULL;
if ((class->class_file.access_flags & ACC_INTERFACE) == ACC_INTERFACE) {
Utf8_info_t* descriptor = &(class->class_file.constant_pool[method->descriptor_index].info.Utf8);
DEBUG_PRINT("hello %s\n", utf8_to_string(descriptor));
class = aux_objref->val.reference_val.val.object.objClass;
method = getMethod(class, method_name, descriptor);
code_attribute = getCodeAttribute(class, method);
} else {
code_attribute = getCodeAttribute(class, method);
}
frame_t *frame = (frame_t*) malloc(sizeof(frame_t));
frame->current_class = class;
frame->current_method = method;
frame->return_address = jvm_pc;
frame->local_var.size = code_attribute->max_locals;
frame->local_var.var = (any_type_t*) malloc(frame->local_var.size * sizeof(any_type_t));
frame->operand_stack.depth = 0;
frame->operand_stack.head = -1;
frame->operand_stack.size = code_attribute->max_stack;
frame->operand_stack.operand = (any_type_t**) malloc(frame->operand_stack.size * sizeof(any_type_t**));
for (i = 0; i < number_of_arguments; i++) {
any_type_t *operand = pop_operand_stack(&(aux_operand_stack));
memmove(&(frame->local_var.var[local_var_index]), operand, sizeof(any_type_t));
local_var_index++;
if(operand->val.primitive_val.tag == DOUBLE ||operand->val.primitive_val.tag == LONG)
local_var_index++;
}
free(aux_operand_stack.operand);
push_frame_stack(&jvm_stack, frame);
//set pc
jvm_pc.currentClass = class;
jvm_pc.method = method;
jvm_pc.code_pc = 0;
jvm_pc.jumped = 1;
DEBUG_PRINT("Done with callMethod with arguments: %s , %s\n", utf8_to_string(class->class_name), utf8_to_string(method_name));
return;
}
/** @} */
// JVM OPERATION STUFF - END
/**
* @brief Função main. Inicio da JVM
*
* @param Tamanho do argv
* @param Argumentos passados por linha de comando
*
* @see string_to_utf8, char_to_array_reference, createClass, getCodeAttribute, getMethod, goToNextOpcode, callMethod
*/
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Instrução: Rode com 'jvm class_identifier [args]'\n");
printf(" ou: 'jvm -p class_identifier'\n");
return 1;
}
if (strncmp(argv[1], "-p", 2) == 0) {
class_t *class = createClass(string_to_utf8(argv[2]));
loadClass(class);
MostraClasse(&(class->class_file));
return 0;
}
class_t *class = createClass(string_to_utf8(argv[1]));
// frame init
frame_t *frame = (frame_t*) malloc(sizeof(frame_t));
frame->current_class = class;
push_frame_stack(&jvm_stack, frame);
frame->current_method = getMethod(class, string_to_utf8("main"), string_to_utf8("([Ljava/lang/String;)V"));
frame->return_address.method = NULL;
frame->return_address.code_pc = 0;
frame->local_var.size = 0;
frame->local_var.var = NULL;
frame->operand_stack.depth = 0;
frame->operand_stack.head = -1;
frame->operand_stack.size = 1;
frame->operand_stack.operand = (any_type_t**) malloc(frame->operand_stack.size * sizeof(any_type_t**));
// Criar array de strings com argumentos passados (argv[2:]) e colocar na pilha de operandos
any_type_t *args = (any_type_t*) malloc(sizeof(any_type_t));
args->tag = REFERENCE;
args->val.reference_val.tag = ARRAY;
args->val.reference_val.val.array.length = argc-2;
args->val.reference_val.val.array.components = (any_type_t *) malloc(args->val.reference_val.val.array.length * sizeof(any_type_t));
uint32_t i = 0;
for (i = 0; i < args->val.reference_val.val.array.length; i++) {
args->val.reference_val.val.array.components[i] = *(char_to_array_reference(argv[i+2]));
}
push_operand_stack(&(frame->operand_stack), args);
// Chamar main
callMethod(frame->current_class, frame->current_method);
jvm_pc.jumped = 0;
code_attribute_t* code_attribute = NULL;
do {
//fetch opcode
code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
u1 opcode = code_attribute->code[jvm_pc.code_pc];
//execute the action for the opcode;
DEBUG_PRINT("Going to execute %#x at %d\n", opcode, jvm_pc.code_pc);
jvm_opcode[opcode]();
goToNextOpcode();
code_attribute = getCodeAttribute(jvm_pc.currentClass, jvm_pc.method);
} while(jvm_pc.code_pc < code_attribute->code_length);
return 0;
}