-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
153 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "compiler.h" | ||
#include "helpers/vector.h" | ||
#include <assert.h> | ||
|
||
void stackframe_pop(struct node *func_node) { | ||
struct stack_frame *frame = &func_node->func.stack_frame; | ||
vector_pop(frame->elements); | ||
} | ||
|
||
struct stack_frame_element *stackframe_back(struct node *func_node) { | ||
struct stack_frame *frame = &func_node->func.stack_frame; | ||
return vector_back_or_null(frame->elements); | ||
} | ||
|
||
struct stack_frame_element *stackframe_back_expect(struct node *func_node, | ||
int expecting_type, | ||
const char *expecting_name) { | ||
struct stack_frame_element *last_element = stackframe_back(func_node); | ||
if (last_element && last_element->type != expecting_type || | ||
!S_EQ(last_element->name, expecting_name)) { | ||
return NULL; | ||
} | ||
|
||
return last_element; | ||
} | ||
|
||
void stackframe_pop_expecting(struct node *func_node, int expecting_type, | ||
const char *expecting_name) { | ||
struct stack_frame *frame = &func_node->func.stack_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.stack_frame; | ||
vector_set_peek_pointer(frame->elements, 0); | ||
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.stack_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.stack_frame; | ||
// 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.stack_frame; | ||
assert(vector_count(frame->elements) == 0); | ||
} |