-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompilateur.lisp
More file actions
207 lines (183 loc) · 4.37 KB
/
Copy pathcompilateur.lisp
File metadata and controls
207 lines (183 loc) · 4.37 KB
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
; Compilateur lisp vers lisp VM
; Groupe Florian NOVELLON et Fabien FERAUD
(setf indexIf 0)
(setf indexComp 0)
(defun compil-fichier (nomFichier &optional dest)
(let ((fichier (open nomFichier)) (code '()) (bytecode '()))
(loop for expr = (read fichier nil) while expr do
(setf code (append code (list expr)))
)
(close fichier)
;(print (append code '((HALT))))
(setf bytecode (compil-list (append code '((HALT)))))
(affiche-code bytecode)
(if (not (null dest))
(with-open-file (str (string-concat "./" dest)
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format str (write-to-string bytecode)))
)
"Compilation termine !"
)
)
(defun compil-list (listLisp)
(if (null listLisp)
NIL
(append
(compil-expr (car listLisp))
(compil-list (cdr listLisp)))))
(defun affiche-code (code)
(affiche-code-ligne code 1))
(defun affiche-code-ligne (code ligne)
(if (null code)
NIL
(progn
(print (string-concat (write-to-string ligne) " : " (write-to-string (car code))))
(affiche-code-ligne (cdr code) (+ 1 ligne)))))
(defun compil-expr (expr &optional env)
(cond
((consp expr)
(case (car expr)
('if (compil-if expr env))
('defun (compil-defun expr env))
('halt `((HALT)))
('nop `((NOP)))
(t (compil-appel expr env))))
((constantp expr) (compil-constant expr))
((symbolp expr) (compil-var expr env))
(t (error "Expression ~s n'est pas évaluable" expr))))
(defun compil-constant (constant)
`((MOVE (LIT ,constant) R0)))
(defun compil-if (code &optional env)
(setf indexIf (+ indexIf 1))
(let ((sinon (intern (string-concat (string "SINON") (write-to-string indexIf))))
(finSi (intern (string-concat (string "FINSI") (write-to-string indexIf)))))
(append
(compil-expr (cadr code) env)
`((CMP (LIT 0) R0))
`((JEQ (LABEL ,sinon)))
(compil-expr (caddr code) env)
`((JMP (LABEL ,finSi)))
`((LABEL ,sinon))
(compil-expr (cadddr code) env)
`((LABEL ,finSi))
)
)
)
(defun compil-var (var &optional env)
(let ((lib (assoc var env)))
(if lib
(append
`((MOVE FP R0))
`((SUB (LIT ,(cdr lib)) R0))
`((LOAD R0 R0))
)
`((MOVE (VAR ,var) RO)) ; pour la gestion des let
)
)
)
(defun compil-defun (code &optional env)
(let ((positionPile 0))
(progn
(map
'list
(lambda (param)
(progn
(setf positionPile (+ positionPile 1))
(setf env (acons param positionPile env))
)
)
(caddr code)
)
(append
`((JMP (LABEL ,(intern (string-concat "END" (string (cadr code)))))))
`((LABEL ,(cadr code)))
(compil-expr (cadddr code) env)
`((MOVE FP R1))
`((ADD (LIT 4) R1))
'((MOVE R1 SP))
`((RTN))
`((LABEL ,(intern (string-concat "END" (string (cadr code))))))
)
)
)
)
(defun compil-appel (code &optional env)
(append
(apply
'append
(map
'list
(lambda (param)
(append
(compil-expr param env)
`((PUSH R0))
)
)
(reverse (cdr code))
)
)
`((MOVE FP R1))
`((MOVE SP FP))
`((PUSH (LIT ,(list-length (cdr code)))))
`((MOVE SP R2))
`((SUB (LIT ,(+ (list-length (cdr code)) 1)) R2))
`((PUSH R2))
`((PUSH R1))
(compil-appel-primitif (car code))
`((POP R1))
`((POP R2))
`((MOVE R1 FP))
`((MOVE R2 SP))
)
)
(defun compil-appel-primitif (nomFonction)
(cond
((member nomFonction '(+ - * /))
(append
'(
(MOVE FP R0)
(SUB (LIT 1) R0)
(LOAD R0 R0)
(MOVE FP R1)
(SUB (LIT 2) R1)
(LOAD R1 R1)
)
(case nomFonction
('+ '((ADD R1 R0)))
('- '((SUB R1 R0)))
('* '((MULT R1 R0)))
('/ '((DIV R1 R0)))
)
)
)
((member nomFonction '(= <= < > >=))
(setf indexComp (+ indexComp 1))
(let ((finCond (intern (string-concat (string "FINCOMP") (write-to-string indexComp)))))
(append
'(
(MOVE FP R0)
(SUB (LIT 1) R0)
(LOAD R0 R0)
(MOVE FP R1)
(SUB (LIT 2) R1)
(LOAD R1 R1)
(CMP R0 R1)
(MOVE (LIT 1) R0)
)
(case nomFonction
('= `((JEQ (LABEL ,finCond))))
('<= `((JPE (LABEL ,finCond))))
('< `((JPG (LABEL ,finCond))))
('> `((JPP (LABEL ,finCond))))
('>= `((JGE (LABEL ,finCond))))
)
'((MOVE (LIT 0) R0))
`((LABEL ,finCond))
)
)
)
(t `((JSR (LABEL ,nomFonction))))
)
)