File: backend.c

package info (click to toggle)
icmake 6.30-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,360 kB
  • ctags: 1,415
  • sloc: ansic: 7,727; makefile: 1,465; sh: 244; asm: 126; cpp: 39
file content (72 lines) | stat: -rw-r--r-- 2,127 bytes parent folder | download | duplicates (4)
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
/*
                                B A C K E N D . C

        version
        offset of the string constant-area  (INT32)
        offset of the variable area         (INT32)
        offset of the strings area          (INT32)
        offset of the first instruction     (INT32)

        code                                (first byte is first instruction)
        ascii-z string constant area
        variables
        filenames
*/

#include "iccomp.h"

static INT8
    opexit = op_exit,
    opcall = op_call;

void backend()
{
    register unsigned
        index;
    BIN_HEADER_
        hdr;

    strcpy(string, "main");
    if ((index = looksym(&funtab)) == funtab.n_defined)
    {
        semantic("function 'main()' not defined");
        exit(1);
    }

    hdr.offset[3] = ftell(s_bin);           /* offset of first instruction */

    hidden_functions();                     /* patchup generated hidden */
                                            /* function calls */

    fseek(s_bin, 0, SEEK_END);              /* upwind to EOF again */

    outbin(&opcall, sizeof(INT8));          /* call main() at its offset */
    outbin(&funtab.symbol[index].var.vu.i->count, sizeof(INT16));

    outbin(&opexit, sizeof(INT8));          /* generate op_ret at the end */

    strcpy(hdr.version, version);           /* set the version */

    hdr.offset[0] = ftell(s_bin);           /* here the strings start */

                                            /* generate the strings */
    for (index = 0; index < n_strings; index++)
        fprintf(s_bin, "%s%c", stringtab[index].string, 0);

    hdr.offset[1] = ftell(s_bin);           /* here the vars start */

    for (index = 0; index < global.n_defined; index++)
    {
        global.symbol[index].var.type &= ~e_var; /* remove 'var' indicator */
        fwrite(&global.symbol[index].var, 1, sizeof(VAR_), s_bin);
    }

    hdr.offset[2] = ftell(s_bin);           /* here the filenames start */
    fputs(filenames, s_bin);

    rewind(s_bin);
                                            /* write the offset info */
    fwrite(&hdr, sizeof(BIN_HEADER_), 1, s_bin);


}