Browse Source

Cross compilo termine

Paul Faure 2 years ago
commit
ca436dc7a1
7 changed files with 405 additions and 0 deletions
  1. 7
    0
      Makefile
  2. 0
    0
      ReadMe
  3. 51
    0
      al.lex
  4. 122
    0
      as.y
  5. 186
    0
      tables.c
  6. 31
    0
      tables.h
  7. 8
    0
      toto.asm

+ 7
- 0
Makefile View File

@@ -0,0 +1,7 @@
1
+build :
2
+	gcc -Wall -c tables.c -o tables.o
3
+	bison -d -t as.y
4
+	flex -o lex.yy.c al.lex
5
+	gcc -Wall -c as.tab.c -o as.tab.o
6
+	gcc -Wall -c lex.yy.c -o lex.yy.o
7
+	gcc as.tab.o lex.yy.o tables.o -ll -o rondoudou_cross_assembleur

+ 0
- 0
ReadMe View File


+ 51
- 0
al.lex View File

@@ -0,0 +1,51 @@
1
+%{
2
+#include "as.tab.h"
3
+int yywrap(void){return 1;}
4
+void
5
+yyerror (char const *s)
6
+{
7
+
8
+  fprintf (stderr, "%s\n", s);
9
+}
10
+
11
+%}
12
+
13
+%%
14
+
15
+"ADD"     { return tADD ;} 
16
+"SUB"     { return tSUB;}
17
+"MUL"     { return tMUL; }
18
+"DIV"			{ return tDIV; }
19
+"INF"			{ return tINF; }
20
+"SUP"     { return tSUP; }
21
+"EQU"     { return tEQU; }
22
+
23
+"AFC"    	{ return tAFC; }
24
+"COP"     { return tCPY; }
25
+"AFCA"    { return tAFCA; }
26
+"COPA"    { return tCPYA; }
27
+
28
+
29
+"READ"    { return tREAD; }
30
+"WR"    { return tWR; }
31
+
32
+"JMP"     { return tJMP; }
33
+"JMF"     { return tJMF; }
34
+
35
+"GET"     { return tGET; }
36
+"PRI"     { return tPRI; }
37
+
38
+"CALL"    { return tCALL; }
39
+"RET"     { return tRET; }
40
+
41
+"STOP"     { return tSTOP; }
42
+
43
+[0-9]+	  { yylval.nombre = atoi(yytext); return tNB; }
44
+
45
+"\n"
46
+" "
47
+"\t"
48
+
49
+%%
50
+
51
+

+ 122
- 0
as.y View File

@@ -0,0 +1,122 @@
1
+%union {
2
+	int nombre;
3
+}
4
+%{
5
+#include "tables.h"
6
+#include <stdio.h> 
7
+
8
+FILE * file;
9
+
10
+%}
11
+
12
+%token tMUL tDIV tADD tSUB tINF tSUP tEQU
13
+%token tAFC tCPY tAFCA tCPYA
14
+%token tREAD tWR
15
+%token tJMP tJMF
16
+%token tGET tPRI
17
+%token tCALL tRET
18
+%token tSTOP
19
+%token<nombre> tNB
20
+
21
+%%
22
+
23
+Programme : Instruction Programme; 
24
+Programme : Instruction;
25
+
26
+Instruction : tMUL tNB tNB tNB {increment_time();
27
+																int reg_dest = get_reg_write($2, file);
28
+																int reg_src1 = get_reg_read($3, file);
29
+																int reg_src2 = get_reg_read($4, file);
30
+																fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2);};
31
+Instruction : tADD tNB tNB tNB {increment_time();
32
+																int reg_dest = get_reg_write($2, file);
33
+																int reg_src1 = get_reg_read($3, file);
34
+																int reg_src2 = get_reg_read($4, file);
35
+																fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2);};
36
+Instruction : tDIV tNB tNB tNB {increment_time();
37
+																int reg_dest = get_reg_write($2, file);
38
+																int reg_src1 = get_reg_read($3, file);
39
+																int reg_src2 = get_reg_read($4, file);
40
+																fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2);};
41
+Instruction : tSUB tNB tNB tNB {increment_time();
42
+																int reg_dest = get_reg_write($2, file);
43
+																int reg_src1 = get_reg_read($3, file);
44
+																int reg_src2 = get_reg_read($4, file);
45
+																fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2);};
46
+Instruction : tINF tNB tNB tNB {increment_time();
47
+																int reg_dest = get_reg_write($2, file);
48
+																int reg_src1 = get_reg_read($3, file);
49
+																int reg_src2 = get_reg_read($4, file);
50
+																fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2);};
51
+Instruction : tSUP tNB tNB tNB {increment_time();
52
+																int reg_dest = get_reg_write($2, file);
53
+																int reg_src1 = get_reg_read($3, file);
54
+																int reg_src2 = get_reg_read($4, file);
55
+																fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2);};
56
+Instruction : tEQU tNB tNB tNB {increment_time();
57
+																int reg_dest = get_reg_write($2, file);
58
+																int reg_src1 = get_reg_read($3, file);
59
+																int reg_src2 = get_reg_read($4, file);
60
+																fprintf(file, "EQU %d %d %d\n", reg_dest, reg_src1, reg_src2);};
61
+
62
+
63
+Instruction : tAFC tNB tNB     {increment_time();
64
+																int reg_dest = get_reg_write($2, file);
65
+															  fprintf(file, "AFC %d %d\n", reg_dest, $3);};
66
+Instruction : tCPY tNB tNB     {increment_time();
67
+																int reg_dest = get_reg_write($2, file);
68
+																int reg_src = get_reg_read($3, file);
69
+																fprintf(file, "CPY %d %d\n", reg_dest, reg_src);};
70
+Instruction : tAFCA tNB tNB     {increment_time();
71
+																int reg_dest = get_reg_write($2, file);
72
+															  fprintf(file, "AFCA %d %d\n", reg_dest, $3);};
73
+Instruction : tCPYA tNB tNB     {increment_time();
74
+																int reg_dest = get_reg_write($2, file);
75
+																int reg_src = get_reg_read($3, file);
76
+																fprintf(file, "CPYA %d %d\n", reg_dest, reg_src);};
77
+
78
+
79
+Instruction : tJMP tNB         {increment_time();
80
+																fprintf(file, "JMP %d\n", $2);};
81
+Instruction : tJMF tNB tNB     {increment_time();
82
+																int reg_src = get_reg_read($2, file);
83
+																int reg_aux = get_reg_write(-1, file);
84
+																fprintf(file, "SUB %d %d %d\n", reg_aux, reg_aux, reg_src);
85
+																fprintf(file, "JMZ %d\n", $3);};
86
+
87
+Instruction : tWR tNB tNB      {increment_time();
88
+																int reg_dest = get_reg_write($2, file);
89
+																int reg_addr = get_reg_read($3, file);
90
+																fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr);};
91
+Instruction : tREAD tNB tNB    {increment_time();
92
+																int reg_addr = get_reg_read($2, file);
93
+																int reg_value = get_reg_read($3, file);
94
+																fprintf(file, "STOREA %d %d\n", reg_addr, reg_value);};
95
+
96
+
97
+Instruction : tGET tNB         {increment_time();
98
+																int reg_dest = get_reg_write($2, file);
99
+																fprintf(file, "GET %d\n", reg_dest);};
100
+Instruction : tPRI tNB         {increment_time();
101
+																int reg_src = get_reg_read($2, file);
102
+																fprintf(file, "PRI %d\n", reg_src);};
103
+
104
+
105
+Instruction : tCALL tNB tNB    {increment_time();
106
+																flush_and_init(file);
107
+																fprintf(file, "CALL %d %d\n", $2, $3);};
108
+Instruction : tRET             {increment_time();
109
+																flush_and_init(file);
110
+																fprintf(file, "RET\n");};
111
+
112
+Instruction : tSTOP             {increment_time();
113
+																fprintf(file, "STOP\n");};
114
+
115
+%%
116
+
117
+int main(void) {
118
+	file = stdout;
119
+    init();
120
+	yyparse();
121
+	return 0;
122
+}

+ 186
- 0
tables.c View File

@@ -0,0 +1,186 @@
1
+
2
+/* 
3
+----------------------------------------
4
+|  Adresse   |  Registre  |  Modifié   | 
5
+----------------------------------------
6
+|            |            |            |            
7
+|            |            |            |            
8
+|            |            |            |            
9
+|      i     | 0x777756b8 |     int    |       
10
+|    size    | 0x777756b8 |     int    |        
11
+----------------------------------------
12
+*/
13
+
14
+#include "tables.h"
15
+#define NB_REG 4
16
+#define MEM_SIZE 1024
17
+
18
+struct case_adresse tableau[MEM_SIZE];
19
+int registres[NB_REG];
20
+
21
+void init (void) {
22
+  int i;
23
+	struct case_adresse case_courante = {0, -1, 0};
24
+	for (i=0; i<MEM_SIZE; i++) {
25
+		case_courante.adresse = i;
26
+		tableau[i] = case_courante;
27
+	}
28
+	for (i=0; i<NB_REG; i++) {
29
+		registres[i] = 0;
30
+	}
31
+}
32
+
33
+void print_case_adresse(struct case_adresse case_courante) {
34
+	printf("{addr : %d ; reg : %d ; modi : %d}\n", case_courante.adresse, case_courante.registre, (int)case_courante.modifie);
35
+}
36
+
37
+void print() {
38
+  int i;
39
+	for (i=0; i<MEM_SIZE; i++) {
40
+		print_case_adresse(tableau[i]);
41
+	}
42
+}
43
+
44
+
45
+struct case_adresse get_info(int adresse) {
46
+	return tableau[adresse];
47
+}
48
+
49
+int get_adresse (int registre) {
50
+	int i = 0;
51
+	while (i < MEM_SIZE && tableau[i].registre != registre) {
52
+		i++;
53
+	}
54
+	if (i == MEM_SIZE) {
55
+		return -1;
56
+	} else {
57
+		return tableau[i].adresse;
58
+	}
59
+}
60
+
61
+void set_registre(int adresse, int registre) {
62
+	tableau[adresse].registre = registre;
63
+}
64
+
65
+void set_modifie(int adresse, char modifie) {
66
+	tableau[adresse].modifie = modifie;
67
+}
68
+
69
+void increment_time() {
70
+	int i;
71
+	for (i=0; i<NB_REG; i++) {
72
+		registres[i]++;
73
+	}
74
+}
75
+
76
+void refresh_registre(int registre) {
77
+	registres[registre] = 0;
78
+}
79
+
80
+int get_register() {
81
+	int i;
82
+	int index_max = 0;
83
+	for (i=0; i<NB_REG; i++) {
84
+		if (registres[index_max] < registres[i]) {
85
+			index_max = i;
86
+		}
87
+	}
88
+	return index_max;
89
+}
90
+
91
+int get_reg_write(int adresse, FILE * file) {
92
+	if (adresse == -1) {
93
+		int dispo = get_register();
94
+		int previous_addr = get_adresse(dispo);
95
+		if (previous_addr != -1) {
96
+			struct case_adresse ancienne_case = get_info(previous_addr);
97
+			if (ancienne_case.modifie == 1) {
98
+				fprintf(file, "STORE %d %d\n", previous_addr, dispo);
99
+				set_modifie(previous_addr, 0);
100
+			}
101
+			set_registre(previous_addr, -1);
102
+		}
103
+		return dispo;
104
+	} else {
105
+		set_modifie(adresse, 1);
106
+		struct case_adresse ma_case = get_info(adresse);
107
+		if (ma_case.registre == -1) {
108
+			int dispo = get_register();
109
+			int previous_addr = get_adresse(dispo);
110
+			if (previous_addr != -1) {
111
+				struct case_adresse ancienne_case = get_info(previous_addr);
112
+				if (ancienne_case.modifie == 1) {
113
+					fprintf(file, "STORE %d %d\n", previous_addr, dispo);
114
+					set_modifie(previous_addr, 0);
115
+				}
116
+				set_registre(previous_addr, -1);
117
+			}
118
+			set_registre(adresse, dispo);
119
+			refresh_registre(dispo);
120
+			return dispo;
121
+		} else {
122
+			refresh_registre(ma_case.registre);
123
+			return ma_case.registre;
124
+		}
125
+	}
126
+}
127
+
128
+int get_reg_read(int adresse, FILE * file) {
129
+	struct case_adresse ma_case = get_info(adresse);
130
+	if (ma_case.registre == -1) {
131
+		int dispo = get_register();
132
+		int previous_addr = get_adresse(dispo);
133
+		if (previous_addr != -1) {
134
+			struct case_adresse ancienne_case = get_info(previous_addr);
135
+			if (ancienne_case.modifie == 1) {
136
+				fprintf(file, "STORE %d %d\n", previous_addr, dispo);
137
+				set_modifie(previous_addr, 0);
138
+			}
139
+			set_registre(previous_addr, -1);
140
+		}
141
+		fprintf(file, "LOAD %d %d\n", dispo, adresse);
142
+		set_registre(adresse, dispo);
143
+		refresh_registre(dispo);
144
+		return dispo;
145
+	} else {
146
+		refresh_registre(ma_case.registre);
147
+		return ma_case.registre;
148
+	}
149
+}
150
+
151
+
152
+void flush_and_init(FILE * file) {
153
+	int i;
154
+	for (i = 0; i<MEM_SIZE; i++) {
155
+		if (tableau[i].registre != -1) {
156
+			if (tableau[i].modifie == 0) {
157
+				tableau[i].registre = -1;
158
+			} else {
159
+				fprintf(file, "STORE %d %d\n", i, tableau[i].registre);
160
+				tableau[i].registre = -1;
161
+				tableau[i].modifie = 0;
162
+			}
163
+		}
164
+	}
165
+	for (i=0; i<NB_REG; i++) {
166
+		registres[i] = 0;
167
+	}
168
+}
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+

+ 31
- 0
tables.h View File

@@ -0,0 +1,31 @@
1
+#ifndef TABLE_H
2
+#define TABLE_H
3
+
4
+/* 
5
+----------------------------------------
6
+|  Adresse   |  Registre  |  Modifié   | 
7
+----------------------------------------
8
+|            |            |            |            
9
+|            |            |            |            
10
+|            |            |            |            
11
+|      i     | 0x777756b8 |     int    |       
12
+|    size    | 0x777756b8 |     int    |        
13
+----------------------------------------
14
+*/
15
+
16
+#include <stdint.h>
17
+#include <stdio.h>
18
+
19
+struct case_adresse {
20
+	int adresse;
21
+	int registre;
22
+	char modifie;
23
+};
24
+
25
+void init(void);
26
+void increment_time();
27
+int get_reg_read(int adresse, FILE * file);
28
+int get_reg_write(int adresse, FILE * file);
29
+void flush_and_init(FILE * file);
30
+
31
+#endif

+ 8
- 0
toto.asm View File

@@ -0,0 +1,8 @@
1
+AFC 0 1
2
+AFC 1 1
3
+AFC 2 1
4
+AFC 3 1
5
+AFC 4 1
6
+AFC 5 1
7
+AFC 6 1
8
+MUL 0 1 2 

Loading…
Cancel
Save