Added package with the documentation and the examples
[lwc.git] / main.c
blob280cb8552f96edad9d84d10392a0b93b3cc09023
1 #include <ctype.h>
2 #include "global.h"
3 #include "SYS.h"
5 int zinit [2];
7 bool MainModule = 0;
8 bool HadErrors = 0;
9 bool StructByRef = 1;
10 bool InlineAllVt = 0;
11 bool ConstVtables = 0;
12 bool VIDeclarations = 0;
13 bool Reentrant = 0;
14 bool ExpandAllAutos = 0;
15 bool Streams_Closed = false;
16 bool ExceptionsUsed = 1;
17 bool GlobInitUsed = 0;
18 bool vtptrConst = 1;
19 bool NoLinkonce = 0;
20 bool OneBigFile = 0;
21 bool HaveAliases = 0; /* not well removed by gcc and produce bigger binaries */
22 bool StdcallMembers = 0;
23 bool ExportVtbl = 1;
24 bool EHUnwind = EHDEFAULT;
25 bool GoodOldC = false;
27 char *current_file;
29 void init ()
31 Token t;
32 int p [6] = { -1, };
33 Token nodcl [] = { -1 };
34 Token xargs [] = { RESERVED_x, -1 };
36 // null invalid structure && type
38 enter_struct (NOOBJ, 1, 0, 0, 0, 0);
39 enter_type (p);
41 // common types
43 p[0] = B_PURE; p[1] = -1;
44 typeID_NOTYPE = enter_type (p);
45 p[0] = B_SINT; p[1] = -1;
46 typeID_int = enter_type (p);
47 p[0] = B_UINT;
48 typeID_uint = enter_type (p);
49 p[0] = B_FLOAT;
50 typeID_float = enter_type (p);
51 p[0] = B_VOID;
52 typeID_void = enter_type (p);
53 sintprintf (p, B_SCHAR, '*', -1);
54 typeID_charP = enter_type (p);
55 p[0] = B_VOID;
56 typeID_voidP = enter_type (p);
57 p[0] = B_SINT;
58 typeID_intP = enter_type (p);
59 sintprintf (p, B_SCHAR, '(', typeID_int, INTERNAL_ARGEND, '*', -1);
60 typeID_ebn_f = enter_type (p);
62 // special objects
64 enter_global_object (RESERVED___FUNCTION__, typeID_charP);
65 enter_global_object (RESERVED___PRETTY_FUNCTION__, typeID_charP);
66 if ((t = Lookup_Symbol ("__builtin_va_list")))
67 enter_typedef (t, typeID_voidP);
69 if ((t = Lookup_Symbol ("__lwcbuiltin_get_estack"))) {
70 sintprintf (p, typeID_voidP, '(', INTERNAL_ARGEND, -1);
71 xdeclare_function (&Global, t, t, enter_type (p), nodcl, xargs, FUNCP_NOTHROW, 0,0);
73 if ((t = Lookup_Symbol ("__lwcbuiltin_set_estack"))) {
74 sintprintf (p, B_VOID, '(', typeID_voidP, INTERNAL_ARGEND, -1);
75 xdeclare_function (&Global, t, t, enter_type (p), nodcl, xargs, FUNCP_NOTHROW, 0,0);
78 if ((t = Lookup_Symbol ("__builtin_expect"))) {
79 sintprintf (p, B_SINT, '(', typeID_int, typeID_int, INTERNAL_ARGEND, -1);
80 xdeclare_function (&Global, t, t, enter_type (p), nodcl, xargs, FUNCP_NOTHROW, 0,0);
82 if ((t = Lookup_Symbol ("__builtin_stdarg_start"))) {
83 sintprintf (p, B_VOID, '(', B_ELLIPSIS, INTERNAL_ARGEND, '*', -1);
84 xdeclare_function (&Global, t, t, enter_type (p), nodcl, xargs, 0, 0, 0);
86 if ((t = Lookup_Symbol ("__builtin_va_end"))) {
87 sintprintf (p, B_VOID, '(', B_ELLIPSIS, INTERNAL_ARGEND, '*', -1);
88 xdeclare_function (&Global, t, t, enter_type (p), nodcl, xargs, 0, 0, 0);
91 if (t = Lookup_Symbol ("__builtin_popcount")) {
92 sintprintf (p, B_SINT, '(', typeID_int, INTERNAL_ARGEND, -1);
93 xdeclare_function (&Global, t, t, enter_type (p), nodcl, xargs, FUNCP_NOTHROW, 0,0);
95 if (t = Lookup_Symbol ("__builtin_constant_p")) {
96 sintprintf (p, B_SINT, '(', B_ELLIPSIS, INTERNAL_ARGEND, -1);
97 xdeclare_function (&Global, t, t, enter_type (p), nodcl, xargs, FUNCP_NOTHROW, 0,0);
99 sintprintf (p, B_VOID, '(', typeID_uint, INTERNAL_ARGEND, '*', -1);
100 xdeclare_function (&Global, INTERN_alloca, INTERN_alloca,
101 enter_type (p), nodcl, xargs, FUNCP_NOTHROW, 0, 0);
102 //#ifdef __GNUC__
103 // xdeclare_function (&Global, RESERVED_alloca, RESERVED_alloca,
104 // enter_type (p), nodcl, xargs, FUNCP_NOTHROW, 0, 0);
105 //#endif
107 name_derrive_union = new_symbol (DERRIVE_UNION);
109 zinit [0] = RESERVED_0, zinit [1] = -1;
111 GLOBAL_INIT_FUNC = new_stream ();
112 Token GIF = new_symbol (GLOBINITF);
113 outprintf (GLOBAL_INIT_FUNC, RESERVED_static, RESERVED_void, GIF, '(', ')',
114 RESERVED___attribute__, '(', '(', RESERVED_constructor, ')', ')', ';',
115 RESERVED_static, RESERVED_void, GIF, '(', ')', '{', -1);
118 void finish ()
120 if (GlobInitUsed) {
121 output_itoken (GLOBAL_INIT_FUNC, '}');
122 #ifdef COMMENT_OUTPUT
123 output_itoken (GLOBAL, new_symbol (COMMENT_SECTION_OBJCTOR));
124 #endif
125 concate_streams (GLOBAL, GLOBAL_INIT_FUNC);
127 if (HadErrors)
128 output_itoken (GLOBAL, new_symbol ("\n#error there were errors in lwc expressions\n"));
131 static const char stdc [] =
132 "#define __STDC__ 1\n"
133 "#define __STDC_VERSION__ 199901L\n"
136 static const char stddef [] =
137 #ifdef __GNUC__
138 // hairy gcc crap...
139 // * * * STILL DOESN'T WORK. shitty Glibc crap
140 "#define NULL ((void*)0)\n"
141 "typedef __SIZE_TYPE__ size_t;\n"
142 "typedef __PTRDIFF_TYPE__ ptrdiff_t;\n"
143 "typedef __WCHAR_TYPE__ wchar_t;\n"
144 "typedef __WINT_TYPE__ wint_t;\n"
145 "void exit (int);\n"
146 #endif
147 "extern void * malloc (unsigned int);\n"
148 #if INTER_alloca == RESERVED_alloca
149 "extern void * alloca (unsigned int);\n"
150 #endif
151 "extern void free (void*);\n"
154 static const char stdlwc [] =
155 ";\n";
157 #ifndef DEBUG
158 #define PROGRESS(x)
159 #else
160 #define PROGRESS(x) if (debugflag.PROGRESS) fputs (COLS x COLE"...\n", stderr);
161 #endif
163 int main (int argc, char **argv)
165 srand (time (NULL) ^ getpid () ^ (int) argv ^ (int) &argc ^ (int) main);
166 #ifdef DEBUG
167 enable_debugs ();
168 #endif
170 PROGRESS ("preprocessing");
171 preproc (argc - 1, argv + 1);
172 init_processors ();
173 initlex ();
174 if (!sys_cpp) {
175 yydo_mem ((char*)sys_defs, sizeof sys_defs - 1);
176 yydo_mem ((char*)stdc, sizeof stdc - 1);
178 #ifdef DO_CPP
179 yydo_mem ((char*)stddef, sizeof stddef - 1);
180 #endif
181 yydo_mem ((char*)stdlwc, sizeof stdlwc - 1);
182 if (yydo (main_file)) {
183 fprintf (stderr, "No such file or directory\n");
184 return 1;
187 #ifdef DO_CPP
188 cleanup_cpp ();
189 #endif
190 init ();
191 init_except ();
192 do_templates ();
193 bogus1 ();
195 #ifdef DEBUG
196 if (debugflag.SHOWPROG) {
197 int i;
198 for (i=0; CODE [i] != -1; i++)
199 PRINTF ("%i %s %c", i, expand (CODE [i]),i%6 ? '\t':'\n');
200 PRINTF ("\n");
202 if (debugflag.SHOWCPP) {
203 FILE *F = fopen (PREPROCOUT, "w");
204 int i;
205 for (i=0; CODE [i] != -1; i++)
206 fprintf (F, "%s ", expand (CODE [i]));
207 fclose (F);
209 #endif
211 FPROTOS = new_stream ();
212 GLOBAL = new_stream ();
213 INTERNAL_CODE = new_stream ();
214 VIRTUALTABLES = new_stream ();
215 VTABLE_DECLARATIONS = new_stream ();
216 STRUCTS = new_stream ();
217 GVARS = new_stream ();
218 INCLUDE = new_stream ();
219 REGEXP_CODE = new_stream ();
220 AUTOFUNCTIONS = new_stream ();
221 FUNCDEFCODE = new_stream ();
223 output_itoken (INCLUDE, new_symbol (strdup ("/* lightweight c++ "LWC_VERSION" */\n")));
224 #ifdef COMMENT_OUTPUT
225 output_itoken (INCLUDE, new_symbol (COMMENT_SECTION_INCLUDE));
226 output_itoken (FPROTOS, new_symbol (COMMENT_SECTION_PROTOTYPES));
227 output_itoken (GLOBAL, new_symbol (COMMENT_SECTION_GLOBAL));
228 output_itoken (INTERNAL_CODE, new_symbol (COMMENT_SECTION_INNERCODE));
229 output_itoken (VIRTUALTABLES, new_symbol (COMMENT_SECTION_VIRTUALTABLES));
230 output_itoken (VTABLE_DECLARATIONS, new_symbol (COMMENT_SECTION_VTDEF));
231 output_itoken (STRUCTS, new_symbol (COMMENT_SECTION_STRUCTS));
232 output_itoken (GVARS, new_symbol (COMMENT_SECTION_GVARS));
233 output_itoken (REGEXP_CODE, new_symbol (COMMENT_SECTION_REGEXP));
234 output_itoken (AUTOFUNCTIONS, new_symbol (COMMENT_SECTION_AUTOFUNCTIONS));
235 output_itoken (FUNCDEFCODE, new_symbol (COMMENT_SECTION_FUNCTIONS));
236 #endif
239 PROGRESS ("parsing translation unit")
240 translation_unit ();
242 PROGRESS ("compiling functions");
243 do do_functions ();
244 while (specialize_abstracts ());
246 PROGRESS ("auto functions")
247 define_auto_functions ();
249 PROGRESS ("making data structures");
250 export_structs ();
251 if (ExceptionsUsed)
252 decl_except_data ();
253 export_virtual_table_instances ();
255 #ifdef HAVE_LINKONCE
256 export_virtual_definitions ();
257 #else
258 if (MainModule) export_virtual_definitions ();
259 else export_virtual_static_definitions ();
260 #endif
262 if (Global) export_fspace (Global);
263 outprintf (FPROTOS, RESERVED_void, '*', RESERVED_malloc, '(', RESERVED_unsigned,
264 RESERVED_int, ')', ';', RESERVED_void, RESERVED_free, '(', ')', ';', -1);
266 PROGRESS ("expanding output");
267 concate_streams (INCLUDE, GLOBAL);
268 GLOBAL = INCLUDE;
269 concate_streams (GLOBAL, STRUCTS);
270 concate_streams (GLOBAL, VTABLE_DECLARATIONS);
271 concate_streams (GLOBAL, FPROTOS);
272 concate_streams (GLOBAL, GVARS);
273 concate_streams (GLOBAL, REGEXP_CODE);
274 concate_streams (GLOBAL, INTERNAL_CODE);
275 concate_streams (GLOBAL, AUTOFUNCTIONS);
276 concate_streams (GLOBAL, FUNCDEFCODE);
277 concate_streams (GLOBAL, VIRTUALTABLES);
278 finish ();
280 #ifdef DEBUG
281 if (!debugflag.NOSTDOUT)
282 #endif
283 export_output (GLOBAL);
285 return HadErrors;