summaryrefslogtreecommitdiff
path: root/assembly.lisp
blob: e4582de08b9d36eb545f438eb78465eefd37e999 (plain)
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
(in-package :cl-forth)

(defparameter *operations* (make-hash-table :test 'equal))

(eval-always
  (defun normalize-op-list (asm-list)
    (cons 'list
          (mapcar (lambda (el) (cond ((stringp el) el)
                                ((listp el) `(format nil ,@el))))
                  asm-list)))

  (defun defop-format (str space-num asm-list)
    (format str
            (format nil "~~{~a~~a~~%~~}" 
                    (make-string space-num :initial-element #\Space))
            asm-list))

  (defun replace-write (out-stream indent forms)
    (if (consp forms)
        (if (eq :write (car forms))
            `(defop-format ,out-stream ,indent
               ,(normalize-op-list (cdr forms)))
            (cons (replace-write out-stream indent (car forms))
                  (replace-write out-stream indent (cdr forms))))
        forms)))

(defmacro defop (op-name+args (&key (indent 4)) &body body)
  (with-gensyms (out-stream)
    (destructuring-bind (op-name . args) (mklist op-name+args)
      `(setf (gethash ,(string op-name) *operations*)
             (lambda (,out-stream ,@args)
               ,(if (or (stringp (car body)) (stringp (caar body)))
                    `(defop-format ,out-stream ,indent
                              ,(normalize-op-list body))
                    (replace-write out-stream indent (car body))))))))

;;; TODO: Turn stack operation comments to DEFOP option,
;;;       which then can be used by the user as a documentation
;;; TODO: Better yet, generate the asm code directly from
;;;       the stack op documentation (this seems easily doable)

;; ( -- a)
(defop (push a) ()
  ("push ~d" a))

;; (rbx rax -- (rbx + rax))
(defop + ()
  "pop rax"
  "pop rbx"
  "add rax, rbx"
  "push rax")

;; (rbx rax -- (rbx - rax))
(defop - ()
  "pop rax"
  "pop rbx"
  "sub rbx, rax"
  "push rbx")

(defop dump ()
  "pop rdi"
  "call dump")

(defop = ()
  "mov rcx, 0"
  "mov rdx, 1"
  "pop rax"
  "pop rbx"
  "cmp rax, rbx"
  "cmove rcx, rdx"
  "push rcx")

(defop (exit code) ()
  "mov rax, 60"
  ("mov rdi, ~a" code)
  "syscall")

(defop (ise label-num) ()
  "pop rax"
  "test rax, rax"
  ("jz et_~a" label-num))

(defop (yoksa yap-num ise-num) (:indent 0)
  ("    jmp et_~a" yap-num)
  ("et_~a:" ise-num))

(defop (yap label-num &optional döngü-num) (:indent 0)
  (if (null döngü-num)
      (:write ("et_~a:" label-num))
      (:write ("    jmp et_~a" döngü-num)
              ("et_~a:" label-num))))

;; (rax -- rax rax)
(defop eş ()
  "pop rax"
  "push rax"
  "push rax")

;; (rax -- )
(defop düş ()
  "pop rax")

(defop (iken label-num) ()
  "pop rax"
  "test rax, rax"
  ("jz et_~a" label-num))

(defop (döngü label-num) (:indent 0)
  ("et_~a:" label-num))

(defop < ()
  "mov rcx, 0"
  "mov rdx, 1"
  "pop rbx"
  "pop rax"
  "cmp rax, rbx"
  "cmovl rcx, rdx"
  "push rcx")

(defop > ()
  "mov rcx, 0"
  "mov rdx, 1"
  "pop rbx"
  "pop rax"
  "cmp rax, rbx"
  "cmovg rcx, rdx"
  "push rcx")

(defop bel ()
  "push bel")

(defop oku ()
  "pop rax"
  "xor rbx, rbx"
  "mov bl, [rax]"
  "push rbx")

(defop yaz ()
  "pop rbx"
  "pop rax"
  "mov [rax], bl")

(defop (syscall num) ()
  (iter (with call-regs = #("rdi" "rsi" "rdx" "r10" "r8" "r9"))
        (initially (:write "pop rax"))
        (for i from (- num 1) downto 0)
        (:write ("pop ~a" (aref call-regs i)))
        (finally (:write "syscall"))))

;;; (rbx rax -- rbx rax rbx)
(defop üst ()
  "pop rax"
  "pop rbx"
  "push rbx"
  "push rax"
  "push rbx")

;;; (rcx rbx rax -- rbx rax rcx)
(defop rot ()
  "pop rax"
  "pop rbx"
  "pop rcx"
  "push rbx"
  "push rax"
  "push rcx")

;;; (rbx rax -- rax rbx)
(defop değiş ()
  "pop rax"
  "pop rbx"
  "push rax"
  "push rbx")

;;; (rbx rcx -- (:shl rbx cl))
(defop << ()
  "pop rcx"
  "pop rbx"
  "shl rbx, cl"
  "push rbx")

;;; (rbx rcx -- (:shr rbx cl))
(defop >> ()
  "pop rcx"
  "pop rbx"
  "shr rbx, cl"
  "push rbx")

;;; (rbx rcx -- (:or rbx cl))
(defop "|" ()
  "pop rax"
  "pop rbx"
  "or rbx, rax"
  "push rbx")

;;; (rbx rcx -- (:and rbx cl))
(defop & ()
  "pop rax"
  "pop rbx"
  "and rbx, rax"
  "push rbx")

(defun gen-header (op str)
  (format str "    ;; -- ~s --~%" op))

(defun gen-code (op str)
  (let ((op-fn (gethash (string (car op)) *operations*)))
    (if (null op-fn)
        (error "~s is not a valid op" op)
        (apply op-fn str (cdr op)))))

(defun gen-dump (str)
  (format str "~{~a~%~}"
          '("dump:"
            "    mov     r9, -3689348814741910323"
            "    sub     rsp, 40"
            "    mov     BYTE [rsp+31], 10"
            "    lea     rcx, [rsp+30]"
            ".L2:"
            "    mov     rax, rdi"
            "    lea     r8, [rsp+32]"
            "    mul     r9"
            "    mov     rax, rdi"
            "    sub     r8, rcx"
            "    shr     rdx, 3"
            "    lea     rsi, [rdx+rdx*4]"
            "    add     rsi, rsi"
            "    sub     rax, rsi"
            "    add     eax, 48"
            "    mov     BYTE [rcx], al"
            "    mov     rax, rdi"
            "    mov     rdi, rdx"
            "    mov     rdx, rcx"
            "    sub     rcx, 1"
            "    cmp     rax, 9"
            "    ja      .L2"
            "    lea     rax, [rsp+32]"
            "    mov     edi, 1"
            "    sub     rdx, rax"
            "    xor     eax, eax"
            "    lea     rsi, [rsp+32+rdx]"
            "    mov     rdx, r8"
            "    mov     rax, 1"
            "    syscall"
            "    add     rsp, 40"
            "    ret")))