-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackframe.c
92 lines (81 loc) · 3.11 KB
/
stackframe.c
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
#include "compiler.h"
#include "vector.h"
#include <assert.h>
static struct compile_process *cp;
void set_compile_process_for_stack_frame(
struct compile_process *compile_process) {
cp = compile_process;
}
void stackframe_pop(struct node *func_node) {
struct stack_frame *frame = &func_node->func.frame;
vector_pop(frame->elements);
}
struct stack_frame_element *stackframe_back(struct node *func_node) {
return vector_back_or_null(func_node->func.frame.elements);
}
struct stack_frame_element *stackframe_back_expect(struct node *func_node,
int expecting_type,
const char *expecting_name) {
struct stack_frame_element *element = stackframe_back(func_node);
if (element && element->type != expecting_type ||
!S_EQ(element->name, expecting_name)) {
return NULL;
}
return element;
}
void stackframe_pop_expecting(struct node *func_node, int expecting_type,
const char *expecting_name) {
struct stack_frame *frame = &func_node->func.frame;
struct stack_frame_element *last_element = stackframe_back(func_node);
if (!last_element) {
compiler_error(cp, "stackframe error: No last element on the stack \n");
}
if (last_element->type != expecting_type &&
!(S_EQ(last_element->name, expecting_name))) {
compiler_error(cp, "stackframe error: expecting wrong element \n");
}
stackframe_pop(func_node);
}
void stackframe_peek_start(struct node *func_node) {
struct stack_frame *frame = &func_node->func.frame;
vector_set_peek_pointer_end(frame->elements);
vector_set_flag(frame->elements, VECTOR_FLAG_PEEK_DECREMENT);
}
struct stack_frame_element *stackframe_peek(struct node *func_node) {
struct stack_frame *frame = &func_node->func.frame;
return vector_peek(frame->elements);
}
void stackframe_push(struct node *func_node,
struct stack_frame_element *element) {
struct stack_frame *frame = &func_node->func.frame;
// The stack grows downwards
element->offset_from_bp = -(vector_count(frame->elements) * STACK_PUSH_SIZE);
vector_push(frame->elements, element);
}
void stackframe_sub(struct node *func_node, int type, const char *name,
size_t amount) {
if (amount % STACK_PUSH_SIZE != 0) {
compiler_error(cp, "stackframe error: allignment issue");
}
size_t total_pushes = amount / STACK_PUSH_SIZE;
for (size_t i = 0; i < total_pushes; i++) {
stackframe_push(func_node,
&(struct stack_frame_element){.type = type, .name = name});
}
}
void stackframe_add(struct node *func_node, int type, const char *name,
size_t amount) {
if (amount % STACK_PUSH_SIZE != 0) {
compiler_error(cp, "stackframe error: allignment issue");
}
size_t total_pushes = amount / STACK_PUSH_SIZE;
for (size_t i = 0; i < total_pushes; i++) {
stackframe_pop(func_node);
}
}
void stackframe_assert_empty(struct node *func_node) {
struct stack_frame *frame = &func_node->func.frame;
if (vector_count(frame->elements) != 0) {
compiler_error(cp, "stack error: stack not empty");
}
}