summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-07-19 21:20:45 -0400
committerIan Moffett <ian@osmora.org>2025-07-19 21:53:18 -0400
commit6759631b90b0ee1a2ce52be410fbe9173b3c2af5 (patch)
tree7f95eab4ad8a5cf290277958ef860e87b06ae84f
parentc4da1b15cfde65a1d1173d3ae256bb54369fa4e4 (diff)
usr: Add initial "oasm" assembler work
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r--usr.bin/oasm/Makefile7
-rw-r--r--usr.bin/oasm/include/oasm/lex.h118
-rw-r--r--usr.bin/oasm/include/oasm/log.h48
-rw-r--r--usr.bin/oasm/include/oasm/parse.h37
-rw-r--r--usr.bin/oasm/include/oasm/state.h41
-rw-r--r--usr.bin/oasm/lex.c272
-rw-r--r--usr.bin/oasm/oasm.c63
-rw-r--r--usr.bin/oasm/parse.c113
8 files changed, 699 insertions, 0 deletions
diff --git a/usr.bin/oasm/Makefile b/usr.bin/oasm/Makefile
new file mode 100644
index 0000000..a83aaab
--- /dev/null
+++ b/usr.bin/oasm/Makefile
@@ -0,0 +1,7 @@
+include user.mk
+
+CFILES = $(shell find . -name "*.c")
+CFLAGS = -Iinclude/
+
+$(ROOT)/base/usr/bin/oasm:
+ gcc $(CFILES) -o $@ $(INTERNAL_CFLAGS) $(CFLAGS)
diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h
new file mode 100644
index 0000000..6688348
--- /dev/null
+++ b/usr.bin/oasm/include/oasm/lex.h
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _OASM_LEX_H_
+#define _OASM_LEX_H_
+
+#include <stdint.h>
+#include <oasm/state.h>
+
+#define __XN_REGS \
+ TT_X0, \
+ TT_X1, \
+ TT_X2, \
+ TT_X3, \
+ TT_X4, \
+ TT_X5, \
+ TT_X6, \
+ TT_X7, \
+ TT_X8, \
+ TT_X9, \
+ TT_X10, \
+ TT_X11, \
+ TT_X12, \
+ TT_X13, \
+ TT_X14, \
+ TT_X15
+
+#define __FN_REGS \
+ TT_F0, \
+ TT_F1, \
+ TT_F2, \
+ TT_F3, \
+ TT_F4, \
+ TT_F5, \
+ TT_F6, \
+ TT_F7
+
+#define __DN_REGS \
+ TT_D0, \
+ TT_D1, \
+ TT_D2, \
+ TT_D3, \
+ TT_D4, \
+ TT_D5, \
+ TT_D6, \
+ TT_D7
+
+#define __VN_REGS \
+ TT_V0, \
+ TT_V1, \
+ TT_V2, \
+ TT_V3, \
+ TT_V4, \
+ TT_V5, \
+ TT_V6, \
+ TT_V7
+
+/*
+ * Token type definitions
+ */
+typedef enum {
+ TT_UNKNOWN, /* Unknown token */
+
+ /* Arithmetic instructions */
+ TT_ADD, /* 'add' */
+ TT_SUB, /* 'sub' */
+ TT_MUL, /* 'mul' */
+ TT_DIV, /* 'div' */
+
+ /* Register ops */
+ TT_MOV,
+ TT_INC,
+ TT_DEC,
+
+ /* Register sets */
+ __XN_REGS,
+ __FN_REGS,
+ __DN_REGS,
+ __VN_REGS,
+
+ /* Symbols */
+ TT_COMMA, /* ',' */
+} tt_t;
+
+struct oasm_token {
+ tt_t type;
+ char *raw;
+};
+
+int lex_tok(struct oasm_state *state, struct oasm_token *ttp);
+
+#endif /* !_OASM_LEX_H_ */
diff --git a/usr.bin/oasm/include/oasm/log.h b/usr.bin/oasm/include/oasm/log.h
new file mode 100644
index 0000000..330c273
--- /dev/null
+++ b/usr.bin/oasm/include/oasm/log.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _OASM_LOG_H_
+#define _OASM_LOG_H_
+
+#include <sys/types.h>
+#include <sys/cdefs.h>
+#include <stdio.h>
+
+#define ERROR_COLOR "\033[31;40m"
+#define WARN_COLOR "\033[35;40m"
+
+void __oasm_debug(const char *fmt, ...);
+void __oasm_err(const char *fmt, ...);
+void __oasm_warn(const char *fmt, ...);
+
+#define oasm_debug(...) __oasm_debug(__VA_ARGS__)
+#define oasm_err(...) __oasm_err(__VA_ARGS__)
+#define oasm_warn(...) __oasm_warn(__VA_ARGS__)
+
+#endif /* !_OASM_LOG_H_ */
diff --git a/usr.bin/oasm/include/oasm/parse.h b/usr.bin/oasm/include/oasm/parse.h
new file mode 100644
index 0000000..04962e7
--- /dev/null
+++ b/usr.bin/oasm/include/oasm/parse.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _OASM_PARSE_H_
+#define _OASM_PARSE_H_
+
+#include <oasm/state.h>
+
+void parse_enter(struct oasm_state *state);
+
+#endif /* !_OASM_PARSE_H_ */
diff --git a/usr.bin/oasm/include/oasm/state.h b/usr.bin/oasm/include/oasm/state.h
new file mode 100644
index 0000000..c21d877
--- /dev/null
+++ b/usr.bin/oasm/include/oasm/state.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _OASM_STATE_H_
+#define _OASM_STATE_H_
+
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+struct oasm_state {
+ int in_fd;
+};
+
+#endif /* !_OASM_STATE_H_ */
diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c
new file mode 100644
index 0000000..a61c62a
--- /dev/null
+++ b/usr.bin/oasm/lex.c
@@ -0,0 +1,272 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/errno.h>
+#include <string.h>
+#include <oasm/state.h>
+#include <oasm/lex.h>
+#include <oasm/log.h>
+
+#define is_num(c) ((c) >= '0' && (c) <= '9')
+
+static char putback = '\0';
+
+/* Instruction mnemonic strings */
+#define S_IMN_MOV "mov"
+#define S_IMN_ADD "add"
+#define S_IMN_SUB "sub"
+#define S_IMN_DIV "div"
+#define S_IMN_INC "inc"
+#define S_IMN_DEC "dec"
+
+/*
+ * Returns 0 if a char is counted as a
+ * skippable token. Otherwise, -1
+ */
+static inline int
+lex_skippable(char c)
+{
+ switch (c) {
+ case ' ':
+ case '\f':
+ case '\t':
+ case '\r':
+ case '\n':
+ return 0;
+ }
+
+ return -1;
+}
+
+/*
+ * Put back a token to grab later
+ *
+ * @c: Character to put back
+ */
+static inline char
+lex_putback(char c)
+{
+ putback = c;
+ return c;
+}
+
+/*
+ * Grab a character from the input file
+ * descriptor.
+ */
+static char
+lex_cin(struct oasm_state *state)
+{
+ char retval;
+
+ if (putback != '\0') {
+ retval = putback;
+ putback = '\0';
+ return retval;
+ }
+
+ if (read(state->in_fd, &retval, 1) <= 0) {
+ return '\0';
+ }
+ return retval;
+}
+
+/*
+ * Nom an operation, directive or any kind
+ * of raw string (unquoted/builtin) and return
+ * memory allocated by strdup() pointing to the
+ * string.
+ *
+ * @state: OASM state pointer
+ * @res: Resulting string
+ *
+ * Returns 0 on success. Greater than zero
+ * value of the last character if a comma or
+ * space was not buffered.
+ */
+static int
+lex_nomstr(struct oasm_state *state, char **res)
+{
+ char buf[256];
+ int retval = 0, n = 0;
+ int tmp;
+
+ memset(buf, 0, sizeof(buf));
+
+ /*
+ * We are filling the buffer containing
+ * the operation or directive.
+ *
+ * Keep going until we hit a space or comman (^)
+ * Examples of such strings (everything in '[]'):
+ *
+ * [mov] [x0], [#1]
+ * ^ ^
+ */
+ while ((tmp = lex_cin(state)) != 0) {
+ if (tmp == ' ' || tmp == ',') {
+ retval = tmp;
+ break;
+ }
+ if (tmp == '\n') {
+ retval = tmp;
+ break;
+
+ }
+
+ buf[n++] = tmp;
+ }
+
+ *res = strdup(buf);
+ return retval;
+}
+
+static tt_t
+token_arith(char *p)
+{
+ if (strcmp(p, S_IMN_MOV) == 0) {
+ return TT_MOV;
+ } else if (strcmp(p, S_IMN_INC) == 0) {
+ return TT_INC;
+ } else if (strcmp(p, S_IMN_DEC) == 0) {
+ return TT_DEC;
+ } else if (strcmp(p, S_IMN_ADD) == 0) {
+ return TT_ADD;
+ } else if (strcmp(p, S_IMN_SUB) == 0) {
+ return TT_SUB;
+ } else if (strcmp(p, S_IMN_DIV) == 0) {
+ return TT_DIV;
+ }
+
+ return TT_UNKNOWN;
+}
+
+static tt_t
+token_xreg(char *p)
+{
+ int num;
+
+ if (p[0] != 'x') {
+ return TT_UNKNOWN;
+ }
+
+ if (!is_num(p[1])) {
+ return TT_UNKNOWN;
+ }
+
+ num = atoi(&p[1]);
+ switch (num) {
+ case 0: return TT_X0;
+ case 1: return TT_X1;
+ case 2: return TT_X2;
+ case 3: return TT_X3;
+ case 4: return TT_X4;
+ case 5: return TT_X5;
+ case 6: return TT_X6;
+ case 7: return TT_X7;
+ case 8: return TT_X8;
+ case 9: return TT_X9;
+ case 10: return TT_X10;
+ case 11: return TT_X11;
+ case 12: return TT_X12;
+ case 13: return TT_X13;
+ case 14: return TT_X14;
+ case 15: return TT_X15;
+ }
+
+ return TT_UNKNOWN;
+}
+
+static tt_t
+token_reg(char *p)
+{
+ tt_t tok;
+
+ if ((tok = token_xreg(p)) != TT_UNKNOWN) {
+ return tok;
+ }
+
+ return TT_UNKNOWN;
+}
+
+int
+lex_tok(struct oasm_state *state, struct oasm_token *ttp)
+{
+ char *p;
+ char c = ' ';
+ int tmp;
+ tt_t tok;
+
+ if (state == NULL || ttp == NULL) {
+ return -EINVAL;
+ }
+
+ /*
+ * Grab characters. If they are skippable,
+ * don't use them.
+ */
+ while (lex_skippable(c) == 0) {
+ if ((c = lex_cin(state)) == 0) {
+ return -1;
+ }
+ }
+
+ switch (c) {
+ case '\n':
+ return 0;
+ case '\0':
+ return -1;
+ case ',':
+ return TT_COMMA;
+ default:
+ ttp->type = TT_UNKNOWN;
+ ttp->raw = NULL;
+
+ lex_putback(c);
+ lex_nomstr(state, &p);
+
+ /* Arithmetic operation? */
+ if ((tok = token_arith(p)) != TT_UNKNOWN) {
+ ttp->type = tok;
+ ttp->raw = p;
+ return 0;
+ }
+
+ /* Register? */
+ if ((tok = token_reg(p)) != TT_UNKNOWN) {
+ ttp->type = tok;
+ ttp->raw = p;
+ return 0;
+ }
+ oasm_err("bad token %s\n", p);
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/usr.bin/oasm/oasm.c b/usr.bin/oasm/oasm.c
new file mode 100644
index 0000000..670a5f4
--- /dev/null
+++ b/usr.bin/oasm/oasm.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <oasm/state.h>
+#include <oasm/parse.h>
+#define OASM_DBG
+#include <oasm/log.h>
+
+static struct oasm_state state;
+
+static void
+oasm_start(struct oasm_state *state)
+{
+ parse_enter(state);
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc < 2) {
+ printf("oasm: usage: oasm <file> <output>\n");
+ return -1;
+ }
+
+ state.in_fd = open(argv[1], O_RDONLY);
+ if (state.in_fd < 0) {
+ printf("could not open \"%s\"\n", argv[1]);
+ return -1;
+ }
+
+ oasm_start(&state);
+ close(state.in_fd);
+ return 0;
+}
diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c
new file mode 100644
index 0000000..45c697a
--- /dev/null
+++ b/usr.bin/oasm/parse.c
@@ -0,0 +1,113 @@
+/* Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <oasm/state.h>
+#include <oasm/lex.h>
+#include <oasm/parse.h>
+#include <oasm/log.h>
+
+static const char *tokstr[] = {
+ [ TT_UNKNOWN] = "bad",
+ [ TT_ADD ] = "add",
+ [ TT_SUB ] = "sub",
+ [ TT_MUL ] = "mul",
+ [ TT_DIV ] = "div",
+ [ TT_COMMA ] = ",",
+ [ TT_INC ] = "inc",
+ [ TT_DEC ] = "dec",
+ [ TT_MOV ] = "mov",
+
+ /* X<n> registers */
+ [ TT_X0 ] = "x0",
+ [ TT_X1 ] = "x1",
+ [ TT_X2 ] = "x2",
+ [ TT_X3 ] = "x3",
+ [ TT_X4 ] = "x4",
+ [ TT_X5 ] = "x5",
+ [ TT_X6 ] = "x6",
+ [ TT_X7 ] = "x7",
+ [ TT_X8 ] = "x8",
+ [ TT_X9 ] = "x9",
+ [ TT_X10 ] = "x10",
+ [ TT_X11 ] = "x11",
+ [ TT_X12 ] = "x12",
+ [ TT_X13 ] = "x13",
+ [ TT_X14 ] = "x14",
+ [ TT_X15 ] = "x15",
+
+ /* V<n> registers */
+ [ TT_F0 ] = "v0",
+ [ TT_F1 ] = "v1",
+ [ TT_F2 ] = "v2",
+ [ TT_F3 ] = "v3",
+ [ TT_F4 ] = "v4",
+ [ TT_F5 ] = "v5",
+ [ TT_F6 ] = "v6",
+ [ TT_F7 ] = "v7",
+
+ /* D<n> registers */
+ [ TT_D0 ] = "d0",
+ [ TT_D1 ] = "d1",
+ [ TT_D2 ] = "d2",
+ [ TT_D3 ] = "d3",
+ [ TT_D4 ] = "d4",
+ [ TT_D5 ] = "d5",
+ [ TT_D6 ] = "d6",
+ [ TT_D7 ] = "d7",
+
+ /* V<n> registers */
+ [ TT_V0 ] = "v0",
+ [ TT_V1 ] = "v1",
+ [ TT_V2 ] = "v2",
+ [ TT_V3 ] = "v3",
+ [ TT_V4 ] = "v4",
+ [ TT_V5 ] = "v5",
+ [ TT_V6 ] = "v6",
+ [ TT_V7 ] = "v7",
+};
+
+void
+parse_enter(struct oasm_state *state)
+{
+ struct oasm_token tok;
+ int error = 0;
+
+ for (;;) {
+ error = lex_tok(state, &tok);
+ if (error < 0) {
+ break;
+ }
+
+ if (tok.raw != NULL) {
+ free(tok.raw);
+ }
+ }
+}