aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: a39bb8feb5142ce89a419e90d5f846de3edae2dc (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
/*
 * Copyright (c) 2015 Andrew Kelley
 *
 * This file is part of zig, which is MIT licensed.
 * See http://opensource.org/licenses/MIT
 */

#include "config.h"
#include "util.hpp"
#include "list.hpp"
#include "buffer.hpp"
#include "parser.hpp"
#include "tokenizer.hpp"
#include "error.hpp"
#include "codegen.hpp"
#include "analyze.hpp"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <inttypes.h>

static int usage(const char *arg0) {
    fprintf(stderr, "Usage: %s [command] [options] target\n"
        "Commands:\n"
        "  build                  create executable, object, or library from target\n"
        "  version                print version number and exit\n"
        "Optional Options:\n"
        "  --release              build with optimizations on and debug protection off\n"
        "  --static               output will be statically linked\n"
        "  --strip                exclude debug symbols\n"
        "  --export [exe|lib|obj] override output type\n"
        "  --name [name]          override output name\n"
        "  --output [file]        override destination path\n"
    , arg0);
    return EXIT_FAILURE;
}

static int version(void) {
    printf("%s\n", ZIG_VERSION_STRING);
    return EXIT_SUCCESS;
}

static Buf *fetch_file(FILE *f) {
    int fd = fileno(f);
    struct stat st;
    if (fstat(fd, &st))
        zig_panic("unable to stat file: %s", strerror(errno));
    off_t big_size = st.st_size;
    if (big_size > INT_MAX)
        zig_panic("file too big");
    int size = (int)big_size;

    Buf *buf = buf_alloc_fixed(size);
    size_t amt_read = fread(buf_ptr(buf), 1, buf_len(buf), f);
    if (amt_read != (size_t)buf_len(buf))
        zig_panic("error reading: %s", strerror(errno));

    return buf;
}

static int build(const char *arg0, const char *in_file, const char *out_file, bool release,
        bool strip, bool is_static, OutType out_type, char *out_name)
{
    static char cur_dir[1024];

    if (!in_file)
        return usage(arg0);

    FILE *in_f;
    if (strcmp(in_file, "-") == 0) {
        in_f = stdin;
        char *result = getcwd(cur_dir, sizeof(cur_dir));
        if (!result)
            zig_panic("unable to get current working directory: %s", strerror(errno));
    } else {
        in_f = fopen(in_file, "rb");
        if (!in_f)
            zig_panic("unable to open %s for reading: %s\n", in_file, strerror(errno));
    }

    fprintf(stderr, "Original source:\n");
    fprintf(stderr, "----------------\n");
    Buf *in_data = fetch_file(in_f);
    fprintf(stderr, "%s\n", buf_ptr(in_data));

    fprintf(stderr, "\nTokens:\n");
    fprintf(stderr, "---------\n");
    ZigList<Token> *tokens = tokenize(in_data);
    print_tokens(in_data, tokens);

    fprintf(stderr, "\nAST:\n");
    fprintf(stderr, "------\n");
    AstNode *root = ast_parse(in_data, tokens);
    assert(root);
    ast_print(root, 0);

    fprintf(stderr, "\nSemantic Analysis:\n");
    fprintf(stderr, "--------------------\n");
    CodeGen *codegen = create_codegen(root, buf_create_from_str(in_file));
    codegen_set_build_type(codegen, release ? CodeGenBuildTypeRelease : CodeGenBuildTypeDebug);
    codegen_set_strip(codegen, strip);
    codegen_set_is_static(codegen, is_static);
    if (out_type != OutTypeUnknown)
        codegen_set_out_type(codegen, out_type);
    if (out_name)
        codegen_set_out_name(codegen, buf_create_from_str(out_name));
    semantic_analyze(codegen);
    ZigList<ErrorMsg> *errors = codegen_error_messages(codegen);
    if (errors->length == 0) {
        fprintf(stderr, "OK\n");
    } else {
        for (int i = 0; i < errors->length; i += 1) {
            ErrorMsg *err = &errors->at(i);
            fprintf(stderr, "Error: Line %d, column %d: %s\n",
                    err->line_start + 1, err->column_start + 1,
                    buf_ptr(err->msg));
        }
        return 1;
    }

    fprintf(stderr, "\nCode Generation:\n");
    fprintf(stderr, "------------------\n");
    code_gen(codegen);

    if (release) {
        fprintf(stderr, "\nOptimization:\n");
        fprintf(stderr, "---------------\n");
        code_gen_optimize(codegen);
    }

    fprintf(stderr, "\nLink:\n");
    fprintf(stderr, "-------\n");
    code_gen_link(codegen, out_file);
    fprintf(stderr, "OK\n");

    return 0;
}

enum Cmd {
    CmdNone,
    CmdBuild,
    CmdVersion,
};

int main(int argc, char **argv) {
    char *arg0 = argv[0];
    char *in_file = NULL;
    char *out_file = NULL;
    bool release = false;
    bool strip = false;
    bool is_static = false;

    OutType out_type = OutTypeUnknown;
    char *out_name = NULL;

    Cmd cmd = CmdNone;
    for (int i = 1; i < argc; i += 1) {
        char *arg = argv[i];
        if (arg[0] == '-' && arg[1] == '-') {
            if (strcmp(arg, "--release") == 0) {
                release = true;
            } else if (strcmp(arg, "--strip") == 0) {
                strip = true;
            } else if (strcmp(arg, "--static") == 0) {
                is_static = true;
            } else if (i + 1 >= argc) {
                return usage(arg0);
            } else {
                i += 1;
                if (strcmp(arg, "--output") == 0) {
                    out_file = argv[i];
                } else if (strcmp(arg, "--export") == 0) {
                    if (strcmp(argv[i], "exe") == 0) {
                        out_type = OutTypeExe;
                    } else if (strcmp(argv[i], "lib") == 0) {
                        out_type = OutTypeLib;
                    } else if (strcmp(argv[i], "obj") == 0) {
                        out_type = OutTypeObj;
                    } else {
                        return usage(arg0);
                    }
                } else if (strcmp(arg, "--name") == 0) {
                    out_name = argv[i];
                } else {
                    return usage(arg0);
                }
            }
        } else if (cmd == CmdNone) {
            if (strcmp(arg, "build") == 0) {
                cmd = CmdBuild;
            } else if (strcmp(arg, "version") == 0) {
                cmd = CmdVersion;
            } else {
                fprintf(stderr, "Unrecognized command: %s\n", arg);
                return usage(arg0);
            }
        } else {
            switch (cmd) {
                case CmdNone:
                    zig_unreachable();
                case CmdBuild:
                    if (!in_file) {
                        in_file = arg;
                    } else {
                        return usage(arg0);
                    }
                    break;
                case CmdVersion:
                    return usage(arg0);
            }
        }
    }

    switch (cmd) {
        case CmdNone:
            return usage(arg0);
        case CmdBuild:
            return build(arg0, in_file, out_file, release, strip, is_static, out_type, out_name);
        case CmdVersion:
            return version();
    }

    zig_unreachable();
}