Arnaud Vergnet 3 years ago
parent
commit
a1c0f7a19f
7 changed files with 128 additions and 34 deletions
  1. 44
    7
      al.lex
  2. 26
    23
      as.y
  3. 4
    0
      asm_instructions.c
  4. 3
    0
      asm_instructions.h
  5. 5
    0
      test.c
  6. 34
    4
      yacc_util.c
  7. 12
    0
      yacc_util.h

+ 44
- 7
al.lex View File

@@ -2,6 +2,7 @@
2 2
 #include "asm_instructions.h"
3 3
 #include "symbol_table.h"
4 4
 #include "as.tab.h"
5
+#include "yacc_util.h"
5 6
 %}
6 7
 
7 8
 D           [0-9]
@@ -36,13 +37,49 @@ D           [0-9]
36 37
 ","			{ return tVIRG; }
37 38
 "{"			{ return tAO; }
38 39
 "}"			{ return tAF; }
39
-"!"		{ return tNOT; }
40
-"=="		{ return tEQ2; }
41
-"!="		{ return tNOTEQ; }
42
-"<"			{ return tINF; }
43
-"<="		{ return tINFEQ; }
44
-">"			{ return tSUP; }
45
-">="		{ return tSUPEQ; }
40
+"=="		{ 
41
+				struct Condition* c = malloc(sizeof(struct Condition));
42
+				c->instruction = EQU;
43
+				c->not = 0;
44
+                yylval.condition = c;
45
+                return tEQ2;
46
+			}
47
+"!="		{ 
48
+				struct Condition* c = malloc(sizeof(struct Condition));
49
+				c->instruction = EQU;
50
+				c->not = 1;
51
+                yylval.condition = c;
52
+                return tNOTEQ;
53
+            }
54
+"<"			{ 
55
+				struct Condition* c = malloc(sizeof(struct Condition));
56
+				c->instruction = INF;
57
+				c->not = 0;
58
+                yylval.condition = c;
59
+                return tINF;
60
+            }
61
+"<="		{ 
62
+				struct Condition* c = malloc(sizeof(struct Condition));
63
+				c->instruction = SUP;
64
+				c->not = 1;
65
+                yylval.condition = c;
66
+                return tINFEQ;
67
+            }
68
+">"			{ 
69
+				struct Condition* c = malloc(sizeof(struct Condition));
70
+				c->instruction = SUP;
71
+				c->not = 0;
72
+                yylval.condition = c;
73
+                return tSUP;
74
+            }
75
+">="		{ 
76
+				struct Condition* c = malloc(sizeof(struct Condition));
77
+				c->instruction = INF;
78
+				c->not = 1;
79
+                yylval.condition = c;
80
+                return tSUPEQ;
81
+            }
82
+"!"			{ return tNOT; }
46 83
 "&&"		{ return tAND; }
47 84
 "||"		{ return tOR; }
48 85
 [ \t\n]+	{ }

+ 26
- 23
as.y View File

@@ -16,7 +16,9 @@ InstructionTable instruction_table;
16 16
     char* symbol_name;
17 17
     SymbolItem* symbol;
18 18
 	int nombre;
19
+	int address;
19 20
 	Instruction instruction;
21
+	struct Condition* condition;
20 22
 }
21 23
 
22 24
 %token<nombre> tNB
@@ -31,13 +33,13 @@ InstructionTable instruction_table;
31 33
 %token tAO
32 34
 %token tAF
33 35
 %token tEQ
34
-%token tEQ2
35
-%token tNOTEQ
36
+%token<condition> tEQ2
37
+%token<condition> tNOTEQ
38
+%token<condition> tINF
39
+%token<condition> tINFEQ
40
+%token<condition> tSUP
41
+%token<condition> tSUPEQ
36 42
 %token tNOT
37
-%token tINF
38
-%token tINFEQ
39
-%token tSUP
40
-%token tSUPEQ
41 43
 %token tAND
42 44
 %token tOR
43 45
 
@@ -56,6 +58,8 @@ InstructionTable instruction_table;
56 58
 %left tMUL
57 59
 %left tDIV
58 60
 %type<symbol> E
61
+%type<symbol> Cond
62
+%type<address> IfSimple
59 63
 
60 64
 %%
61 65
 
@@ -74,7 +78,7 @@ Body : tAO Instructions tAF  { printf("Body\n"); } ;
74 78
 
75 79
 Instructions : Instruction Instructions |  { printf("Instructions\n"); } ;
76 80
 
77
-Instruction : Aff | Printf { printf("Instruction\n"); } ;
81
+Instruction : Aff | Printf | If { printf("Instruction\n"); } ;
78 82
 
79 83
 Aff : tID tEQ E tPV { write_affectation(&symbol_table, &instruction_table, $1, $3); } ;
80 84
 
@@ -97,29 +101,28 @@ DeclarationConst : tCONST { current_type = TYPE_CONST; }  DeclarationBody tPV ;
97 101
 
98 102
 DeclarationBody : VariableIdentifier tVIRG DeclarationBody | VariableIdentifier ;
99 103
 
100
-VariableIdentifier: tID { add_symbol(&symbol_table, current_type, $1); }
104
+VariableIdentifier: tID { add_symbol(&symbol_table, current_type, $1); } ;
101 105
     
102 106
 // While : tWHILE tPO Cond tPF Body { printf("While\n"); } ;
103
-// 
104
-// If : IfSimple | IfElse { printf("If\n"); } ;
105
-// 
106
-// IfSimple : tIF tPO Cond tPF Body { printf("IfSimple\n"); } ;
107
-// 
108
-// IfElse : IfSimple tELSE Body { printf("IfElse\n"); } ;
109
-// 
110
-// Cond : tNOT Cond | Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT tPO Cond tPF { printf("Cond\n"); } ;
111 107
 
112
-Printf : tPRINTF tPO E tPF tPV  { write_print(&instruction_table, $3); } ;
108
+If : IfSimple { update_jmf(&instruction_table, $1); } ;
113 109
 
114
-// If : tIF tPO Cond tPF Body ;
115
-// 
116
-// Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | tNOT Cond ;
110
+IfSimple : tIF tPO Cond tPF { $<address>$ = write_jmf(&instruction_table, $3); } Body { $$ = $<address>5; };
117 111
 
118
-// Invocation : tID tPO  Args  tPF ;
112
+// IfElse : IfSimple tELSE Body { printf("IfElse\n"); } ;
119 113
 
120
-// Args : .... cf params
114
+Cond : tNOT Cond { $$ = write_not_condition(&symbol_table, &instruction_table, $2); }
115
+| Cond tAND Cond { $$ = write_op(&symbol_table, &instruction_table, MUL, $1, $3); }
116
+| Cond tOR Cond { $$ = write_op(&symbol_table, &instruction_table, ADD, $1, $3); }
117
+| E tEQ2 E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
118
+| E tINF E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
119
+| E tINFEQ E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
120
+| E tSUP E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
121
+| E tSUPEQ E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
122
+| E tNOTEQ E { $$ = write_condition(&symbol_table, &instruction_table, $2, $1, $3); }
123
+| tNOT tPO Cond tPF { $$ = write_not_condition(&symbol_table, &instruction_table, $3); } ;
121 124
 
122
-// Return : tRET E tPV ;
125
+Printf : tPRINTF tPO E tPF tPV  { write_print(&instruction_table, $3); } ;
123 126
 
124 127
 %%
125 128
 

+ 4
- 0
asm_instructions.c View File

@@ -48,4 +48,8 @@ void write_instruction(InstructionItem *item, FILE *file) {
48 48
     } else {
49 49
         fprintf(file, "%s %d %d %d\n", instructions_labels[item->instruction], item->arg1, item->arg2, item->arg3);
50 50
     }
51
+}
52
+
53
+int get_current_address(struct InstructionTable *table) {
54
+    return table->index;
51 55
 }

+ 3
- 0
asm_instructions.h View File

@@ -2,6 +2,7 @@
2 2
 #define COMPILATOR_2000_ASM_INSTRUCTIONS_H
3 3
 
4 4
 #include <stdio.h>
5
+#include "symbol_table.h"
5 6
 
6 7
 #define INSTRUCTION_TABLE_SIZE 100
7 8
 
@@ -43,4 +44,6 @@ void write_instruction_table(InstructionTable *table, FILE *file);
43 44
 
44 45
 void write_instruction(InstructionItem *item, FILE *file);
45 46
 
47
+int get_current_address(InstructionTable *table);
48
+
46 49
 #endif //COMPILATOR_2000_ASM_INSTRUCTIONS_H

+ 5
- 0
test.c View File

@@ -4,4 +4,9 @@ int main(){
4 4
     x = 2;
5 5
     printf(x);
6 6
     y = 3 + 3 + x;
7
+    if (x == 2) {
8
+        printf(x);
9
+        printf(y);
10
+    }
11
+    x = 3;
7 12
 }

+ 34
- 4
yacc_util.c View File

@@ -15,13 +15,11 @@ const SymbolItem* write_op(SymbolTable* symbol_table, InstructionTable *instruct
15 15
 const SymbolItem* write_negation(SymbolTable* symbol_table, InstructionTable *instruction_table, SymbolItem *op2) {
16 16
 //    Create temp variable with 0
17 17
     const SymbolItem* op1 = init_temp_symbol(symbol_table, instruction_table, 0);
18
-    const SymbolItem* dest = add_temp_symbol(symbol_table, TYPE_INT);
19 18
 //    Make the 0 - variable operation
20
-    add_instruction(instruction_table, SOU, dest->address, op1->address, op2->address);
21
-    return dest;
19
+    add_instruction(instruction_table, SOU, op1->address, op1->address, op2->address);
20
+    return op1;
22 21
 }
23 22
 
24
-
25 23
 const SymbolItem* init_temp_symbol(SymbolTable* symbol_table, InstructionTable *instruction_table, int constant) {
26 24
     const SymbolItem* dest = add_temp_symbol(symbol_table, TYPE_INT);
27 25
     add_instruction(instruction_table, AFC, dest->address, constant, 0);
@@ -32,4 +30,36 @@ void write_print(InstructionTable *instruction_table, SymbolItem *src) {
32 30
     add_instruction(instruction_table, PRI, src->address, 0, 0);
33 31
 }
34 32
 
33
+int update_jmf(InstructionTable *table, int address) {
34
+    if (address > table->index) {
35
+        return -1;
36
+    }
37
+    table->table[address].arg2 = get_current_address(table);
38
+    return 0;
39
+}
40
+
41
+int write_jmf(InstructionTable *table, SymbolItem* cond) {
42
+    int addr = get_current_address(table);
43
+    add_instruction(table, JMF, cond->address, 0, 0);
44
+    return addr;
45
+}
46
+
47
+const SymbolItem* write_not_condition(SymbolTable* symbol_table, InstructionTable *instruction_table, const SymbolItem* cond) {
48
+//    Create temp variable with 0
49
+    const SymbolItem* op1 = init_temp_symbol(symbol_table, instruction_table, 0);
50
+//    Check if the condition is equal to 0 (makes the negation)
51
+    add_instruction(instruction_table, EQU, op1->address, op1->address, cond->address);
52
+    return op1;
53
+}
54
+
55
+const SymbolItem* write_condition(SymbolTable* symbol_table, InstructionTable *instruction_table, Condition* op, SymbolItem* cond1, SymbolItem* cond2) {
56
+    const SymbolItem *dest = write_op(symbol_table, instruction_table, op->instruction, cond1, cond2);
57
+    if (op->not) {
58
+        return write_not_condition(symbol_table, instruction_table, dest);
59
+    }
60
+    return dest;
61
+}
62
+
63
+
64
+
35 65
 

+ 12
- 0
yacc_util.h View File

@@ -4,6 +4,10 @@
4 4
 #ifndef COMPILATOR_2000_YACC_UTIL_H
5 5
 #define COMPILATOR_2000_YACC_UTIL_H
6 6
 
7
+typedef struct Condition {
8
+    Instruction instruction;
9
+    int not;
10
+} Condition;
7 11
 
8 12
 void write_affectation(SymbolTable* symbolTable, InstructionTable *instructionTable, char* symbol_dest, SymbolItem* src);
9 13
 
@@ -15,4 +19,12 @@ const SymbolItem* init_temp_symbol(SymbolTable* symbol_table, InstructionTable *
15 19
 
16 20
 void write_print(InstructionTable *instruction_table, SymbolItem *src);
17 21
 
22
+int update_jmf(InstructionTable *table, int address);
23
+
24
+int write_jmf(InstructionTable *table, SymbolItem* cond);
25
+
26
+const SymbolItem* write_not_condition(SymbolTable* symbol_table, InstructionTable *table, const SymbolItem* cond);
27
+
28
+const SymbolItem* write_condition(SymbolTable* symbol_table, InstructionTable *table, Condition* op, SymbolItem* cond1, SymbolItem* cond2);
29
+
18 30
 #endif //COMPILATOR_2000_YACC_UTIL_H

Loading…
Cancel
Save