Debian Codegraph @

terps/tads/tads3/test/test_comp_obj.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/test/test_comp_obj.cpp,v 1.1 1999/07/11 00:47:03 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 test_comp_obj.cpp - parser test: compile to object file
15 Function
16
17 Notes
18
19 Modified
20 05/01/99 MJRoberts - Creation
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include "os.h"
27 #include "t3std.h"
28 #include "tctok.h"
29 #include "resload.h"
30 #include "tcmain.h"
31 #include "tchostsi.h"
32 #include "tcglob.h"
33 #include "tcprs.h"
34 #include "tctarg.h"
35 #include "vmfile.h"
36 #include "tcmake.h"
37 #include "vmimage.h"
38 #include "vmrunsym.h"
39 #include "vmmeta.h"
40 #include "t3test.h"
41
42
43 static void errexit(const char *msg)
44 {
45 printf("%s\n", msg);
46 exit(1);
47 }
48
49 int main(int argc, char **argv)
50 {
51 CResLoader *res_loader;
52 CTcHostIfc *hostifc;
53 int curarg;
54 int fatal_error_count = 0;
55 osfildef *fpout = 0;
56 CVmFile *objfile = 0;
57 CTPNStmProg *node;
58 const char *obj_fname;
59 int success;
60 char pathbuf[OSFNMAX];
61
62 /* initialize for testing */
63 test_init();
64
65 /* create the host interface object */
66 hostifc = new CTcHostIfcStdio();
67
68 /* create a resource loader */
69 os_get_special_path(pathbuf, sizeof(pathbuf), argv[0], OS_GSP_T3_RES);
70 res_loader = new CResLoader(pathbuf);
71
72 /* initialize the compiler */
73 CTcMain::init(hostifc, res_loader, 0);
74
75 /* keep all fixups when writing an object file */
76 G_keep_objfixups = TRUE;
77 G_keep_propfixups = TRUE;
78 G_keep_enumfixups = TRUE;
79
80 err_try
81 {
82 /* scan -I arguments */
83 for (curarg = 1 ; curarg < argc ; ++curarg)
84 {
85 char *p;
86
87 /* get the argument string for easy reference */
88 p = argv[curarg];
89
90 /* if it's not an option, we're done */
91 if (*p != '-')
92 break;
93
94 /* if it's a -I argument, use it */
95 if (*(p + 1) == 'I')
96 {
97 char *path;
98
99 /*
100 * if it's with this argument, read it, otherwise move
101 * on to the next argument
102 */
103 if (*(p + 2) == '\0')
104 path = argv[++curarg];
105 else
106 path = p + 2;
107
108 /* add the directory to the include path list */
109 G_tok->add_inc_path(path);
110 }
111 else if (*(p + 1) == 'v')
112 {
113 /* set verbose mode */
114 G_tcmain->set_verbosity(TRUE);
115 }
116 else if (*(p + 1) == 's')
117 {
118 char *fname;
119 osfildef *symfp;
120 CVmFile *symfile;
121
122 /* load a symbol file */
123 if (*(p + 2) == '\0')
124 fname = argv[++curarg];
125 else
126 fname = p + 2;
127
128 /* open the file */
129 symfp = osfoprb(fname, OSFTT3SYM);
130 if (symfp == 0)
131 {
132 fprintf(stderr, "unable to open symbol file %s\n", fname);
133 continue;
134 }
135
136 /* load the symbol file */
137 symfile = new CVmFile();
138 symfile->set_file(symfp, 0);
139 G_prs->read_symbol_file(symfile);
140
141 /* done with the symbol file */
142 delete symfile;
143 }
144 else
145 {
146 /*
147 * invalid usage - consume all the arguments and fall
148 * through to the usage checker
149 */
150 curarg = argc;
151 break;
152 }
153 }
154
155 /* check arguments */
156 if (curarg + 2 != argc)
157 {
158 /* terminate the compiler */
159 CTcMain::terminate();
160
161 /* delete our objects */
162 delete res_loader;
163
164 /* exit with an error */
165 errexit("usage: test_comp_obj [options] <source-file> "
166 "<object-file>\n"
167 "options:\n"
168 " -sfile - load symbols from file\n"
169 " -Idir - add dir to include path\n"
170 " -v - verbose error messages");
171 }
172
173 /* set up the tokenizer with the main input file */
174 if (G_tok->set_source(argv[curarg], argv[curarg]))
175 errexit("unable to open source file");
176
177 /* set up an output file */
178 obj_fname = argv[curarg+1];
179 fpout = osfopwb(obj_fname, OSFTT3OBJ);
180 if (fpout == 0)
181 errexit("unable to open object file");
182 objfile = new CVmFile();
183 objfile->set_file(fpout, 0);
184
185 /* read the first token */
186 G_tok->next();
187
188 /* parse at the top level */
189 node = G_prs->parse_top();
190
191 /* if errors occurred during parsing, stop here */
192 if (G_tcmain->get_error_count() != 0 || node == 0)
193 goto done;
194
195 /* fold symbolic constants for all nodes */
196 node->fold_constants(G_prs->get_global_symtab());
197
198 /* if errors occurred during constant folding, stop now */
199 if (G_tcmain->get_error_count() != 0)
200 goto done;
201
202 /* generate code and write the object file */
203 node->build_object_file(objfile, 0);
204
205 done: ;
206 }
207 err_catch(exc)
208 {
209 /*
210 * if it's not a general internal or fatal error, log it; don't
211 * log general errors, since these will have been logged as
212 * specific internal errors before being thrown
213 */
214 if (exc->get_error_code() != TCERR_INTERNAL_ERROR
215 && exc->get_error_code() != TCERR_FATAL_ERROR)
216 G_tok->log_error(TC_SEV_FATAL, exc->get_error_code());
217
218 /* count the fatal error */
219 ++fatal_error_count;
220 }
221 err_end;
222
223 /* report errors */
224 fprintf(stderr,
225 "Warnings: %d\n"
226 "Errors: %d\n"
227 "Longest string: %lu, longest list: %lu\n",
228 G_tcmain->get_warning_count(),
229 G_tcmain->get_error_count() + fatal_error_count,
230 (unsigned long)G_cg->get_max_str_len(),
231 (unsigned long)G_cg->get_max_list_cnt());
232
233 /*
234 * note whether or not the compilation was successful - it succeeded
235 * if we had no errors or fatal errors
236 */
237 success = (G_tcmain->get_error_count() + fatal_error_count == 0);
238
239 /* delete the object file object (this closes the file) */
240 delete objfile;
241
242 /*
243 * if any errors occurred, delete the object file in the external
244 * file system - this prevents us from leaving around an incomplete
245 * or corrupted image file when compilation fails, and helps
246 * 'make'-type tools realize that they must generate the image file
247 * target again on the next build, even if source files didn't
248 * change
249 */
250 if (!success)
251 osfdel(obj_fname);
252
253 /* shut down the compiler */
254 CTcMain::terminate();
255
256 /* done with the res loader */
257 delete res_loader;
258
259 /* delete the host interface */
260 delete hostifc;
261
262 /* show any unfreed memory */
263 t3_list_memory_blocks(0);
264
265 /*
266 * terminate - exit with a success indication if we had no errors
267 * (other than warnings); exit with an error indication otherwise
268 */
269 return (success ? OSEXSUCC : OSEXFAIL);
270 }
271
272 /*
273 * dummy 'make' object implementation
274 */
275 void CTcMake::write_build_config_to_sym_file(class CVmFile *)
276 {
277 }
278
279 /* ------------------------------------------------------------------------ */
280 /*
281 * dummy implementation of runtime symbol table
282 */
283 void CVmRuntimeSymbols::add_sym(const char *, size_t,
284 const vm_val_t *)
285 {
286 }
287
288 /* dummy implementation of runtime metaclass table */
289 vm_meta_entry_t *CVmMetaTable::get_entry_by_id(const char *id) const
290 {
291 return 0;
292 }