This repository has been archived by the owner on Dec 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
cgcmain.c
321 lines (303 loc) · 12 KB
/
cgcmain.c
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
316
317
318
319
320
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.
NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms. If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder.
THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.
IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
//
// cgcmain.c
//
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "slglobals.h"
// Profile registration functions:
int RegisterProfiles_generic(void);
static int (*RegistrationFunctions[])(void) = {
RegisterProfiles_generic,
};
int CommandLineArgs(int argc, char **argv, int pass);
int main(int argc, char **argv)
{
const char *copyright = "(c) 2001-2002 NVIDIA Corp.";
int numerrors, ii;
if (!InitCgStruct()) {
return 1;
}
if (!CommandLineArgs(argc, argv, 0))
return 1;
if (!InitAtomTable(atable, 0)) {
printf(OPENSL_TAG ": atom table initialization failed.\n");
return 1;
}
if (!InitScanner(Cg))
return 1;
for (ii = 0; ii < sizeof(RegistrationFunctions)/sizeof(RegistrationFunctions[0]); ii++)
RegistrationFunctions[ii]();
if (!CommandLineArgs(argc, argv, 1))
return 1;
if (!InitTokenStreams(Cg))
return 1;
if (!InitSymbolTable(Cg))
return 1;
if (!SetInputFile(Cg->options.sourceFileName))
return 1;
if (Cg->options.PrintVersion) {
printf(OPENSL_TAG ": version %d.%d.%04d%s, build date %s %s.\n",
HSL_VERSION, HSL_SUB_VERSION, HSL_SUB_SUB_VERSION, NDA_STRING, Build_Date, Build_Time);
}
#if defined(CGC_ENABLE_TOOLS)
if (Cg->options.Tokenize) {
TokenizeInput();
} else
#endif // defined(CGC_ENABLE_TOOLS)
{
if (!OpenListFile())
return 1;
if (!OpenOutputFile())
return 1;
PrintOptions(argc, argv);
if (!Cg->options.NoStdlib) {
if (!ReadFromTokenStream(&stdlib_cg_stream,
LookUpAddString(atable, "<stdlib>"),
StartGlobalScope))
{
return 1;
}
} else
StartGlobalScope(Cg);
#if defined(_DEBUG) || defined(__GNUC__)
yyparse();
#else
__try {
yyparse();
} __except(1) {
FatalError("*** exception during compilation ***");
}
#endif
CompileProgram(Cg, Cg->tokenLoc, CurrentScope);
numerrors = FreeScanner(Cg);
while (CurrentScope) {
if (Cg->options.DumpParseTree)
PrintScopeDeclarations();
PopScope();
}
if (Cg->options.DumpAtomTable)
PrintAtomTable(atable);
}
FreeSymbolTable(Cg);
FreeAtomTable(atable);
CloseOutputFiles("End of program");
return numerrors;
} // main
void PrintHelp()
{
slProfile *lProfile;
int ii;
printf("usage: cgc [-quiet] [-nocode] [-nostdlib] [-longprogs] [-v] [-Dmacro[=value]] \n [-profile id] [-entry id] [-o ofile] [file.cg]\n");
#if defined(CGC_DEBUG_THE_COMPILER)
printf(" [-atom] [-scan] [-tree] [-node] [-final] [-trap] [-comments] [-cdbg0]\n");
#endif
printf("supported profiles:\n");
ii = 0;
while ((lProfile = EnumerateProfiles(ii++)))
printf(" \"%s\"\n", GetAtomString(atable, lProfile->name));
} // PrintHelp
int CommandLineArgs(int argc, char **argv, int pass)
{
int ii;
for (ii = 1; ii < argc; ii++) {
if (argv[ii][0] == '-') {
if (!strcmp(argv[ii], "-debug")) {
if (pass == 0)
Cg->options.DebugMode = 1;
} else if (!strcmp(argv[ii], "-quiet") || !strcmp(argv[ii], "-q")) {
if (pass == 0)
Cg->options.Quiet = 1;
} else if (!strcmp(argv[ii], "-nocode")) {
if (pass == 0)
Cg->options.NoCodeGen = 1;
} else if (!strcmp(argv[ii], "-nowarn")) {
if (pass == 0)
Cg->options.NoWarnings = 1;
} else if (!strcmp(argv[ii], "-nostdlib")) {
if (pass == 0)
Cg->options.NoStdlib = 1;
} else if (!strcmp(argv[ii], "-error")) {
if (pass == 0)
Cg->options.ErrorMode = 1;
} else if (!strcmp(argv[ii], "-longprogs")) {
if (pass == 0)
Cg->options.AllowLongPrograms = 1;
} else if (!strcmp(argv[ii], "-posinv")) {
if (pass == 0)
Cg->options.PositionInvariant = 1;
} else if (!strcmp(argv[ii], "-v")) {
if (pass == 0)
Cg->options.PrintVersion = 1;
#if defined(CGC_ENABLE_TOOLS)
} else if (!strcmp(argv[ii], "-tokenize")) {
if (pass == 0)
Cg->options.Tokenize = 1;
#endif // defined(CGC_ENABLE_TOOLS)
#if defined(CGC_DEBUG_THE_COMPILER)
} else if (!strcmp(argv[ii], "-atom")) {
if (pass == 0)
Cg->options.DumpAtomTable = 1;
} else if (!strcmp(argv[ii], "-scan")) {
if (pass == 0)
Cg->options.TraceScanner = 1;
} else if (!strcmp(argv[ii], "-tree")) {
if (pass == 0)
Cg->options.DumpParseTree = 1;
} else if (!strcmp(argv[ii], "-node")) {
if (pass == 0)
Cg->options.DumpNodeTree = 1;
} else if (!strcmp(argv[ii], "-final")) {
if (pass == 0)
Cg->options.DumpFinalTree = 1;
} else if (!strcmp(argv[ii], "-trap")) {
if (pass == 0)
Cg->options.TrapOnError = 1;
} else if (!strcmp(argv[ii], "-comments")) {
if (pass == 0)
Cg->options.Comments = 1;
} else if (!strcmp(argv[ii], "-cdbg0")) {
if (pass == 0)
Cg->DebugLevel = 0;
} else if (!strcmp(argv[ii], "-cdbg1")) {
if (pass == 0)
Cg->DebugLevel = 1;
} else if (!strcmp(argv[ii], "-cdbg2")) {
if (pass == 0)
Cg->DebugLevel = 2;
} else if (!strcmp(argv[ii], "-cdbg3")) {
if (pass == 0)
Cg->DebugLevel = 3;
#endif // defined(CGC_DEBUG_THE_COMPILER)
} else if (!strcmp(argv[ii], "-o")) {
ii++;
if (pass == 0) {
if (ii < argc) {
Cg->options.outputFileName = argv[ii];
} else {
printf(OPENSL_TAG ": missing output file after \"-o\"\n");
return 0;
}
}
} else if (!strcmp(argv[ii], "-l")) {
ii++;
if (pass == 0) {
if (ii < argc) {
Cg->options.listFileName = argv[ii];
} else {
printf(OPENSL_TAG ": missing listing file after \"-l\"\n");
return 0;
}
}
} else if (!strcmp(argv[ii], "-help") || !strcmp(argv[ii], "-h")) {
if (pass == 1)
PrintHelp();
} else if (!strcmp(argv[ii], "-profile")) {
ii++;
if (pass == 0) {
if (ii < argc) {
if (Cg->options.profileString) {
printf(OPENSL_TAG ": multiple profiles\n");
return 0;
}
Cg->options.profileString = argv[ii];
} else {
printf(OPENSL_TAG ": missing profile name after \"-profile\"\n");
return 0;
}
}
} else if (!strcmp(argv[ii], "-entry")) {
ii++;
if (pass == 0) {
if (ii < argc) {
if (Cg->options.entryName) {
printf(OPENSL_TAG ": multiple entries\n");
return 0;
}
Cg->options.entryName = argv[ii];
} else {
printf(OPENSL_TAG ": missing entry name after \"-entry\"\n");
return 0;
}
}
} else if (argv[ii][1] == 'D') {
if (pass == 1) {
if (!PredefineMacro(argv[ii]+2)) {
printf(OPENSL_TAG ": bad argument: \"%s\"\n", argv[ii]);
return 0;
}
}
} else {
if (pass == 1) {
printf(OPENSL_TAG ": bad argument: \"%s\"\n", argv[ii]);
PrintHelp();
return 0;
}
}
} else {
if (pass == 1) {
if (Cg->options.sourceFileName) {
printf(OPENSL_TAG ": multiple input files: \"%s\" \"%s\" ...\n",
Cg->options.sourceFileName, argv[ii]);
return 0;
} else {
Cg->options.sourceFileName = argv[ii];
}
}
}
}
if (pass == 0) {
if (Cg->options.profileString == NULL) {
Cg->options.profileString = "generic";
}
if (Cg->options.entryName == NULL)
Cg->options.entryName = "main";
}
if (pass == 1) {
if (!InitHAL(Cg->options.profileString, Cg->options.entryName)) {
printf(OPENSL_TAG ": hal initialization failure.\n");
return 0;
}
}
return 1;
} // CommandLineArgs