summaryrefslogtreecommitdiff
path: root/masm/sectbl.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-09-09 12:41:49 -0400
committerFreya Murphy <freya@freyacat.org>2024-09-09 12:41:49 -0400
commit2ed275821676a0d5baea6c7fd843d71c72c2342c (patch)
tree480297f28e5c42d02a47b3b94027a7abe507d010 /masm/sectbl.c
downloadmips-2ed275821676a0d5baea6c7fd843d71c72c2342c.tar.gz
mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.tar.bz2
mips-2ed275821676a0d5baea6c7fd843d71c72c2342c.zip
initial mips32 (r2000ish mips32r6) assembler
Diffstat (limited to 'masm/sectbl.c')
-rw-r--r--masm/sectbl.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/masm/sectbl.c b/masm/sectbl.c
new file mode 100644
index 0000000..f568a6f
--- /dev/null
+++ b/masm/sectbl.c
@@ -0,0 +1,103 @@
+#include <string.h>
+#include <stdlib.h>
+#include <mips.h>
+#include <merror.h>
+#include <mlimits.h>
+
+#include "parse.h"
+
+#define SECTBL_INIT_LEN 8
+static const char inital_section[MAX_LEX_LENGTH] = "data";
+
+int sectbl_init(struct section_table *sec_tbl)
+{
+ sec_tbl->len = SECTBL_INIT_LEN;
+ sec_tbl->count = 0;
+ sec_tbl->total_ins = 0;
+ sec_tbl->sections = malloc(sizeof(struct section) * SECTBL_INIT_LEN);
+
+ if (sec_tbl->sections == NULL) {
+ ERROR("cannot alloc");
+ return M_ERROR;
+ }
+
+ if (sectbl_alloc(sec_tbl, &sec_tbl->current, inital_section))
+ return M_ERROR;
+
+ return M_SUCCESS;
+}
+
+void sectbl_free(struct section_table *sec_tbl)
+{
+ for (uint32_t i = 0; i < sec_tbl->count; i++) {
+ free(sec_tbl->sections[i].ins);
+ }
+ free(sec_tbl->sections);
+}
+
+int sectbl_alloc(struct section_table *sec_tbl, struct section **sec,
+ const char name[MAX_LEX_LENGTH])
+{
+ if (sec_tbl->count >= sec_tbl->len) {
+ sec_tbl->len *= 2;
+ sec_tbl->sections = realloc(sec_tbl->sections,
+ sizeof(struct section) * sec_tbl->len);
+
+ if (sec_tbl->sections == NULL) {
+ ERROR("cannot realloc");
+ return M_ERROR;
+ }
+ }
+
+ struct section *temp;
+ temp = &sec_tbl->sections[sec_tbl->count++];
+ strcpy(temp->name,name);
+ temp->count = 0;
+ temp->len = SECTBL_INIT_LEN;
+ temp->start = sec_tbl->total_ins;
+ temp->alignment = 1;
+ temp->ins = malloc(sizeof(union mips_instruction) * SECTBL_INIT_LEN);
+
+ if (temp->ins == NULL) {
+ ERROR("cannot alloc");
+ return M_ERROR;
+ }
+
+ *sec = temp;
+ return M_SUCCESS;
+}
+
+int sectbl_push(struct section_table *sec_tbl, struct section *section,
+ union mips_instruction ins)
+{
+ if (section->count >= section->len) {
+ section->len *= 2;
+ section->ins = realloc(section->ins,
+ sizeof(union mips_instruction) * section->len);
+
+ if (section->ins == NULL) {
+ ERROR("cannot realloc");
+ return M_ERROR;
+ }
+ }
+
+ section->ins[section->count++] = ins;
+ sec_tbl->total_ins++;
+
+ return M_SUCCESS;
+}
+
+int sectbl_get(struct section_table *sec_tbl, struct section **sec,
+ const char name[MAX_LEX_LENGTH])
+{
+ for (uint32_t i = 0; i < sec_tbl->count; i++) {
+ struct section *temp = &sec_tbl->sections[i];
+ if (strcmp(name, temp->name) == 0) {
+ if (sec != NULL)
+ *sec = temp;
+ return M_SUCCESS;
+ }
+ }
+
+ return M_ERROR;
+}