-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathstackframe.c
More file actions
104 lines (93 loc) · 3.54 KB
/
stackframe.c
File metadata and controls
104 lines (93 loc) · 3.54 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
/*
* PeachCompiler C Compiler Project
* Copyright (C) 2026 Daniel McCarthy <daniel@dragonzap.com>
* This file is part of the PeachCompiler.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* For full source code, documentation, and structured learning,
* see the official compiler development video course by Daniel McCarthy here:
* dragonzap.com/course/creating-a-c-compiler-from-scratch
*
* Learn to build your own C compiler from scratch with that video course over 39 hours of video content available.
*/
#include "compiler.h"
#include "helpers/vector.h"
#include <assert.h>
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);
assert(last_element);
assert(last_element->type == expecting_type && S_EQ(last_element->name, expecting_name));
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)
{
assert((amount % STACK_PUSH_SIZE) == 0);
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)
{
assert((amount % STACK_PUSH_SIZE) == 0);
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;
assert(vector_count(frame->elements) == 0);
}