gen/data.h 2.5 KiB raw
1
#ifndef DATA_H
2
#define DATA_H
3
4
#include <stdio.h>
5
6
#include "../limits.h"
7
#include "../resolver.h"
8
#include "../types.h"
9
10
/* Data section offsets in memory */
11
#define DATA_RO_OFFSET 0x10000
12
#define DATA_RW_OFFSET 0xFFFFF0
13
14
/* String literal data in the data section */
15
typedef struct {
16
    const char *data;   /* String content */
17
    usize       length; /* String length */
18
    usize       offset; /* Offset in data section */
19
} string_data_t;
20
21
/* Kind of data item in the rw data section */
22
typedef enum {
23
    DATA_ARRAY, /* Array backing data for slices */
24
    DATA_CONST, /* Named constant */
25
} data_kind_t;
26
27
/* Unified data item for the rw data section */
28
typedef struct {
29
    data_kind_t      kind;
30
    usize            offset; /* Offset in data section */
31
    node_t          *node;
32
    struct parser_t *parser; /* Parser that owns this node's spans */
33
    union {
34
        struct {
35
            usize   length; /* Number of elements */
36
            type_t *elem;   /* Element type */
37
        } array;
38
        struct {
39
            const char *name;     /* Constant name */
40
            usize       name_len; /* Length of name */
41
        } constant;
42
    } as;
43
} data_item_t;
44
45
/* Data section for static data (strings, constants, etc.) */
46
typedef struct {
47
    string_data_t strings[MAX_STRING_LITERALS];
48
    usize         nstrings;
49
    data_item_t   items[MAX_CONSTANTS * 2]; /* Arrays and constants */
50
    usize         nitems;
51
    usize         ro_size;       /* Total size of read-only data section */
52
    usize         ro_offset;     /* Data section offset */
53
    usize         rw_offset;     /* Data section offset */
54
    usize         rw_init_total; /* Pre-computed total size */
55
    usize         rw_init_size;  /* Current init cursor */
56
    usize         rw_bss_size;   /* Current BSS cursor */
57
} data_section_t;
58
59
/* Initialize the data section management. */
60
void data_init(data_section_t *d);
61
/* Add a string literal to the data section.
62
 * Returns the data section offset where the string is stored. */
63
usize data_string(data_section_t *d, const char *str, usize len);
64
/* Add array data for a slice to the data section.
65
 * Returns the offset in the data section. */
66
usize data_array(data_section_t *d, struct parser_t *p, node_t *array_node);
67
/* Add a node to the data section. */
68
usize data_node(
69
    data_section_t  *d,
70
    struct parser_t *p,
71
    node_t          *node,
72
    const char      *name,
73
    usize            name_len
74
);
75
/* Emit the data section to the output. */
76
void data_emit_ro(data_section_t *d, FILE *out);
77
void data_emit_rw(data_section_t *d, FILE *out);
78
79
#endif