Skip to content

Commit 315d4ec

Browse files
committed
Print memory for iniquity.
1 parent 9cce681 commit 315d4ec

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

langs/iniquity/compile-ops.rkt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@
2121
unpad-stack)]
2222
['peek-byte (seq pad-stack
2323
(Call 'peek_byte)
24-
unpad-stack)]))
24+
unpad-stack)]
25+
['dump-memory-stats
26+
(seq (Mov rdi rsp)
27+
(Mov 'rsi 'rbp)
28+
(Mov 'rdx rbx)
29+
pad-stack
30+
(Call 'print_memory)
31+
(Mov rax 0)
32+
unpad-stack)]))
2533

2634
;; Op1 -> Asm
2735
(define (compile-op1 p)

langs/iniquity/compile.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
(prog (externs)
1818
(Global 'entry)
1919
(Label 'entry)
20-
(Mov rbx rdi) ; recv heap pointer
20+
(Mov 'rbp 'rsp) ; save stack base pointer
21+
(Mov rbx rdi) ; recv heap pointer
2122
(compile-e e '())
2223
(Ret)
2324
(compile-defines ds)
@@ -29,7 +30,8 @@
2930
(seq (Extern 'peek_byte)
3031
(Extern 'read_byte)
3132
(Extern 'write_byte)
32-
(Extern 'raise_error)))
33+
(Extern 'raise_error)
34+
(Extern 'print_memory)))
3335

3436
;; [Listof Defn] -> Asm
3537
(define (compile-defines ds)

langs/iniquity/main.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <inttypes.h>
34
#include "values.h"
45
#include "print.h"
56
#include "runtime.h"
@@ -38,3 +39,39 @@ int main(int argc, char** argv)
3839
free(heap);
3940
return 0;
4041
}
42+
43+
const char* val_typeof_string(int64_t t) {
44+
switch (val_typeof(t)) {
45+
case T_INT: return "INT";
46+
case T_BOOL: return "BOOL";
47+
case T_CHAR: return "CHAR";
48+
case T_EOF: return "EOF";
49+
case T_VOID: return "VOID";
50+
case T_EMPTY: return "EMPTY";
51+
case T_BOX: return "BOX";
52+
case T_CONS: return "CONS";
53+
case T_VECT: return "VECT";
54+
case T_STR: return "STR";
55+
default: return "UNKNOWN";
56+
}
57+
}
58+
59+
60+
61+
void print_memory(int64_t* rsp, int64_t rbp, int64_t rbx) {
62+
int stack_count = (rbp-(int64_t)rsp) / 8;
63+
int heap_count = (rbx-(int64_t)heap) / 8;
64+
65+
printf("----------------------------------------------------------------\n");
66+
// printf("rsp: %" PRIx64 ", rbp: %" PRIx64 ", stack count: %d, heap count: %d\n", (int64_t)rsp, rbp, stack_count, heap_count);
67+
int i;
68+
69+
printf("STACK:\n");
70+
for (i = 0; i < stack_count; i++) {
71+
printf("[%" PRIx64 "] = %016" PRIx64 ", %s\n", (int64_t)rsp + i, rsp[i], val_typeof_string(rsp[i]));
72+
}
73+
printf("HEAP:\n");
74+
for (i = 0; i < heap_count; i++) {
75+
printf("[%" PRIx64 "] = %016" PRIx64 ", %s\n", (int64_t)heap + i, heap[i], val_typeof_string(heap[i]));
76+
}
77+
}

langs/iniquity/parse.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
[_ (error "Parse error" s)]))
4848

4949
(define op0
50-
'(read-byte peek-byte void))
50+
'(read-byte peek-byte void dump-memory-stats))
5151

5252
(define op1
5353
'(add1 sub1 zero? char? write-byte eof-object?

0 commit comments

Comments
 (0)