forked from gabrielmm1234/jvm-sb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
117 lines (98 loc) · 2.81 KB
/
main.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
/**
*@file
*@section DESCRIPTION
*Universidade de Brasilia
*
*Gabriel Mesquita de Araujo 13/0009121\n
*Gabriel Ferreira Silva 14/0140131\n
*Renato 13/0009121\n
*Leandro 13/0009121\n
*Carlos 13/0009121\n\n
*
* Software Basico - 1/2016\n
* Professor: Marcelo Ladeira\n\n
*
* Funcao main do processamento do arquivo .class
* e execucao das instrucoes -> JVM.
*
* para compilar: make clean && make\n
* para executar: ./jvm.exe .class 1 //Arquivo .class com a main e 1 para printar o classfile.\n
* ./jvm.exe .class 0 //Arquivo .class com a main e 0 para nao printar o classfile.\n
* ./jvm.exe //Programa pede pelo .class e opcao de print\n
*/
#include "./includes/exibidor.h"
#include "./includes/instrucao.h"
#include "./includes/carregador.h"
#include "./includes/metodo.h"
#define arqSize 100
/**
*Salva nome do arquivo passado via linha de comando.
*/
char* arquivo;
/**
*Flag para printar no prompt - 1 printa - 0 nao printa.
*/
int printPrompt = 0;
/**
* Inicio da execucao da jvm
* @param argumentos passados via linha de comando.
* @param Primeiro argumento eh o .class que contem a main
* @param Segundo argumento eh flag para printar no prompt 1-printa. 0-nao printa.
*/
int main(int argc, char* argv[]) {
arquivo = calloc(arqSize,sizeof(char));
/**
*Ponteiro para uma estrutura de method_info. Para comecar executando
*a main.
*/
method_info* Main;
/**
*1 - Verifica argumentos por linha de comando.
*/
if (argc < 3) {
printf("Forneca o .class que contem a main: \n");
scanf("%s", arquivo);
getchar();
printf("Deseja imprimir no prompt? 1 - SIM / 0 - NAO \n");
scanf("%d", &printPrompt);
}
else {
strcpy(arquivo, argv[1]);
if(*argv[2] == '1')
printPrompt = 1;
}
/**
*2 - Carrega no array de classes o object.class.
*/
carregaObjectClasse("java/lang/Object");
/**
*3 - Carrega no array de classes o .class passado por linha de comando.
*/
carregaMemClasse(arquivo);
classFile* mainClass = buscaClasseIndice(1);
/**
*4 - Busca o metodo main para comecar a execucao.
*/
Main = buscaMetodoMain();
//Se nao encontrou o main o .class passado esta errado.
if (Main == NULL) {
printf("Forneça um .class via linha de comando com o método main.");
return 0;
}
/**
*5 - Cria frame e coloca na pilha. Passa o metodo com o bytecode
*e a classe que contem o metodo com a constantPool.
*/
empilhaMetodo(Main, mainClass);
/**
*6 - Executa o metodo main que esta no topo da stackFrame.
*/
executaFrameCorrente();
/**
*7 - Se passa 1 na linha de comando imprime no prompt
*/
if (printPrompt)
for (int i = 1; i < area_met.num_classes; i++)
imprimePrompt(area_met.array_classes[i]);
return 0;
}