forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.asm
544 lines (420 loc) · 13.2 KB
/
printer.asm
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
;;; Turns forms (lists, values/atoms) into strings
;;;
;;;
%include "macros.mac"
section .data
; Constant strings for printing
static unknown_type_string, db "#<unknown>"
static unknown_value_string, db "#<unknown value>"
static function_type_string, db "#<function>"
static macro_type_string, db "#<macro>"
static nil_value_string, db "nil"
static true_value_string, db "true"
static false_value_string, db "false"
section .text
;; Input: Address of object in RSI
;; print_readably in RDI. First bit set to zero for false
;;
;; Output: Address of string in RAX
;;
;; Modifies:
;; RCX
;; R8
;; R12
;; R13
;; R14
;; Calls: raw_to_string,
;;
;;
pr_str:
; Get the type
mov cl, BYTE [rsi]
; Check if it's already a string
cmp cl, maltype_string
jne .not_string
; ---------------------------
; Handle string
test rdi, 1
jz .string_not_readable
; printing readably, so escape characters
call string_new ; Output string in rax
mov r12, rax
add r12, Array.data ; Output data
mov r13, rsi
add r13, Array.data ; Input data
mov r14d, DWORD [rsi + Array.length]
add r14, Array.data
add r14, rsi ; End of input data
; Put " at start of output string
mov [r12], BYTE '"'
inc r12
; Loop through the input string, escaping characters
.string_loop:
cmp r13, r14
je .string_finished
mov cl, BYTE [r13] ; Get next character
inc r13
cmp cl, '"' ;
je .string_escape_char
cmp cl, 92 ; Escape '\'
je .string_escape_char
cmp cl, 10 ; Newline
je .string_newline
; No special case, just copy the byte
mov [r12], BYTE cl
inc r12
jmp .string_loop
.string_newline:
mov cl, 'n'
;jmp .string_escape_char
.string_escape_char: ; Add a '\' before char in cl
mov [r12], BYTE 92 ; Escape '\'
inc r12
mov [r12], BYTE cl
inc r12
jmp .string_loop
.string_finished:
mov [r12], BYTE '"' ; At the end
inc r12
; Calculate length of string
sub r12, rax
sub r12, Array.data
mov [rax + Array.length], DWORD r12d
ret
.string_not_readable:
; Just return the string
call incref_object
mov rax, rsi
ret
; ----------------------------
.not_string:
; Now test the container type (value, list, map, vector)
mov ch, cl
and ch, container_mask
jz .value
cmp ch, container_list
je .list
cmp ch, container_symbol
je .symbol
cmp ch, container_map
je .map
cmp ch, container_vector
je .vector
cmp ch, container_function
je .function_or_macro
cmp ch, container_atom
je .atom
; Unknown
mov rsi, unknown_type_string
mov edx, unknown_type_string.len
call raw_to_string ; Puts a String in RAX
ret
; --------------------------------
.value:
mov ch, cl
and ch, content_mask
jz .value_nil
cmp ch, content_int
je .value_int
cmp ch, content_true
je .value_true
cmp ch, content_false
je .value_false
mov rsi, unknown_value_string
mov edx, unknown_value_string.len
call raw_to_string ; Puts a String in RAX
ret
; --------------------------------
.value_nil:
mov rsi, nil_value_string
mov edx, nil_value_string.len
call raw_to_string
ret
.value_true:
mov rsi, true_value_string
mov edx, true_value_string.len
call raw_to_string
ret
.value_false:
mov rsi, false_value_string
mov edx, false_value_string.len
call raw_to_string
ret
; --------------------------------
.value_int:
mov rax, [rsi + Cons.car]
call itostring
ret
; --------------------------------
.list:
mov r12, rsi ; Input list
call string_new ; String in rax
mov r13, rax ; Output string in r13
; Put '(' onto string
mov rsi, rax
mov cl, '('
call string_append_char
; loop through list
.list_loop:
; Extract values and print
mov rsi, r12
mov cl, BYTE [rsi] ; Get type
; Check if it's a pointer (address)
mov ch, cl
and ch, content_mask
cmp ch, content_pointer
je .list_loop_pointer
cmp ch, content_empty
je .list_check_end
; A value (nil, int etc. or function)
mov ch, cl ; Save type, container
and cl, content_mask ; Remove list type -> value
mov BYTE [rsi], cl
push rcx
push r13
push r12
call pr_str ; String in rax
pop r12
pop r13
pop rcx
mov cl, ch ; Restore list type
mov BYTE [r12], cl
jmp .list_loop_got_str
.list_loop_pointer:
mov rsi, [rsi + Cons.car] ; Address of object
push r13
push r12
call pr_str ; String in rax
pop r12
pop r13
.list_loop_got_str:
; concatenate strings in rax and rsi
mov rsi, r13 ; Output string
mov rdx, rax ; String to be copied
push rsi ; Save output string
push rax ; save temporary string
call string_append_string
; Release the string
pop rsi ; Was in rax, temporary string
call release_array
pop rsi ; restore output string
.list_check_end:
; Check if this is the end of the list
mov cl, BYTE [r12 + Cons.typecdr]
cmp cl, content_pointer
jne .list_finished
; More left in the list
; Add space between values
mov cl, ' '
mov rsi, r13
call string_append_char
; Get next Cons
mov r12, [r12 + Cons.cdr]
jmp .list_loop
.list_finished:
; put ')' at the end of the string
mov cl, ')'
mov rsi, r13
call string_append_char
mov rax, rsi
ret
; --------------------------------
.symbol:
; Make a copy of the string
call string_new ; in rax
mov ebx, DWORD [rsi + Array.length]
mov [rax + Array.length], ebx
mov rcx, rsi
add rcx, Array.data ; Start of input data
mov rdx, rsi
add rdx, Array.size ; End of input data
mov r12, rax
add r12, Array.data ; Start of output data
.symbol_copy_loop:
; Copy [rax] -> [r12]
mov rbx, [rcx]
mov [r12], rbx
add rcx, 8 ; Next 64 bits of input
cmp rcx, rdx
je .symbol_finished
add r12, 8 ; Next 64 bits of output
jmp .symbol_copy_loop
.symbol_finished:
ret
; --------------------------------
.map:
mov r12, rsi ; Input map
call string_new ; String in rax
mov r13, rax ; Output string in r13
; Put '{' onto string
mov rsi, rax
mov cl, '{'
call string_append_char
; loop through map
.map_loop:
; Extract values and print
mov rsi, r12
mov cl, BYTE [rsi] ; Get type
; Check if it's a pointer (address)
mov ch, cl
and ch, content_mask
cmp ch, content_pointer
je .map_loop_pointer
cmp ch, content_empty
je .map_check_end
; A value (nil, int etc. or function)
xchg ch, cl
mov [rsi], BYTE cl ; Remove map type -> value
xchg ch, cl
push rcx
push r13
push r12
call pr_str ; String in rax
pop r12
pop r13
pop rcx
mov cl, BYTE [r12] ; Restore map type
jmp .map_loop_got_str
.map_loop_pointer:
mov rsi, [rsi + Cons.car] ; Address of object
push r13
push r12
call pr_str ; String in rax
pop r12
pop r13
.map_loop_got_str:
; concatenate strings in rax and rsi
mov rsi, r13 ; Output string
mov rdx, rax ; String to be copied
push rsi ; Save output string
push rax ; save temporary string
call string_append_string
; Release the string
pop rsi ; Was in rax, temporary string
call release_array
pop rsi ; restore output string
.map_check_end:
; Check if this is the end of the map
mov cl, BYTE [r12 + Cons.typecdr]
cmp cl, content_nil
je .map_finished
; More left in the map
; Add space between values
mov cl, ' '
mov rsi, r13
call string_append_char
; Get next Cons
mov r12, [r12 + Cons.cdr]
jmp .map_loop
.map_finished:
; put '}' at the end of the string
mov cl, '}'
mov rsi, r13
call string_append_char
mov rax, rsi
ret
; --------------------------------
.vector:
mov r12, rsi ; Input vector
call string_new ; String in rax
mov r13, rax ; Output string in r13
; Put '[' onto string
mov rsi, rax
mov cl, '['
call string_append_char
; loop through vector
.vector_loop:
; Extract values and print
mov rsi, r12
mov cl, BYTE [rsi] ; Get type
; Check if it's a pointer (address)
mov ch, cl
and ch, content_mask
cmp ch, content_pointer
je .vector_loop_pointer
cmp ch, content_empty
je .vector_check_end
; A value (nil, int etc. or function)
mov ch, cl ; Save type, container
and cl, content_mask ; Remove vector type -> value
mov BYTE [rsi], cl
push rcx
push r13
push r12
call pr_str ; String in rax
pop r12
pop r13
pop rcx
mov cl, ch ; Restore vector type
mov BYTE [r12], cl
jmp .vector_loop_got_str
.vector_loop_pointer:
mov rsi, [rsi + Cons.car] ; Address of object
push r13
push r12
call pr_str ; String in rax
pop r12
pop r13
.vector_loop_got_str:
; concatenate strings in rax and rsi
mov rsi, r13 ; Output string
mov rdx, rax ; String to be copied
push rsi ; Save output string
push rax ; save temporary string
call string_append_string
; Release the string
pop rsi ; Was in rax, temporary string
call release_array
pop rsi ; restore output string
.vector_check_end:
; Check if this is the end of the vector
mov cl, BYTE [r12 + Cons.typecdr]
cmp cl, content_pointer
jne .vector_finished
; More left in the vector
; Add space between values
mov cl, ' '
mov rsi, r13
call string_append_char
; Get next Cons
mov r12, [r12 + Cons.cdr]
jmp .vector_loop
.vector_finished:
; put ']' at the end of the string
mov cl, ']'
mov rsi, r13
call string_append_char
mov rax, rsi
ret
; --------------------------------
.function_or_macro:
cmp cl, maltype_macro
je .macro
; a function
mov rsi, function_type_string
mov edx, function_type_string.len
call raw_to_string ; Puts a String in RAX
ret
.macro:
mov rsi, macro_type_string
mov edx, macro_type_string.len
call raw_to_string ; Puts a String in RAX
ret
; --------------------------------
.atom:
mov rsi, [rsi + Cons.car] ; What the atom points to
call string_new ; String in rax
; Start string with '(atom'
mov rbx, '(atom '
mov [rax + Array.data], rbx
mov [rax + Array.length], DWORD 6
push rax
call pr_str
mov rdx, rax ; string to be copied
pop rsi ; Output string
call string_append_string
; closing bracket
mov cl, ')'
call string_append_char
mov rax, rsi
ret