aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 5f224fe36377ed8142cb37242e271a53f9df45ad (plain) (blame)
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
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include "cibic.tab.h"
#include "ast.h"
#include "semantics.h"
#include "ssa.h"
#include "mips.h"

extern char linebuff[];
extern char *lptr;
extern int yyparse();
extern FILE *yyin;
char *fname;

int yywrap() {
    return 1;
}

void print_error(char *err_msg, char *lb, int row, int col, int warn) {
    *lptr = '\0';
    fprintf(stderr, "%d:%d: %s: %s\n",
            row, col, warn ? "warning" : "error", err_msg);
    if (lb)
    {
        fprintf(stderr, "%s\n", lb);
        while (--col) fprintf(stderr, "%c", ' ');
        fprintf(stderr, "^\n");
    }
    if (!warn) exit(1);
}

int yyerror(char *err_msg) {
    print_error(err_msg, linebuff,
            yylloc.first_line, yylloc.first_column, 0);
    return 0;
}

void print_ast() {
    if (fname)
        fprintf(stderr, "AST for file: \"%s\"\n", fname);
    else
        fprintf(stderr, "AST for stdin\n");
    cibic_init();
    yyparse();
    cnode_debug_print(ast_root, 1);
}

void print_sem() {
    cibic_init();
    yyparse();
    semantics_check(ast_root, 0);
}

void lib_generate() {
    FILE *f = fopen("lib.s", "r");     
    static char buff[1024];
    if (f)
    {
        size_t size;
        while ((size = fread(buff, 1, 1024, f)))
            fwrite(buff, 1, size, stdout);
    }
}

void print_ssa() {
    cibic_init();
    yyparse();
    semantics_check(ast_root, 1);
    ssa_generate(0);
}

void compile() {
    cibic_init();
    yyparse();
    semantics_check(ast_root, 1);
    ssa_generate(1);
    mips_generate();
    lib_generate();
}

void print_help() {
    fprintf(stderr,
            "CIBIC: C Implemented Bare and Ingenuous Compiler\n\n"
	        "Copyright (C) 2014 Ted Yin\n"
	        "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
	        "There is NO WARRANTY, to the extent permitted by law.\n"
	        "Usage: [options] filename\n"
            "Options:\n" 
            "\t --ast \t\t Print AST Construction\n"
            "\t --sem \t\t Print Semantic Information\n"
            "\t --ssa \t\t Print Single Static Assignment\n"
            "\t --help \t Show this info\n"
        );
}

static struct option lopts[] = {
    { "ast", no_argument, 0, 'a'},
    { "sem", no_argument, 0, 'm'},
    { "ssa", no_argument, 0, 's'},
    { "help", no_argument, 0, 'h'},
    {0, 0, 0, 0}
};

enum {
    PRINT_AST,
    PRINT_HELP,
    PRINT_SEM,
    PRINT_SSA,
    COMPILE
} mode = COMPILE;

int main(int argc, char **argv) {
    int option_index = 0;
    while (1)
    {
        int c = getopt_long(argc, argv, "ah", lopts, &option_index);
        if (c == -1)
            break;
        switch (c)
        {
            case 0: break;
            case 'a': mode = PRINT_AST; break;
            case 's': mode = PRINT_SSA; break;
            case 'm': mode = PRINT_SEM; break;
            case 'h': print_help(); return 0;
            default: print_help(); return 0;
        }
    }
    if (optind == argc - 1)
    {
        fname = argv[argc - 1];
        yyin = fopen(fname, "r");
        if (!yyin)
        {
            fprintf(stderr, "Error while opening file.\n");
            return 1;
        }
    }
    else if (optind == argc)
    {
        fname = NULL;
        yyin = stdin;
    }
    else
    {
        fprintf(stderr, "Only one source file is exepected.\n");
        return 1;
    }
    switch (mode)
    {
        case PRINT_AST: print_ast(); break;
        case PRINT_SEM: print_sem(); break;
        case PRINT_SSA: print_ssa(); break;
        case PRINT_HELP: print_help(); break;
        case COMPILE: compile(); break;
    }
    return 0;
}