Cthulhu  0.2.10
Cthulhu compiler collection
main.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #include "pl0/sema.h"
4 #include "pl0/ast.h"
5 
6 #include "interop/compile.h"
7 #include "driver/driver.h"
8 
9 #include "std/str.h"
10 #include "std/vector.h"
11 
12 #include "pl0_bison.h" // IWYU pragma: keep
13 #include "pl0_flex.h" // IWYU pragma: keep
14 
15 CT_CALLBACKS(kCallbacks, pl0);
16 
17 static void pl0_postparse(language_runtime_t *runtime, scan_t *scan, void *tree)
18 {
19  pl0_t *ast = tree;
20  CTASSERT(ast->type == ePl0Module);
21  arena_t *arena = runtime->arena;
22 
23  const vector_t *path = vector_len(ast->mod) > 0
24  ? ast->mod
25  : vector_init(str_basename(scan_path(scan), arena), arena);
26 
27  size_t const_count = vector_len(ast->consts);
28  size_t global_count = vector_len(ast->globals);
29  size_t proc_count = vector_len(ast->procs);
30 
31  const size_t sizes[ePl0TagTotal] = {
32  [ePl0TagValues] = const_count + global_count,
33  [ePl0TagProcs] = proc_count,
34  [ePl0TagImportedValues] = 64,
35  [ePl0TagImportedProcs] = 64
36  };
37 
38  lang_add_unit(runtime, build_unit_id(path, arena), ast->node, ast, sizes, ePl0TagTotal);
39 }
40 
41 static const char *const kLangNames[] = CT_LANG_EXTS("pl", "pl0");
42 
43 static const size_t kRootSizes[ePl0TagTotal] = {
44  [ePl0TagValues] = 1,
45  [ePl0TagTypes] = 1,
46  [ePl0TagProcs] = 1,
47  [ePl0TagModules] = 1
48 };
49 
50 static const char *const kDeclNames[ePl0TagTotal] = {
51 #define DECL_TAG(id, init, str) [id] = (str),
52 #include "pl0/pl0.inc"
53 };
54 
55 CT_DRIVER_API const language_t kPl0Module = {
56  .info = {
57  .id = "lang/pl0",
58  .name = "PL/0",
59  .version = {
60  .license = "LGPLv3",
61  .author = "Elliot Haisley",
62  .desc = "PL/0 language driver",
63  .version = CT_NEW_VERSION(2, 3, 2)
64  },
65  },
66 
67  .builtin = {
68  .name = CT_TEXT_VIEW("pl0\0lang"),
69  .decls = kRootSizes,
70  .names = kDeclNames,
71  .length = ePl0TagTotal,
72  },
73 
74  .ast_size = sizeof(pl0_t),
75 
76  .exts = kLangNames,
77 
78  .fn_create = pl0_init,
79 
80  .fn_postparse = pl0_postparse,
81  .scanner = &kCallbacks,
82 
83  .fn_passes = {
84  [ePassForwardDecls] = pl0_forward_decls,
85  [ePassImportModules] = pl0_process_imports,
86  [ePassCompileDecls] = pl0_compile_module
87  }
88 };
89 
#define CT_TEXT_VIEW(str)
create a text view from a string literal
Definition: text.h:39
CT_NODISCARD CT_PUREFN CT_SCAN_API const char * scan_path(const scan_t *scan)
get the path of a scanner
Definition: scan.c:56
CT_BROKER_API void lang_add_unit(language_runtime_t *runtime, unit_id_t id, const node_t *node, void *ast, const size_t *sizes, size_t count)
all runtime apis
Definition: broker.c:462
#define CT_LANG_EXTS(...)
Definition: broker.h:49
CT_BROKER_API text_view_t build_unit_id(const vector_t *parts, arena_t *arena)
Definition: broker.c:532
#define CT_NEW_VERSION(major, minor, patch)
creates a new ctu_version_t from major, minor and patch
Definition: version_def.h:20
#define CT_LANG_EXPORT(mod)
declares the entry point for a language driver module
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
CT_NODISCARD CT_STD_API char * str_basename(const char *path, arena_t *arena)
get the filename from a path
Definition: str.c:203
CT_NODISCARD CT_STD_API vector_t * vector_init(void *value, arena_t *arena)
create a new vector with a single initial value
Definition: vector.c:79
CT_NODISCARD CT_PUREFN CT_STD_API size_t vector_len(const vector_t *vector)
get the length of a vector
Definition: vector.c:152
CT_CALLBACKS(kCallbacks, ctu)
CT_DRIVER_API const language_t kPl0Module
Definition: main.c:55
void pl0_process_imports(language_runtime_t *runtime, compile_unit_t *context)
Definition: sema.c:534
void pl0_init(language_runtime_t *runtime, tree_t *root)
Definition: sema.c:153
@ ePl0TagTotal
Definition: sema.h:14
void pl0_forward_decls(language_runtime_t *runtime, compile_unit_t *context)
Definition: sema.c:466
void pl0_compile_module(language_runtime_t *runtime, compile_unit_t *context)
Definition: sema.c:565
an allocator object
Definition: arena.h:86
arena_t * arena
default memory arena
Definition: broker.h:266
a language driver support capabilities
Definition: broker.h:143
module_info_t info
common information about the language
Definition: broker.h:145
const char * id
unique id for the module
Definition: broker.h:101
Definition: ast.h:24
pl0_type_t type
Definition: ast.h:25
const vector_t * mod
Definition: ast.h:113
const vector_t * globals
Definition: ast.h:107
const vector_t * consts
Definition: ast.h:104
const node_t * node
Definition: ast.h:26
const vector_t * procs
Definition: ast.h:110
a source file scanner
Definition: scan.h:24
a generic vector of pointers
Definition: vector.c:16