summaryrefslogtreecommitdiff
path: root/source/luametatex/source/tex/texdumpdata.c
blob: 1500ec78b15d3f2ce57564ac104f4c0bb94a1952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
    See license.txt in the root of this project.
*/

# include "luametatex.h"

dump_state_info lmt_dump_state = {
    .fingerprint = luametatex_format_fingerprint,
    .padding     = 0
};

/*tex

    After \INITEX\ has seen a collection of fonts and macros, it can write all the necessary
    information on an auxiliary file so that production versions of \TEX\ are able to initialize
    their memory at high speed. The present section of the program takes care of such output and
    input. We shall consider simultaneously the processes of storing and restoring, so that the
    inverse relation between them is clear.

    The global variable |format_ident| is a string that is printed right after the |banner| line
    when \TEX\ is ready to start. For \INITEX\ this string says simply |(INITEX)|; for other
    versions of \TEX\ it says, for example, |(preloaded format = plain 1982.11.19)|, showing the
    year, month, and day that the format file was created. We have |format_ident = 0| before \TEX's
    tables are loaded. |FORMAT_ID| is a new field of type int suitable for the identification of a
    format: values between 0 and 256 (included) can not be used because in the previous format they
    are used for the length of the name of the engine.

    Because most used processors are little endian, we flush that way, but after that we just stick
    to the architecture. This also lets it come out as a readable 12 character (not nul terminated)
    string on a little endian machine. By using integers we can be sure that when it's generated on
    a different architecture the format is not seen as valid.

*/

/*

    In \LUAMETATEX\ the code has been overhauled. The sections are better separated and we write
    less to the file because we try to be sparse. Also, a more dynamic approach is used. In the
    \CONTEXT\ macro package most of what goes into the format is \LUA\ bytecode.

    We no longer hand endian related code here which saves swapping bytes on the most popular
    architectures. We also maintain some statistics and have several points where we check if
    we're still okay.

    Here we only have the main chunk. The specific data sections are implemented where it makes
    most sense.

*/

# define MAGIC_FORMAT_NUMBER_LE_1 0x58544D4C // 0x4C4D5458 // LMTX
# define MAGIC_FORMAT_NUMBER_LE_2 0x5845542D // 0x2D544558 // -TEX
# define MAGIC_FORMAT_NUMBER_LE_3 0x544D462D // 0x2D464D54 // -FMT

static int tex_aux_report_dump_state(dumpstream f, int pos, const char *what)
{
    int tmp = ftell(f);
    tex_print_format("%i %s", tmp - pos, what);
    fflush(stdout);
    return tmp;
}

/* todo: move more dumping to other files, then also the sizes. */

static void tex_aux_dump_fingerprint(dumpstream f)
{
    dump_via_int(f, MAGIC_FORMAT_NUMBER_LE_1);
    dump_via_int(f, MAGIC_FORMAT_NUMBER_LE_2);
    dump_via_int(f, MAGIC_FORMAT_NUMBER_LE_3);
    dump_via_int(f, luametatex_format_fingerprint);
}

static void tex_aux_undump_fingerprint(dumpstream f)
{
    int x;
    undump_int(f, x);
    if (x == MAGIC_FORMAT_NUMBER_LE_1) {
        undump_int(f, x);
        if (x == MAGIC_FORMAT_NUMBER_LE_2) {
            undump_int(f, x);
            if (x == MAGIC_FORMAT_NUMBER_LE_3) {
                undump_int(f, x);
                if (x == luametatex_format_fingerprint) {
                    return;
                } else {
                    tex_fatal_undump_error("version id");
                }
            }
        }
    }
    tex_fatal_undump_error("initial fingerprint");
}

static void tex_aux_dump_final_check(dumpstream f)
{
    dump_via_int(f, luametatex_format_fingerprint);
}

static void tex_aux_undump_final_check(dumpstream f)
{
    int x;
    undump_int(f, x);
    if (x == luametatex_format_fingerprint) {
        return;
    } else {
        tex_fatal_undump_error("final fingerprint");
    }
}

static void tex_aux_create_fmt_name(void)
{
    lmt_print_state.selector = new_string_selector_code;
//    lmt_dump_state.format_identifier = tex_make_string();
//    lmt_dump_state.format_name = tex_make_string();
    tex_print_format("%s %i.%i.%i %s",lmt_fileio_state.fmt_name, year_par, month_par, day_par, lmt_fileio_state.job_name);
    lmt_print_state.selector = terminal_and_logfile_selector_code;
}

static void tex_aux_dump_preamble(dumpstream f)
{
    dump_via_int(f, hash_size);
    dump_via_int(f, hash_prime);
    dump_via_int(f, prim_size);
    dump_via_int(f, prim_prime);
    dump_int(f, lmt_hash_state.hash_data.allocated);
    dump_int(f, lmt_hash_state.hash_data.ptr);
    dump_int(f, lmt_hash_state.hash_data.top);
}

static void tex_aux_undump_preamble(dumpstream f)
{
    int x;
    undump_int(f, x);
    if (x != hash_size) {
        goto BAD;
    }
    undump_int(f, x);
    if (x != hash_prime) {
        goto BAD;
    }
    undump_int(f, x);
    if (x != prim_size) {
        goto BAD;
    }
    undump_int(f, x);
    if (x != prim_prime) {
        goto BAD;
    }
    undump_int(f, lmt_hash_state.hash_data.allocated);
    undump_int(f, lmt_hash_state.hash_data.ptr);
    undump_int(f, lmt_hash_state.hash_data.top);
    /*tex
        We can consider moving all these allocaters to the start instead of this exception.
    */
    tex_initialize_hash_mem();
    return;
  BAD:
    tex_fatal_undump_error("preamble");
}

void tex_store_fmt_file(void)
{
    int pos = 0;
    dumpstream f = NULL;

    /*tex
        If dumping is not allowed, abort. The user is not allowed to dump a format file unless
        |save_ptr = 0|. This condition implies that |cur_level=level_one|, hence the |xeq_level|
        array is constant and it need not be dumped.
    */

    if (lmt_save_state.save_stack_data.ptr != 0) {
        tex_handle_error(
            succumb_error_type,
            "You can't dump inside a group",
            "'{...\\dump}' is a no-no."
        );
    }

    /*tex
        We don't store some things.
    */

    tex_dispose_specification_nodes();

    /*tex
        Create the |format_ident|, open the format file, and inform the user that dumping has begun.
    */

    {
        int callback_id = lmt_callback_defined(pre_dump_callback);
        if (callback_id > 0) {
            (void) lmt_run_callback(lmt_lua_state.lua_instance, callback_id, "->");
        }
    }

    /*tex
        We report the usual plus some more statistics. When something is wrong the machine just
        quits, hopefully with some meaningful error. We always create the format in normal log and
        terminal mode. We create a format name first because we also use that in error reporting.
    */

    tex_aux_create_fmt_name();

    f = tex_open_fmt_file(1);
    if (! f) {
        tex_formatted_error("system", "format file '%s' cannot be opened for writing", lmt_fileio_state.fmt_name);
        return;
    }

    tex_print_nlp();
    tex_print_format("Dumping format in file '%s': ", lmt_fileio_state.fmt_name);
    fflush(stdout);

    tex_compact_tokens();
    tex_compact_string_pool();

    tex_aux_dump_fingerprint(f); pos = tex_aux_report_dump_state(f, pos, "fingerprint + ");
    lmt_dump_engine_info(f);     pos = tex_aux_report_dump_state(f, pos, "engine + ");
    tex_aux_dump_preamble(f);    pos = tex_aux_report_dump_state(f, pos, "preamble + ");
    tex_dump_constants(f);       pos = tex_aux_report_dump_state(f, pos, "constants + ");
    tex_dump_string_pool(f);     pos = tex_aux_report_dump_state(f, pos, "stringpool + ");
    tex_dump_node_mem(f);        pos = tex_aux_report_dump_state(f, pos, "nodes + ");
    tex_dump_token_mem(f);       pos = tex_aux_report_dump_state(f, pos, "tokens + ");
    tex_dump_equivalents_mem(f); pos = tex_aux_report_dump_state(f, pos, "equivalents + ");
    tex_dump_math_codes(f);      pos = tex_aux_report_dump_state(f, pos, "math codes + ");
    tex_dump_text_codes(f);      pos = tex_aux_report_dump_state(f, pos, "text codes + ");
    tex_dump_primitives(f);      pos = tex_aux_report_dump_state(f, pos, "primitives + ");
    tex_dump_hashtable(f);       pos = tex_aux_report_dump_state(f, pos, "hashtable + ");
    tex_dump_font_data(f);       pos = tex_aux_report_dump_state(f, pos, "fonts + ");
    tex_dump_math_data(f);       pos = tex_aux_report_dump_state(f, pos, "math + ");
    tex_dump_language_data(f);   pos = tex_aux_report_dump_state(f, pos, "language + ");
    tex_dump_insert_data(f);     pos = tex_aux_report_dump_state(f, pos, "insert + ");
    lmt_dump_registers(f);       pos = tex_aux_report_dump_state(f, pos, "bytecodes + ");
    tex_aux_dump_final_check(f); pos = tex_aux_report_dump_state(f, pos, "housekeeping = ");

    tex_aux_report_dump_state(f, 0, "total.");
    tex_close_fmt_file(f);
    tex_print_ln();

}

/*tex

    Corresponding to the procedure that dumps a format file, we have a function that reads one in.
    The function returns |false| if the dumped format is incompatible with the present \TEX\ table
    sizes, etc.

    The inverse macros are slightly more complicated, since we need to check the range of the values
    we are reading in. We say |undump (a) (b) (x)| to read an integer value |x| that is supposed to
    be in the range |a <= x <= b|.

*/

int tex_fatal_undump_error(const char *s)
{
    tex_emergency_message("system", "fatal format error, loading file '%s' failed with bad '%s' data, remake the format", emergency_fmt_name, s);
    return tex_emergency_exit();
}

//define undumping(s) printf("undumping: %s\n",s); fflush(stdout);
# define undumping(s)

static void tex_aux_undump_fmt_data(dumpstream f)
{
    undumping("warmingup")

    undumping("fingerprint") tex_aux_undump_fingerprint(f);
    undumping("engineinfo")  lmt_undump_engine_info(f);
    undumping("preamble")    tex_aux_undump_preamble(f);
    undumping("constants")   tex_undump_constants(f);
    undumping("strings")     tex_undump_string_pool(f);
    undumping("nodes")       tex_undump_node_mem(f);
    undumping("tokens")      tex_undump_token_mem(f);
    undumping("equivalents") tex_undump_equivalents_mem(f);
    undumping("mathcodes")   tex_undump_math_codes(f);
    undumping("textcodes")   tex_undump_text_codes(f);
    undumping("primitives")  tex_undump_primitives(f);
    undumping("hashtable")   tex_undump_hashtable(f);
    undumping("fonts")       tex_undump_font_data(f);
    undumping("math")        tex_undump_math_data(f);
    undumping("languages")   tex_undump_language_data(f);
    undumping("inserts")     tex_undump_insert_data(f);
    undumping("bytecodes")   lmt_undump_registers(f);
    undumping("finalcheck")  tex_aux_undump_final_check(f);

    undumping("done")

    /*tex This should go elsewhere. */

    cur_list.prev_depth = ignore_depth_criterium_par;
}

/*
    The next code plays nice but on an error we exit anyway so some code is never reached in that
    case.
*/

int tex_load_fmt_file(void)
{
    dumpstream f = tex_open_fmt_file(0);
    if (f) {
        tex_aux_undump_fmt_data(f);
        tex_close_fmt_file(f);
        return 1;
    } else {
        return tex_fatal_undump_error("filehandle");
    }
}

void tex_initialize_dump_state(void)
{
    if (! lmt_engine_state.dump_name) {
        lmt_engine_state.dump_name = lmt_memory_strdup("initex");
    }
}