Skip to content

Commit 4cfba3e

Browse files
committed
2 parents 3d3f23a + a980fb8 commit 4cfba3e

33 files changed

Lines changed: 1608 additions & 704 deletions

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
*.out
33
*.exe
44
compilador
5+
compiler
6+
AssemblyCode.obj
57
/bin
68
all
79
.vscode
810
output.txt
9-
log.txt
11+
log.txt
12+
vm

AssemblyCode.obj

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
START
2+
ALLOC 0 1
3+
ALLOC 1 1
4+
JMP 1
5+
2 NULL
6+
ALLOC 2 1
7+
LDV 1
8+
LDC 1
9+
CEQ
10+
JMPF 3
11+
LDC 1
12+
JMP 4
13+
3 NULL
14+
LDV 1
15+
STR 2
16+
LDV 1
17+
LDC 1
18+
SUB
19+
STR 1
20+
LDV 2
21+
CALL 2
22+
LDV 0
23+
MULT
24+
4 NULL
25+
STR 0
26+
DALLOC 2 1
27+
RETURNF
28+
1 NULL
29+
RD
30+
STR 1
31+
CALL 2
32+
LDV 0
33+
STR 1
34+
LDV 1
35+
PRN
36+
DALLOC 0 2
37+
HLT

VirtualMachine/instruction.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "instruction.h"
5+
6+
7+
void insertInstruction(InstructionVector *instructionVector, char inst[21], int *InstructionAddr) {
8+
InstructionVector novo;
9+
10+
for(int i = 0, j = 0; i < 20; i++) {
11+
if(i == 4 || i == 12 || i == 16 || i == 20){
12+
j = 0;
13+
}
14+
if(i < 4)
15+
novo.label[j++] = inst[i];
16+
else if(i < 12)
17+
novo.instruction[j++] = inst[i];
18+
else if(i < 16)
19+
novo.param1[j++] = inst[i];
20+
else if(i < 20)
21+
novo.param2[j++] = inst[i];
22+
}
23+
novo.label[4] = '\0';
24+
novo.instruction[8] = '\0';
25+
novo.param1[4] = '\0';
26+
novo.param2[4] = '\0';
27+
instructionVector[(*InstructionAddr)] = novo;
28+
(*InstructionAddr) = *InstructionAddr + 1;
29+
}
30+
31+
int toInt(char *number) {
32+
int result = 0;
33+
for(int i = 0; number[i] >= 48 && number[i] <= 57; i++) {
34+
result += number[i] - 48;
35+
result *= 10;
36+
}
37+
result /= 10;
38+
return result;
39+
}
40+
41+
void printInstructions(InstructionVector *instructions, int size) {
42+
printf("INSTRUCTIONS\n");
43+
for(int i = 0; i < size; i++)
44+
printf("LABEL: %4.4s\t INST: %8.8s\tPARAM1: %4.4s\tPARAM2: %4.4s\n", instructions[i].label, instructions[i].instruction, instructions[i].param1, instructions[i].param2);
45+
printf("\n");
46+
}
47+
48+
49+
void readInstructions(FILE *file, InstructionVector *instructionVector, int *InstructionAddr) {
50+
char instruction[22];
51+
while(!feof(file)){
52+
fgets(instruction, 22, file);
53+
//printf("%s {SIZE = %d}", instruction, strlen(instruction));
54+
insertInstruction(instructionVector, instruction, InstructionAddr);
55+
}
56+
}
57+
58+
// void freeInstructions(InstructionVector **instructionVector) {
59+
// InstructionVector *aux = *instructionVector, *aux2 = NULL;
60+
// while(aux != NULL) {
61+
// aux2 = aux->next;
62+
// free(aux);
63+
// aux = aux2;
64+
// }
65+
// *instructionVector = NULL;
66+
// }

VirtualMachine/instruction.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef _INSTRUCTION_H_
2+
#define _INSTRUCTION_H_
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
typedef struct instructionVector {
8+
char label[5];
9+
char instruction[9];
10+
char param1[5];
11+
char param2[5];
12+
}InstructionVector;
13+
14+
void insertInstruction(InstructionVector *instructionVector, char inst[21], int *InstructionAddr);
15+
16+
void printInstructions(InstructionVector *instructions, int size);
17+
18+
// Function which collect all instructions from object to execute
19+
void readInstructions(FILE *file, InstructionVector *instructionVector, int *InstructionAddr);
20+
21+
int toInt(char *number);
22+
23+
void freeInstructions(InstructionVector **instructionVector);
24+
25+
#endif

VirtualMachine/main.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include "stack.h"
4+
#include "instruction.h"
5+
#include <stdbool.h>
6+
#include <string.h>
7+
8+
9+
bool isEqualString(char *str1, char *str2){
10+
return strcmp(str1, str2) == 0;
11+
}
12+
13+
int searchLabelIndex(InstructionVector *instructions, int numberOfInstructions, char *label){
14+
for(int i = 0; i < numberOfInstructions; i++){
15+
if(isEqualString(instructions[i].label, label))
16+
return i;
17+
}
18+
return -1;
19+
}
20+
21+
void executeProgram(InstructionVector *instructions, int numberOfInstructions) {
22+
int stack[500] = {0}, addr = 0, aux, instructionAddr = 0, n, m;
23+
24+
while(instructionAddr != numberOfInstructions){
25+
printf("\n\nInstruction: %s %s %s %s", instructions[instructionAddr].label, instructions[instructionAddr].instruction, instructions[instructionAddr].param1, instructions[instructionAddr].param2);
26+
getchar();
27+
if(isEqualString(instructions[instructionAddr].instruction, "LDC ")) {
28+
addr++;
29+
stack[addr] = toInt(instructions[instructionAddr].param1);
30+
} else if(isEqualString(instructions[instructionAddr].instruction, "LDV ")) {
31+
addr++;
32+
stack[addr] = stack[toInt(instructions[instructionAddr].param1)];
33+
} else if(isEqualString(instructions[instructionAddr].instruction, "ADD ")) {
34+
stack[addr-1] = stack[addr-1] + stack[addr];
35+
addr--;
36+
} else if(isEqualString(instructions[instructionAddr].instruction, "SUB ")) {
37+
stack[addr-1] = stack[addr-1] - stack[addr];
38+
addr--;
39+
} else if(isEqualString(instructions[instructionAddr].instruction, "MULT ")) {
40+
stack[addr-1] = stack[addr-1] * stack[addr];
41+
addr--;
42+
} else if(isEqualString(instructions[instructionAddr].instruction, "DIVI ")) {
43+
stack[addr-1] = stack[addr-1] / stack[addr];
44+
addr--;
45+
} else if(isEqualString(instructions[instructionAddr].instruction, "INV ")) {
46+
stack[addr] = - stack[addr];
47+
} else if(isEqualString(instructions[instructionAddr].instruction, "AND ")) {
48+
if(stack[addr-1] == 1 && stack[addr] == 1){
49+
stack[addr-1] = 1;
50+
}else{
51+
stack[addr-1] = 0;
52+
}
53+
addr--;
54+
} else if(isEqualString(instructions[instructionAddr].instruction, "OR ")) {
55+
if(stack[addr-1] == 1 || stack[addr] == 1){
56+
stack[addr-1] = 1;
57+
}else{
58+
stack[addr-1] = 0;
59+
}
60+
addr--;
61+
} else if(isEqualString(instructions[instructionAddr].instruction, "NEG ")) {
62+
stack[addr] = 1 - stack[addr];
63+
} else if(isEqualString(instructions[instructionAddr].instruction, "CME ")) {
64+
if(stack[addr-1] < stack[addr]){
65+
stack[addr-1] = 1;
66+
}else{
67+
stack[addr-1] = 0;
68+
}
69+
addr--;
70+
} else if(isEqualString(instructions[instructionAddr].instruction, "CMA ")) {
71+
if(stack[addr-1] > stack[addr]){
72+
stack[addr-1] = 1;
73+
}else{
74+
stack[addr-1] = 0;
75+
}
76+
addr--;
77+
} else if(isEqualString(instructions[instructionAddr].instruction, "CEQ ")) {
78+
if(stack[addr-1] == stack[addr]){
79+
stack[addr-1] = 1;
80+
}else{
81+
stack[addr-1] = 0;
82+
}
83+
addr--;
84+
} else if(isEqualString(instructions[instructionAddr].instruction, "CDIF ")) {
85+
if(stack[addr-1] != stack[addr]){
86+
stack[addr-1] = 1;
87+
}else{
88+
stack[addr-1] = 0;
89+
}
90+
addr--;
91+
} else if(isEqualString(instructions[instructionAddr].instruction, "CMEQ ")) {
92+
if(stack[addr-1] <= stack[addr]){
93+
stack[addr-1] = 1;
94+
}else{
95+
stack[addr-1] = 0;
96+
}
97+
addr--;
98+
} else if(isEqualString(instructions[instructionAddr].instruction, "CMAQ ")) {
99+
if(stack[addr-1] >= stack[addr]){
100+
stack[addr-1] = 1;
101+
}else{
102+
stack[addr-1] = 0;
103+
}
104+
addr--;
105+
} else if(isEqualString(instructions[instructionAddr].instruction, "STR ")) {
106+
stack[toInt(instructions[instructionAddr].param1)] = stack[addr];
107+
addr--;
108+
} else if(isEqualString(instructions[instructionAddr].instruction, "JMP ")) {
109+
aux = searchLabelIndex(instructions, numberOfInstructions, instructions[instructionAddr].param1);
110+
instructionAddr = aux;//Instrução logo após o "LABEL NULL";
111+
} else if(isEqualString(instructions[instructionAddr].instruction, "JMPF ")) {
112+
if(stack[addr] == 0){
113+
aux = searchLabelIndex(instructions, numberOfInstructions, instructions[instructionAddr].param1);
114+
instructionAddr = aux;//Instrução logo após o "LABEL NULL";
115+
}
116+
addr--;
117+
} else if(isEqualString(instructions[instructionAddr].instruction, "RD ")) {
118+
addr++;
119+
scanf("%d", &aux);
120+
stack[addr] = aux;
121+
} else if(isEqualString(instructions[instructionAddr].instruction, "PRN ")) {
122+
printf("\n\n\n\n\n\n%d", stack[addr]);
123+
addr--;
124+
} else if(isEqualString(instructions[instructionAddr].instruction, "START ")) {
125+
addr--;
126+
} else if(isEqualString(instructions[instructionAddr].instruction, "ALLOC ")) {
127+
printf("\nANTES ALLOC\n\n");
128+
printStack(stack, addr);
129+
m = toInt(instructions[instructionAddr].param1);
130+
n = toInt(instructions[instructionAddr].param2);
131+
for(int k = 0; k < n; k++){
132+
addr++;
133+
stack[addr] = stack[m + k];
134+
}
135+
printf("\nDEPOIS ALLOC\n\n");
136+
printStack(stack, addr);
137+
} else if(isEqualString(instructions[instructionAddr].instruction, "DALLOC ")) {
138+
printf("\nANTES DALLOC\n\n");
139+
printStack(stack, addr);
140+
m = toInt(instructions[instructionAddr].param1);
141+
n = toInt(instructions[instructionAddr].param2);
142+
for(int k = n - 1; k >= 0; k--){
143+
stack[m+k] = stack[addr];
144+
addr--;
145+
}
146+
printf("\nDEPOIS DALLOC\n\n");
147+
printStack(stack, addr);
148+
} else if(isEqualString(instructions[instructionAddr].instruction, "HLT ")) {
149+
return;
150+
} else if(isEqualString(instructions[instructionAddr].instruction, "CALL ")) {
151+
addr++;
152+
stack[addr] = instructionAddr+1;
153+
aux = searchLabelIndex(instructions, numberOfInstructions, instructions[instructionAddr].param1);
154+
instructionAddr = aux;//Instrução logo após o "LABEL NULL";
155+
} else if(isEqualString(instructions[instructionAddr].instruction, "RETURN ")) {
156+
instructionAddr = (stack[addr] - 1); //Atualiza no final
157+
addr--;
158+
} else if(isEqualString(instructions[instructionAddr].instruction, "RETURNF ")) { //VERIFICAR SE Ë ESSE MESMO O RETORNO QUE PRECISA
159+
instructionAddr = stack[addr] - 1;
160+
addr--;
161+
}
162+
163+
instructionAddr++;
164+
}
165+
}
166+
167+
int main() {
168+
FILE *sourceFile;
169+
int instructionAddr = 0;
170+
sourceFile = fopen("AssemblyCode.obj", "r");
171+
172+
if(!sourceFile) {
173+
printf("\nError! File could not be opened!\n");
174+
exit(1);
175+
}
176+
177+
InstructionVector instructions[2048] = {0};
178+
// Stack *stack = NULL;
179+
180+
readInstructions(sourceFile, instructions, &instructionAddr);
181+
182+
//printInstructions(instructions, instructionAddr);
183+
184+
executeProgram(instructions, instructionAddr);
185+
186+
fclose(sourceFile);
187+
// freeInstructions(&instructions);
188+
// freeStack(&stack);
189+
190+
return 0;
191+
}

VirtualMachine/stack.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "stack.h"
2+
3+
void printStack(int *s, int addr) {
4+
printf("\nSTACK");
5+
while(addr != -1) {
6+
printf("\n%d", s[addr]);
7+
addr--;
8+
}
9+
}
10+
11+
// void freeStack(Stack **st) {
12+
// Stack *aux = (*st), *aux2;
13+
// while(aux != NULL){
14+
// aux2 = aux->next;
15+
// free(aux);
16+
// aux = aux2;
17+
// }
18+
// *st = aux;
19+
// }

VirtualMachine/stack.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _STACK_H_
2+
#define _STACK_H_
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
// //Simple stack functions (Posfix conversion)
8+
// typedef struct Stack {
9+
// int value;
10+
// struct Stack *next;
11+
// }Stack;
12+
13+
14+
// void push(Stack **stack, int value);
15+
16+
// int pop(Stack **stack);
17+
18+
void printStack(int *s, int addr);
19+
20+
// void freeStack(Stack **st);
21+
#endif

VirtualMachine/vm

48.7 KB
Binary file not shown.

exec.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mkdir bin
22
cd src
3-
gcc -c verifyChar.c -o ../bin/verifyChar.o && gcc -c symbol.c -o ../bin/symbol.o && gcc -c error.c -o ../bin/error.o && gcc -c token.c -o ../bin/token.o && gcc -c lexico.c -o ../bin/lexico.o && gcc -c sintatico.c -o ../bin/sintatico.o && gcc -c main.c -o ../bin/main.o
3+
gcc -c verifyChar.c -o ../bin/verifyChar.o && gcc -c codeGeneration.c -o ../bin/codeGeneration.o && gcc -c symbol.c -o ../bin/symbol.o && gcc -c error.c -o ../bin/error.o && gcc -c token.c -o ../bin/token.o && gcc -c lexic.c -o ../bin/lexic.o && gcc -c syntatic.c -o ../bin/syntatic.o && gcc -c main.c -o ../bin/main.o
44
cd ../bin
5-
gcc *.o -o ../compilador
5+
gcc *.o -o ../compiler

0 commit comments

Comments
 (0)