-
Notifications
You must be signed in to change notification settings - Fork 1
/
virtualMachine.c
600 lines (558 loc) · 13.3 KB
/
virtualMachine.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define TAILLE_PILE 400
#define TAILLE_PCODE 400
#define debug 0
// structures pour le chargement de code -- les tokens --
typedef enum {
NUM, ADD, SUB, MUL, DIV, EQL, NEQ,
GTR, LSS, GEQ, LEQ, PRN, INN, INT,
LDI, LDA, LDV ,STO, BRN, BZE, HLT,AND, OR, MOD,
ERREUR_TOKEN, FIN_TOKEN
}CODE_LEX;
typedef struct {
CODE_LEX CODE;
char NOM[20]; //peut contenir des nombres
} Token;
Token tabToken[] = {
{ADD, "ADD"},
{SUB, "SUB"},
{MUL, "MUL"},
{DIV, "DIV"},
{EQL, "EQL"},
{NEQ, "NEQ"},
{GTR, "GTR"},
{LSS, "LSS"},
{GEQ, "GEQ"},
{LEQ, "LEQ"},
{PRN, "PRN"},
{INN, "INN"},
{INT, "INT"},
{LDI, "LDI"},
{LDA, "LDA"},
{LDV, "LDV"},
{STO, "STO"},
{BRN, "BRN"},
{BZE, "BZE"},
{HLT, "HLT"},
{AND, "AND"},
{OR, "OR"},
{MOD, "MOD"},
//{NUM_TOKEN, NUM}
// {FIN_TOKEN, EOF}
};
int nbMotRes = sizeof tabToken / sizeof tabToken[0];
char Car_Cour;
Token SYM_COUR;
// structures pour le remplissage de la table du PCODE
typedef struct {
CODE_LEX MNE;
int SUITE;
}INSTRUCTION;
INSTRUCTION PCODE[TAILLE_PCODE];
int PC = -1;
int OFFSET=-1; //OFFSET
//---------------------- GESTION DES ERREURS --------------------
typedef enum{
ADD_ERR,
SUB_ERR,
MUL_ERR,
DIV_ERR,
EQL_ERR,
NEQ_ERR,
GTR_ERR,
LSS_ERR,
GEQ_ERR,
LEQ_ERR,
PRN_ERR,
INN_ERR,
INT_ERR,
LDI_ERR,
LDA_ERR,
LDV_ERR,
STO_ERR,
BRN_ERR,
BZE_ERR,
HLT_ERR,
AND_ERR,
OR_ERR,
MOD_ERR,
ERR_FICH_VID,
ERR_ID_INV,
ERR_NBR_LONG,
NUM_ERR,
GENCODE_TAILLE_ERR,
INST_PCODE_ERR
}Erreurs_t;
typedef struct {
Erreurs_t CODE_ERR;
char mes[40];
} Erreurs;
Erreurs MES_ERR[] = {
{ADD_ERR, "ADD_ERR"},
{SUB_ERR, "SUB_ERR"},
{MUL_ERR, "MUL_ERR"},
{DIV_ERR, "DIV_ERR"},
{EQL_ERR, "EQL_ERR"},
{NEQ_ERR, "NEQ_ERR"},
{GTR_ERR, "GTR_ERR"},
{LSS_ERR, "LSS_ERR"},
{GEQ_ERR, "GEQ_ERR"},
{LEQ_ERR, "LEQ_ERR"},
{PRN_ERR, "PRN_ERR"},
{INN_ERR, "INN_ERR"},
{INT_ERR, "INT_ERR"},
{LDI_ERR, "LDI_ERR"},
{LDA_ERR, "LDA_ERR"},
{LDV_ERR, "LDV_ERR"},
{STO_ERR, "STO_ERR"},
{BRN_ERR, "BRN_ERR"},
{BZE_ERR, "BZE_ERR"},
{HLT_ERR, "HLT_ERR"},
{AND_ERR, "AND_ERR"},
{OR_ERR, "OR_ERR"},
{MOD_ERR, "MOD_ERR"},
{NUM_ERR, "NUM_ERR"},
{ ERR_FICH_VID, "FICHIER: vide"},
{ERR_ID_INV, "IDF: non valide "},
{ERR_NBR_LONG, "NUMBER: tres long"},
{GENCODE_TAILLE_ERR, "GENCODE_TAILLE_ERR"},
{INST_PCODE_ERR, "INST_PCODE_ERR"}
};
int tailleERR = sizeof MES_ERR / sizeof MES_ERR[0];
//signatures des fonctoins utilisees
void showCodeToken(Token token);
int estBlanc(char c);
void lire_Car();
void Lire_MNE();
void Lire_Suite();
int detectError( Erreurs_t er);
void nextToken(void);
// ===================== la structure pile ==================
int MEM[TAILLE_PILE];
int SP = -1; // tete de la pile
// =====================interpretation du pcode ======================
void INTER_PCODE();
void INTER_INST(INSTRUCTION INST);
// ====================== chargement du pcode =======================
void ECRIRE1(CODE_LEX MNE);
void ECRIRE2(CODE_LEX MNE, int A);
void LOADER();
void showLoadCode();
void showMNE(CODE_LEX mne);
// ====================== analyse syntaxique ========================
void PCODE_NT(); // le non terminal pcode
void INST_PCODE();
FILE *FL_in = NULL;
int main(int argc, char* argv[]){
if (!argv[1]) {
printf("fichier n'exste pas\n");
exit(EXIT_FAILURE);
}
FL_in = fopen(argv[1], "r");
LOADER();
fclose(FL_in);
showLoadCode();
INTER_PCODE();
// printf("SP = %d OFFSET = %d PC = %d\n", SP, OFFSET, PC);
return 0;
}
// =====================interpretation du pcode ======================
void INTER_PCODE(){
PC=0;
while (PCODE[PC].MNE!=HLT){
if(debug){
printf("======= ");
showMNE(PCODE[PC].MNE);
printf(" =======\n");
}
INTER_INST(PCODE[PC]);
if( debug){
printf("----- SP = %d\tPC = %d\n", SP, PC);
for(int i = 0; i <= SP; i++){
printf("MEM[%d] %d\n",i, MEM[i]);
}
}
}
}
void INTER_INST(INSTRUCTION INST){
int val1, adr, val2;
switch(INST.MNE){
case INT:
OFFSET=SP=INST.SUITE;
PC++;
break;
case LDI:
MEM[++SP]=INST.SUITE;
PC++;
break;
case LDA:
MEM[++SP]=INST.SUITE;
PC++;
break;
case STO:
val1=MEM[SP--];
adr=MEM[SP--];
MEM[adr]=val1;
PC++;
break;
case LDV:
adr=MEM[SP--];
MEM[++SP]=MEM[adr];
PC++;
break;
case EQL:
val2=MEM[SP--];
val1=MEM[SP--];
MEM[++SP]=(val1==val2);
PC++;
break;
case NEQ:
val2=MEM[SP--];
val1=MEM[SP--];
MEM[++SP]=(val1 != val2);
PC++;
break;
case LSS:
val2=MEM[SP--];
val1=MEM[SP--];
MEM[++SP]=(val1<val2);
PC++;
break;
case LEQ:
val2=MEM[SP--];
val1=MEM[SP--];
MEM[++SP]=(val1<=val2);
PC++;
break;
case GTR:
val2=MEM[SP--];
val1=MEM[SP--];
MEM[++SP]=(val1>val2);
PC++;
break;
case GEQ:
val2=MEM[SP--];
val1=MEM[SP--];
MEM[++SP]=(val1>=val2);
PC++;
break;
case ADD:
val2 = MEM[SP--];
val1 = MEM[SP--];
MEM[++SP] = (val1+ val2);
PC++;
break;
case SUB:
val2 = MEM[SP--];
val1 = MEM[SP--];
MEM[++SP] = (val1- val2);
PC++;
break;
case MUL:
val2 = MEM[SP--];
val1 = MEM[SP--];
MEM[++SP] = (val1* val2);
PC++;
break;
case DIV:
val2 = MEM[SP--];
val1 = MEM[SP--];
MEM[++SP] = (val1/ val2);
PC++;
break;
case INN:
adr = MEM[SP--];
scanf("%d", &val1);
MEM[adr] = val1;
PC++;
break;
case PRN:
printf("%d\n", MEM[SP--]);
PC++; break;
case BZE:
if (MEM[SP--]==0)
PC=INST.SUITE;
else PC++;
break;
case BRN:
PC=INST.SUITE;
break;
case AND:
val2 = MEM[SP--];
val1 = MEM[SP--];
MEM[++SP] = (val1 && val2);
PC++;
break;
case OR:
val2 = MEM[SP--];
val1 = MEM[SP--];
MEM[++SP] = (val1 || val2);
PC++;
break;
case MOD:
val2 = MEM[SP--];
val1 = MEM[SP--];
MEM[++SP] = (val1 % val2);
PC++;
break;
}
}
// ====================== chargement du pcode =======================
void ECRIRE1(CODE_LEX MNE){
if (PC == TAILLE_PCODE) {
printf("GENCODE_TAILLE_ERR");
exit(EXIT_FAILURE);
}
PC++;
PCODE[PC].MNE = MNE;
}
void ECRIRE2(CODE_LEX MNE, int A){
if (PC == TAILLE_PCODE) {
printf("GENCODE_TAILLE_ERR");
exit(EXIT_FAILURE);
}
PC++;
PCODE[PC].MNE = MNE;
PCODE[PC].SUITE = A;
}
void LOADER(){
nextToken();
PCODE_NT();
if( SYM_COUR.CODE = FIN_TOKEN){
puts("BRAVO =============");
}
else{
puts("PAS BRAVO :((((((((((((((((((((((((((((((");
}
}
// ============ syntaxique ================
void Test_Symbole (CODE_LEX cl, Erreurs_t COD_ERR){
if (SYM_COUR.CODE == cl){
nextToken();
}
else{
detectError(COD_ERR);
}
}
void PCODE_NT(){
// PCODE_NT ::= INT NUM {INST_PCODE} HLT
// printf("SYM_CUR %s\n", SYM_COUR.NOM);
Token INT_tk = SYM_COUR;
Test_Symbole(INT, INT_ERR);
Token NUM_tk = SYM_COUR;
Test_Symbole(NUM, NUM_ERR);
ECRIRE2(INT_tk.CODE, atoi(NUM_tk.NOM));
while(SYM_COUR.CODE != HLT){
INST_PCODE();
}
ECRIRE1(HLT);
Test_Symbole(HLT, HLT_ERR);
}
void INST_PCODE(){
// INST_PCODE ::= ADD | SUB|EQL|…| [LDA | BZE|BRN|LDI] NUM
CODE_LEX mne = SYM_COUR.CODE;
switch (mne){
case ADD:
case SUB:
case MUL:
case DIV:
case EQL:
case NEQ:
case GTR:
case LSS:
case GEQ:
case LEQ:
case PRN:
case INN:
case LDV:
case STO:
case AND:
case OR:
case MOD:
ECRIRE1(mne);
nextToken();
break;
case LDA:
case LDI:
case BZE:
case BRN:
nextToken(); //suite
ECRIRE2(mne, atoi(SYM_COUR.NOM));
Test_Symbole(NUM, NUM_ERR); // juste pour verifier et passer au next token
break;
case HLT: break;
default:
detectError(INST_PCODE_ERR);
break;
}
}
void showLoadCode(){
for(int i = 0; i <= PC; i++){
printf("%4d ", i);
showMNE(PCODE[i].MNE);
printf(" %d\n", PCODE[i].SUITE);
}
}
void showMNE(CODE_LEX mne){
switch (mne){
case ADD:
printf("ADD");
break;
case SUB:
printf("SUB");
break;
case MUL:
printf("MUL");
break;
case DIV:
printf("DIV");
break;
case EQL:
printf("EQL");
break;
case NEQ:
printf("NEQ");
break;
case GTR:
printf("GTR");
break;
case LSS:
printf("LSS");
break;
case GEQ:
printf("GEQ");
break;
case LEQ:
printf("LEQ");
break;
case PRN:
printf("PRN");
break;
case INN:
printf("INN");
break;
case INT:
printf("INT");
break;
case LDI:
printf("LDI");
break;
case LDA:
printf("LDA");
break;
case LDV:
printf("LDV");
break;
case STO:
printf("STO");
break;
case BRN:
printf("BRN");
break;
case BZE:
printf("BZE");
break;
case HLT:
printf("HLT");
break;
case AND:
printf("AND");
break;
case OR:
printf("OR");
break;
case MOD:
printf("MOD");
break;
default:
printf("Unknown");
break;
}
}
//=================================== lexical================
int estBlanc(char c) {
return c == ' ' || c == '\t' || c == '\n';
}
void lire_Car(){
Car_Cour = getc(FL_in);
}
void Lire_MNE(){
int lonLex = 0;
SYM_COUR.NOM[lonLex++] = Car_Cour;
lire_Car();
while (isalpha(Car_Cour)) {
SYM_COUR.NOM[lonLex++] = Car_Cour;
lire_Car();
}
//fin du mot
SYM_COUR.NOM[lonLex++] = '\0';
ungetc(Car_Cour, FL_in);
int i = 0;
while( (i < nbMotRes) && (strcasecmp(SYM_COUR.NOM, tabToken[i].NOM) != 0) ){
i++;
}
if( i< nbMotRes) {
SYM_COUR.CODE = tabToken[i].CODE;
}
else{
detectError(INST_PCODE_ERR);
}
}
void Lire_Suite(){
int lonLex = 0;
SYM_COUR.NOM[lonLex++] = Car_Cour;
lire_Car();
if( Car_Cour == '+'|| Car_Cour == '-'){
SYM_COUR.NOM[lonLex++] = Car_Cour;
lire_Car();
}
while ( isdigit(Car_Cour) ) {
SYM_COUR.NOM[lonLex++] = Car_Cour;
lire_Car();
}
if ( isalpha(Car_Cour) ) {
while ( isalnum(Car_Cour) ) {
SYM_COUR.NOM[lonLex++] = Car_Cour;
lire_Car();
}
detectError(ERR_ID_INV);
SYM_COUR.CODE = ERREUR_TOKEN;
}
else if (lonLex <= 11) {
SYM_COUR.CODE = NUM;
}
else{ // Nombre est tres long
detectError(ERR_NBR_LONG);
SYM_COUR.CODE = ERREUR_TOKEN;
}
//fin du mot
SYM_COUR.NOM[lonLex++] = '\0';
ungetc(Car_Cour, FL_in);
}
int detectError( Erreurs_t er){
int i = 0;
for(; i < tailleERR, er != MES_ERR[i].CODE_ERR; i++);
printf("erreur %s\n", MES_ERR[i].mes);
exit(EXIT_FAILURE);
}
void nextToken(void) {
lire_Car();
memset(SYM_COUR.NOM, '\0', 20); //new word(token)
while (estBlanc(Car_Cour))
lire_Car();
if( isdigit( Car_Cour) || Car_Cour == '+'|| Car_Cour== '-'){
Lire_Suite();
}
else if( isalpha( Car_Cour) ){
Lire_MNE();
}
else if( Car_Cour == EOF){
SYM_COUR.CODE = FIN_TOKEN;
}
else {
detectError(INST_PCODE_ERR);
}
}