summaryrefslogtreecommitdiff
path: root/include/obe
diff options
context:
space:
mode:
Diffstat (limited to 'include/obe')
-rw-r--r--include/obe/arena.h77
-rw-r--r--include/obe/array.h60
-rw-r--r--include/obe/ir.h80
-rw-r--r--include/obe/lexer.h84
-rw-r--r--include/obe/parser.h40
-rw-r--r--include/obe/string.h42
-rw-r--r--include/obe/utils.h27
-rw-r--r--include/obe/x86_64/codegen.h31
8 files changed, 441 insertions, 0 deletions
diff --git a/include/obe/arena.h b/include/obe/arena.h
new file mode 100644
index 0000000..5b3490a
--- /dev/null
+++ b/include/obe/arena.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_ARENA_H
+#define OBE_ARENA_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#define OBE_ARENA_ALIGMENT_BYTES (16)
+#define OBE_ARENA_ALIGMENT_MASK (OBE_ARENA_ALIGMENT_BYTES - 1)
+#define OBE_ARENA_REGION_DEFAULT_CAPACITY (8 * 1024)
+#define OBE_ARENA_PADDING(bytes) \
+ ((uint8_t)((OBE_ARENA_ALIGMENT_BYTES - bytes) & OBE_ARENA_ALIGMENT_MASK))
+
+typedef struct obe_arena_region obe_arena_region_t;
+
+struct obe_arena_region
+{
+ obe_arena_region_t* next;
+ size_t offset;
+ size_t capacity;
+ uint8_t* data;
+};
+
+typedef struct obe_arena
+{
+ obe_arena_region_t* begin;
+ obe_arena_region_t* end;
+} obe_arena_t;
+
+/* arena */
+
+void*
+obe_arena_alloc(obe_arena_t* arena, size_t size);
+
+void*
+obe_arena_realloc(obe_arena_t* arena,
+ void* old_ptr,
+ size_t old_size,
+ size_t new_size);
+
+void
+obe_arena_release(obe_arena_t* arena);
+
+void
+obe_arena_free(obe_arena_t* arena);
+
+/* region */
+
+obe_arena_region_t*
+obe_arena_region_new(size_t capacity);
+
+void
+obe_arena_region_free(obe_arena_region_t* r);
+
+char*
+obe_arena_strdup(obe_arena_t* arena, char* s);
+
+#endif /* OBE_ARENA_H */
diff --git a/include/obe/array.h b/include/obe/array.h
new file mode 100644
index 0000000..4ef9204
--- /dev/null
+++ b/include/obe/array.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_ARRAY_H
+#define OBE_ARRAY_H
+#include <obe/arena.h>
+#include <stddef.h>
+
+#define OBE_ARRAY_INITIAL_CAPACITY 4
+
+#define obe_array(arena, T) (T*)obe_array_new((arena), sizeof(T))
+
+#define obe_array_append(arr, item) \
+ do { \
+ obe_array_header_t* h = obe_array_get_header(arr); \
+ if (h->capacity < h->length + 1) { \
+ arr = obe_array_grow(arr); \
+ h = obe_array_get_header(arr); \
+ } \
+ arr[h->length++] = item; \
+ } while (0)
+
+typedef struct obe_array_header
+{
+ obe_arena_t* arena;
+ size_t capacity;
+ size_t item_size;
+ size_t length;
+} obe_array_header_t;
+
+void*
+obe_array_new(obe_arena_t* arena, size_t item_size);
+
+obe_array_header_t*
+obe_array_get_header(void* arr);
+
+void*
+obe_array_grow(void* arr);
+
+size_t
+obe_array_length(void* arr);
+
+#endif /* OBE_ARRAY_H */
diff --git a/include/obe/ir.h b/include/obe/ir.h
new file mode 100644
index 0000000..ae6f797
--- /dev/null
+++ b/include/obe/ir.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_IR_H
+#define OBE_IR_H
+
+#include <obe/arena.h>
+#include <obe/string.h>
+
+typedef enum obe_ir_inst_kind
+{
+ OBE_IR_INST_CONST,
+ OBE_IR_INST_PRINT,
+ OBE_IR_INST_RETURN,
+ OBE_IR_INST_BR,
+ OBE_IR_INST_JMP,
+ OBE_IR_INST_LABEL,
+} obe_ir_inst_kind_t;
+
+typedef enum obe_ir_type
+{
+ OBE_IR_TYPE_INT
+} obe_ir_type_t;
+
+typedef enum obe_ir_operand_kind
+{
+ OBE_IR_OPERAND_LITERAL,
+ OBE_IR_OPERAND_IDENT
+} obe_ir_operand_kind_t;
+
+typedef struct obe_ir_operand
+{
+ obe_ir_operand_kind_t kind;
+ char *value;
+} obe_ir_operand_t;
+
+typedef struct obe_ir_inst
+{
+ obe_ir_inst_kind_t kind;
+ obe_ir_type_t type;
+ char* dest;
+ obe_ir_operand_t operand1;
+ obe_ir_operand_t operand2;
+} obe_ir_inst_t;
+
+typedef struct obe_ir_function
+{
+ obe_string_t name;
+ obe_ir_inst_t* instrs;
+} obe_ir_function_t;
+
+typedef struct obe_ir_translation_unit
+{
+ obe_ir_function_t* funcs;
+} obe_ir_translation_unit_t;
+
+obe_ir_translation_unit_t*
+obe_ir_translation_unit_new(obe_arena_t* arena);
+
+obe_ir_function_t*
+obe_ir_function_new(obe_arena_t* arena, obe_string_t name);
+
+#endif /* OBE_IR_H */
diff --git a/include/obe/lexer.h b/include/obe/lexer.h
new file mode 100644
index 0000000..9761aa7
--- /dev/null
+++ b/include/obe/lexer.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_LEXER_H
+#define OBE_LEXER_H
+
+#include <obe/string.h>
+#include <stddef.h>
+
+typedef enum obe_token_kind
+{
+ TOKEN_KW_RETURN,
+ TOKEN_KW_FN,
+ TOKEN_KW_BR,
+ TOKEN_IDENT,
+ TOKEN_LABEL,
+ TOKEN_NUMBER,
+ TOKEN_INT,
+ TOKEN_EQ,
+ TOKEN_COLON,
+ TOKEN_SEMICOLON,
+ TOKEN_LBRACE,
+ TOKEN_RBRACE,
+ TOKEN_EOF,
+ TOKEN_UNKOWN
+} obe_token_kind_t;
+
+typedef struct obe_lexer_loc
+{
+ size_t offset;
+ size_t lineoffset;
+ size_t lineno;
+} obe_lexer_loc_t;
+
+typedef struct obe_token
+{
+ obe_token_kind_t kind;
+ obe_string_t value;
+ obe_lexer_loc_t loc;
+} obe_token_t;
+
+typedef struct obe_lexer
+{
+ char* filename;
+ obe_string_t source;
+ obe_lexer_loc_t loc;
+} obe_lexer_t;
+
+void
+obe_lexer_init(obe_lexer_t* lexer, char* filename);
+
+bool
+obe_lexer_is_eof(obe_lexer_t* lexer);
+
+char
+obe_lexer_current_char(obe_lexer_t* lexer);
+
+char
+obe_lexer_next_char(obe_lexer_t* lexer);
+
+void
+obe_lexer_next_token(obe_lexer_t* lexer, obe_token_t* token);
+
+char*
+obe_token_to_cstr(obe_token_kind_t kind);
+
+#endif /* OBE_LEXER_H */
diff --git a/include/obe/parser.h b/include/obe/parser.h
new file mode 100644
index 0000000..026af5c
--- /dev/null
+++ b/include/obe/parser.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_PARSER_H
+#define OBE_PARSER_H
+
+#include <obe/arena.h>
+#include <obe/ir.h>
+#include <obe/lexer.h>
+
+typedef struct obe_parser
+{
+ obe_arena_t* arena;
+ obe_lexer_t* lexer;
+} obe_parser_t;
+
+void
+obe_parser_init(obe_parser_t* parser, obe_lexer_t* lexer, obe_arena_t* arena);
+
+obe_ir_translation_unit_t*
+obe_parser_parse(obe_parser_t* parser);
+
+#endif /* OBE_PARSER_H */
diff --git a/include/obe/string.h b/include/obe/string.h
new file mode 100644
index 0000000..6194cb2
--- /dev/null
+++ b/include/obe/string.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_STRING_H
+#define OBE_STRING_H
+
+#define PRIs "%.*s"
+#define PRIsARG(s) (int)(s).length, (s).chars
+
+#include <stdbool.h>
+#include <stddef.h>
+
+typedef struct obe_string
+{
+ char* chars;
+ size_t length;
+} obe_string_t;
+
+obe_string_t
+obe_string_from_cstr(char* cstr);
+
+bool
+obe_string_eq(obe_string_t s1, obe_string_t s2);
+
+#endif /* OBE_STRING_H */
diff --git a/include/obe/utils.h b/include/obe/utils.h
new file mode 100644
index 0000000..ef4c1c5
--- /dev/null
+++ b/include/obe/utils.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_UTILS_H
+#define OBE_UTILS_H
+
+char*
+read_file_contents(char* file_path);
+
+#endif /* OBE_UTILS_H */
diff --git a/include/obe/x86_64/codegen.h b/include/obe/x86_64/codegen.h
new file mode 100644
index 0000000..01ad3ff
--- /dev/null
+++ b/include/obe/x86_64/codegen.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe 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 Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef OBE_X86_64_CODEGEN_H
+#define OBE_X86_64_CODEGEN_H
+
+#include <obe/ir.h>
+#include <stdio.h>
+
+void
+obe_x86_64_codegen_emit(obe_ir_translation_unit_t* tu, FILE *out);
+
+#endif /* OBE_X86_64_CODEGEN_H */