comparison dmd/idgen.c @ 1:c53b6e3fe49a trunk

[svn r5] Initial commit. Most things are very rough.
author lindquist
date Sat, 01 Sep 2007 21:43:27 +0200
parents
children 788401029ecf
comparison
equal deleted inserted replaced
0:a9e71648e74d 1:c53b6e3fe49a
1
2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2006 by Digital Mars
4 // All Rights Reserved
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
10
11 // Program to generate string files in d data structures.
12 // Saves much tedious typing, and eliminates typo problems.
13 // Generates:
14 // id.h
15 // id.c
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdarg.h>
21 #include <assert.h>
22
23 struct Msgtable
24 {
25 char *ident; // name to use in DMD source
26 char *name; // name in D executable
27 };
28
29 Msgtable msgtable[] =
30 {
31 { "IUnknown" },
32 { "Object" },
33 { "object" },
34 { "max" },
35 { "min" },
36 { "This", "this" },
37 { "ctor", "_ctor" },
38 { "dtor", "_dtor" },
39 { "classInvariant", "__invariant" },
40 { "unitTest", "_unitTest" },
41 { "staticCtor", "_staticCtor" },
42 { "staticDtor", "_staticDtor" },
43 { "init" },
44 { "size" },
45 { "__sizeof", "sizeof" },
46 { "alignof" },
47 { "mangleof" },
48 { "stringof" },
49 { "tupleof" },
50 { "length" },
51 { "remove" },
52 { "ptr" },
53 { "funcptr" },
54 { "dollar", "__dollar" },
55 { "offset" },
56 { "offsetof" },
57 { "ModuleInfo" },
58 { "ClassInfo" },
59 { "classinfo" },
60 { "typeinfo" },
61 { "outer" },
62 { "Exception" },
63 { "withSym", "__withSym" },
64 { "result", "__result" },
65 { "returnLabel", "__returnLabel" },
66 { "delegate" },
67 { "line" },
68 { "empty", "" },
69 { "p" },
70 { "coverage", "__coverage" },
71
72 { "TypeInfo" },
73 { "TypeInfo_Class" },
74 { "TypeInfo_Interface" },
75 { "TypeInfo_Struct" },
76 { "TypeInfo_Enum" },
77 { "TypeInfo_Typedef" },
78 { "TypeInfo_Pointer" },
79 { "TypeInfo_Array" },
80 { "TypeInfo_StaticArray" },
81 { "TypeInfo_AssociativeArray" },
82 { "TypeInfo_Function" },
83 { "TypeInfo_Delegate" },
84 { "TypeInfo_Tuple" },
85 { "TypeInfo_Const" },
86 { "TypeInfo_Invariant" },
87 { "elements" },
88 { "_arguments_typeinfo" },
89 { "_arguments" },
90 { "_argptr" },
91 { "_match" },
92
93 { "LINE", "__LINE__" },
94 { "FILE", "__FILE__" },
95 { "DATE", "__DATE__" },
96 { "TIME", "__TIME__" },
97 { "TIMESTAMP", "__TIMESTAMP__" },
98 { "VENDOR", "__VENDOR__" },
99 { "VERSIONX", "__VERSION__" },
100
101 { "nan" },
102 { "infinity" },
103 { "dig" },
104 { "epsilon" },
105 { "mant_dig" },
106 { "max_10_exp" },
107 { "max_exp" },
108 { "min_10_exp" },
109 { "min_exp" },
110 { "re" },
111 { "im" },
112
113 { "C" },
114 { "D" },
115 { "Windows" },
116 { "Pascal" },
117 { "System" },
118
119 { "exit" },
120 { "success" },
121 { "failure" },
122
123 { "keys" },
124 { "values" },
125 { "rehash" },
126
127 { "sort" },
128 { "reverse" },
129 { "dup" },
130 { "idup" },
131
132 // For inline assembler
133 { "___out", "out" },
134 { "___in", "in" },
135 { "__int", "int" },
136 { "__dollar", "$" },
137 { "__LOCAL_SIZE" },
138
139 // For operator overloads
140 { "uadd", "opPos" },
141 { "neg", "opNeg" },
142 { "com", "opCom" },
143 { "add", "opAdd" },
144 { "add_r", "opAdd_r" },
145 { "sub", "opSub" },
146 { "sub_r", "opSub_r" },
147 { "mul", "opMul" },
148 { "mul_r", "opMul_r" },
149 { "div", "opDiv" },
150 { "div_r", "opDiv_r" },
151 { "mod", "opMod" },
152 { "mod_r", "opMod_r" },
153 { "eq", "opEquals" },
154 { "cmp", "opCmp" },
155 { "iand", "opAnd" },
156 { "iand_r", "opAnd_r" },
157 { "ior", "opOr" },
158 { "ior_r", "opOr_r" },
159 { "ixor", "opXor" },
160 { "ixor_r", "opXor_r" },
161 { "shl", "opShl" },
162 { "shl_r", "opShl_r" },
163 { "shr", "opShr" },
164 { "shr_r", "opShr_r" },
165 { "ushr", "opUShr" },
166 { "ushr_r", "opUShr_r" },
167 { "cat", "opCat" },
168 { "cat_r", "opCat_r" },
169 { "assign", "opAssign" },
170 { "addass", "opAddAssign" },
171 { "subass", "opSubAssign" },
172 { "mulass", "opMulAssign" },
173 { "divass", "opDivAssign" },
174 { "modass", "opModAssign" },
175 { "andass", "opAndAssign" },
176 { "orass", "opOrAssign" },
177 { "xorass", "opXorAssign" },
178 { "shlass", "opShlAssign" },
179 { "shrass", "opShrAssign" },
180 { "ushrass", "opUShrAssign" },
181 { "catass", "opCatAssign" },
182 { "postinc", "opPostInc" },
183 { "postdec", "opPostDec" },
184 { "index", "opIndex" },
185 { "indexass", "opIndexAssign" },
186 { "slice", "opSlice" },
187 { "sliceass", "opSliceAssign" },
188 { "call", "opCall" },
189 { "cast", "opCast" },
190 { "match", "opMatch" },
191 { "next", "opNext" },
192 { "opIn" },
193 { "opIn_r" },
194
195 { "classNew", "new" },
196 { "classDelete", "delete" },
197
198 // For foreach
199 { "apply", "opApply" },
200 { "applyReverse", "opApplyReverse" },
201
202 { "adDup", "_adDupT" },
203 { "adReverse", "_adReverse" },
204
205 // For internal functions
206 { "aaLen", "_aaLen" },
207 { "aaKeys", "_aaKeys" },
208 { "aaValues", "_aaValues" },
209 { "aaRehash", "_aaRehash" },
210
211 // For pragma's
212 { "lib" },
213 { "msg" },
214 { "GNU_asm" },
215 { "LLVM_intrinsic" },
216 { "LLVM_internal" },
217
218 // For toHash/toString
219 { "tohash", "toHash" },
220 { "tostring", "toString" },
221
222 // Special functions
223 { "alloca" },
224 { "main" },
225 { "WinMain" },
226 { "DllMain" },
227 };
228
229
230 int main()
231 {
232 FILE *fp;
233 unsigned i;
234
235 {
236 fp = fopen("id.h","w");
237 if (!fp)
238 { printf("can't open id.h\n");
239 exit(EXIT_FAILURE);
240 }
241
242 fprintf(fp, "// File generated by idgen.c\n");
243 #if __DMC__
244 fprintf(fp, "#pragma once\n");
245 #endif
246 fprintf(fp, "#ifndef DMD_ID_H\n");
247 fprintf(fp, "#define DMD_ID_H 1\n");
248 fprintf(fp, "struct Identifier;\n");
249 fprintf(fp, "struct Id\n");
250 fprintf(fp, "{\n");
251
252 for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
253 { char *id = msgtable[i].ident;
254
255 fprintf(fp," static Identifier *%s;\n", id);
256 }
257
258 fprintf(fp, " static void initialize();\n");
259 fprintf(fp, "};\n");
260 fprintf(fp, "#endif\n");
261
262 fclose(fp);
263 }
264
265 {
266 fp = fopen("id.c","w");
267 if (!fp)
268 { printf("can't open id.c\n");
269 exit(EXIT_FAILURE);
270 }
271
272 fprintf(fp, "// File generated by idgen.c\n");
273 fprintf(fp, "#include \"id.h\"\n");
274 fprintf(fp, "#include \"identifier.h\"\n");
275 fprintf(fp, "#include \"lexer.h\"\n");
276
277 for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
278 { char *id = msgtable[i].ident;
279 char *p = msgtable[i].name;
280
281 if (!p)
282 p = id;
283 fprintf(fp,"Identifier *Id::%s;\n", id);
284 }
285
286 fprintf(fp, "void Id::initialize()\n");
287 fprintf(fp, "{\n");
288
289 for (i = 0; i < sizeof(msgtable) / sizeof(msgtable[0]); i++)
290 { char *id = msgtable[i].ident;
291 char *p = msgtable[i].name;
292
293 if (!p)
294 p = id;
295 fprintf(fp," %s = Lexer::idPool(\"%s\");\n", id, p);
296 }
297
298 fprintf(fp, "}\n");
299
300 fclose(fp);
301 }
302
303 return EXIT_SUCCESS;
304 }