Initial commit of files related to NED architecture.
[ned1] / nedasm / nedasm_structures.c
/*
* © 2018 Aaron Taylor <ataylor at subgeniuskitty dot com>
* See LICENSE.txt file for copyright and license details.
*/
#include <stdio.h>
#include <stdlib.h>
#include "nedasm_structures.h"
struct instruction *
seek_instruction_list_start(struct instruction * instructions)
{
if (instructions == NULL) return NULL;
while (instructions->prev != NULL) instructions = instructions->prev;
return instructions;
}
struct instruction *
seek_instruction_list_end(struct instruction * instructions)
{
if (instructions == NULL) return NULL;
while (instructions->next != NULL) instructions = instructions->next;
return instructions;
}
void
insert_instruction_struct_before(struct instruction * list, struct instruction * new_node)
{
new_node->next = list;
new_node->prev = list->prev;
list->prev = new_node;
if (new_node->prev != NULL) (new_node->prev)->next = new_node;
}
void
insert_instruction_struct_after(struct instruction * list, struct instruction * new_node)
{
new_node->prev = list;
new_node->next = list->next;
list->next = new_node;
if (new_node->next != NULL) (new_node->next)->prev = new_node;
}
struct instruction *
remove_instruction_struct(struct instruction * instructions)
{
//struct instruction * delete_me = instructions;
struct instruction * return_me = NULL;
if (instructions->prev != NULL) {
instructions->prev->next = instructions->next;
return_me = instructions->prev;
}
if (instructions->next != NULL) {
instructions->next->prev = instructions->prev;
if (return_me != NULL) return_me = instructions->next;
}
// TODO: Given the one-pass & die-early nature of this task, skip memory management entirely?
//if (delete_me->label != NULL) free(delete_me->label);
//if (delete_me->target != NULL) free(delete_me->target);
//free(delete_me);
return return_me;
}
struct instruction *
create_instruction_struct(void)
{
struct instruction * new_struct = malloc(sizeof(struct instruction));
new_struct->prev = NULL;
new_struct->next = NULL;
new_struct->linenum = 0;
new_struct->syllable = 0;
new_struct->data = 0;
new_struct->target = NULL;
new_struct->label = NULL;
new_struct->address = 0;
return new_struct;
}
void
insert_NOP_structs_after(struct instruction * list, uint8_t count)
{
while (count != 0) {
struct instruction * new_struct = create_instruction_struct();
new_struct->syllable = NOP;
insert_instruction_struct_after(list, new_struct);
count--;
}
}
void
insert_NOP_structs_before(struct instruction * list, uint8_t count)
{
while (count != 0) {
struct instruction * new_struct = create_instruction_struct();
new_struct->syllable = NOP;
insert_instruction_struct_before(list, new_struct);
count--;
}
}