60
|
1 #define EXTERN /**/
|
|
2
|
607
|
3 /************************************************************************
|
|
4 ** Copyright (C) 2006 Shinji Kono
|
|
5 ** 連絡先: 琉球大学情報工学科 河野 真治
|
|
6 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
|
|
7 **
|
|
8 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
|
|
9 ** その際には、誰が貢献したを示すこの部分を残すこと。
|
|
10 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
|
|
11 ** 営利利用も上記に反しない範囲で許可します。
|
|
12 ** バイナリの配布の際にはversion messageを保存することを条件とします。
|
|
13 ** このプログラムについては特に何の保証もしない、悪しからず。
|
|
14 **
|
|
15 ** Everyone is permitted to do anything on this program
|
|
16 ** including copying, modifying, improving,
|
|
17 ** as long as you don't try to pretend that you wrote it.
|
|
18 ** i.e., the above copyright notice has to appear in all copies.
|
|
19 ** Binary distribution requires original version messages.
|
|
20 ** You don't have to ask before copying, redistribution or publishing.
|
|
21 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
|
|
22 ***********************************************************************/
|
327
|
23 #include <stdio.h>
|
60
|
24 #include "mc.h"
|
327
|
25 #include "mc-parse.h"
|
597
|
26 #include "mc-codegen.h"
|
60
|
27
|
327
|
28 #include "conv/conv.h"
|
66
|
29 #include "conv/convdef.h"
|
82
|
30 #include "conv/c.h"
|
60
|
31
|
68
|
32 extern void type_print(int type,NMTBL *nptr,FILE *out);
|
74
|
33 extern void type_print1(int type,NMTBL *nptr,FILE *out,int cont);
|
68
|
34 extern void sym_print(int type,FILE *out);
|
|
35
|
69
|
36 static FILE *vout,*svout;
|
|
37 static FILE *null;
|
60
|
38
|
|
39 static void
|
609
|
40 expr_(int c)
|
|
41 {
|
|
42 }
|
|
43
|
|
44 static void
|
651
|
45 bool_(int c)
|
|
46 {
|
|
47 }
|
|
48
|
|
49 static void
|
67
|
50 comment_(int c)
|
66
|
51 {
|
67
|
52 fprintf(vout,"%c",c);
|
66
|
53 }
|
|
54
|
|
55 static void
|
|
56 open_(char *s)
|
65
|
57 {
|
346
|
58 char *p;
|
564
|
59 p = make_filename_with_extension(s,"cc");
|
77
|
60 #if 1
|
65
|
61 vout = fopen(p,"w");
|
69
|
62 #else
|
|
63 vout = stderr;p=p;
|
|
64 #endif
|
|
65 null=fopen("/dev/null","w");
|
66
|
66 if (!vout) error(-1);
|
65
|
67 }
|
|
68
|
|
69 static void
|
69
|
70 conv_()
|
|
71 {
|
|
72 vout=svout;
|
|
73 }
|
|
74
|
|
75 static void
|
|
76 noconv_(int c)
|
|
77 {
|
|
78 svout=vout;
|
|
79 vout=c?null:vout;
|
|
80 }
|
|
81
|
|
82 static void
|
66
|
83 print_(char *s)
|
60
|
84 {
|
|
85 fprintf(vout,"c: %s\n",s);
|
|
86 }
|
65
|
87
|
|
88 static void
|
66
|
89 close_()
|
65
|
90 {
|
|
91 fclose(vout);
|
|
92 }
|
66
|
93
|
|
94 void
|
67
|
95 case_begin_(int cases,int def){
|
|
96 fprintf(vout,"case ");
|
|
97 }
|
|
98
|
|
99 void
|
66
|
100 case_(int cases,int def){
|
67
|
101 if (def)
|
|
102 fprintf(vout,"default:");
|
|
103 else
|
69
|
104 fprintf(vout,"%d:",car(cases));
|
66
|
105 }
|
|
106
|
|
107 void
|
79
|
108 code_(NMTBL *n){
|
|
109 int args;
|
|
110 args=n->dsp;
|
68
|
111 fprintf(vout,"code %s(",n->nm);
|
|
112 while(args) {
|
79
|
113 n=(NMTBL *)caddr(args);
|
68
|
114 type_print(n->ty,n,vout);
|
|
115 args=cadr(args);
|
|
116 if(args) fprintf(vout,",");
|
|
117 }
|
597
|
118 fprintf(vout,")");
|
66
|
119 }
|
|
120
|
|
121 void
|
|
122 code_end_(){
|
76
|
123 fprintf(vout,"}");
|
66
|
124 }
|
|
125
|
|
126 void
|
|
127 comma_(){
|
67
|
128 fprintf(vout,",");
|
66
|
129 }
|
|
130
|
|
131 void
|
|
132 cond_(){
|
|
133
|
|
134 }
|
|
135
|
|
136 void
|
|
137 cond1_(){
|
69
|
138 fprintf(vout,"?");
|
66
|
139 }
|
|
140
|
|
141 void
|
|
142 cond2_(){
|
69
|
143 fprintf(vout,":");
|
66
|
144 }
|
|
145
|
|
146 void
|
|
147 cond_end_(){
|
|
148
|
|
149 }
|
|
150
|
|
151 void
|
|
152 decl_data_begin_(){
|
|
153
|
|
154 }
|
|
155
|
|
156 void
|
|
157 decl_data_end_(){
|
|
158
|
|
159 }
|
|
160
|
|
161 void
|
|
162 def_(NMTBL *n){
|
|
163 }
|
|
164
|
|
165 void
|
|
166 dowhile_(){
|
72
|
167 fprintf(vout,"do");
|
66
|
168 }
|
|
169
|
|
170 void
|
|
171 dowhile_cond_(){
|
72
|
172 fprintf(vout,"while(");
|
66
|
173 }
|
|
174
|
|
175 void
|
609
|
176 dowhile_end_(int cond){
|
72
|
177 fprintf(vout,");");
|
66
|
178 }
|
|
179
|
|
180 int
|
468
|
181 error_(int n){
|
66
|
182 return 0;
|
|
183 }
|
|
184
|
|
185 void
|
|
186 extern_(){
|
67
|
187 fprintf(vout,"extern ");
|
66
|
188 }
|
|
189
|
|
190 void
|
|
191 for_(){
|
68
|
192 fprintf(vout,"for(");
|
66
|
193 }
|
|
194
|
|
195 void
|
|
196 for1_(){
|
68
|
197 fprintf(vout,";");
|
66
|
198 }
|
|
199
|
|
200 void
|
609
|
201 for2_(int cond){
|
68
|
202 fprintf(vout,";");
|
66
|
203 }
|
|
204
|
|
205 void
|
609
|
206 for_body_(int conti){
|
68
|
207 fprintf(vout,")");
|
66
|
208 }
|
|
209
|
|
210 void
|
609
|
211 for_end_(int p0,int p1,int e){
|
66
|
212
|
|
213 }
|
|
214
|
|
215 void
|
|
216 funcall_(int type){
|
68
|
217 fprintf(vout,"(");
|
66
|
218 }
|
|
219
|
|
220 void
|
609
|
221 funcall_args_(int f, int t, int arg){
|
68
|
222 fprintf(vout,")");
|
66
|
223 }
|
|
224
|
|
225 void
|
79
|
226 function_(NMTBL *n,int cont){
|
|
227 int args;
|
564
|
228 type_print1(cadr(n->ty),0,vout,cont);
|
68
|
229 fprintf(vout,"%s(",n->nm);
|
79
|
230 args=n->dsp;
|
68
|
231 while(args) {
|
79
|
232 n=(NMTBL *)caddr(args);
|
597
|
233 if (n==&str_ret) { args=cadr(args); continue; }
|
68
|
234 type_print(n->ty,n,vout);
|
|
235 args=cadr(args);
|
76
|
236 if(args) {
|
327
|
237 /* if(caddr(args)==(int)&str_ret) break; */
|
76
|
238 fprintf(vout,",");
|
|
239 }
|
68
|
240 }
|
|
241 fprintf(vout,")");
|
66
|
242 }
|
|
243
|
|
244 void
|
|
245 function_end_(){
|
|
246
|
|
247 }
|
|
248
|
|
249 void
|
|
250 goto_(){
|
76
|
251 fprintf(vout,"goto ");
|
66
|
252 }
|
|
253
|
|
254 void
|
|
255 goto_label_(){
|
|
256
|
|
257 }
|
|
258
|
|
259 void
|
68
|
260 id_(int sym,NMTBL *nptr){
|
564
|
261 char *p = nptr->nm;
|
|
262 for(;*p && *p!='.';p++) fputc(*p,vout);
|
68
|
263 }
|
|
264
|
|
265 void
|
348
|
266 string_(char *s,int len){
|
69
|
267 int c;
|
|
268 fprintf(vout,"\"");
|
|
269 while((c=*s++)) {
|
|
270 if(c=='\n') fprintf(vout,"\\n");
|
|
271 else if(c=='\r') fprintf(vout,"\\r");
|
564
|
272 else if(c=='\t') fprintf(vout,"\\t");
|
69
|
273 else if(c=='\e') fprintf(vout,"\\e");
|
564
|
274 else if(c=='"') fprintf(vout,"\\\"");
|
|
275 else if(c=='\\') fprintf(vout,"\\\\");
|
69
|
276 else if(!(' '<=c&&c<=0x7f)) fprintf(vout,"\\%03o",c);
|
|
277 else fprintf(vout,"%c",c);
|
|
278 }
|
|
279 fprintf(vout,"\"");
|
68
|
280 }
|
|
281
|
|
282 void
|
|
283 const_(int symval){
|
|
284 fprintf(vout,"%d",symval);
|
|
285 }
|
66
|
286
|
68
|
287 void
|
|
288 return_f_(){
|
|
289 fprintf(vout,"return");
|
|
290 }
|
|
291
|
|
292 void
|
|
293 defined_(char *s){
|
|
294 fprintf(vout,"defined(%s)",s);
|
|
295 }
|
|
296
|
|
297 void
|
|
298 environment_(){
|
|
299 fprintf(vout,"environment");
|
66
|
300 }
|
|
301
|
|
302 void
|
|
303 if_(){
|
68
|
304 fprintf(vout,"if (");
|
66
|
305 }
|
|
306
|
|
307 void
|
|
308 if_else_(){
|
69
|
309 fprintf(vout," else ");
|
66
|
310 }
|
|
311
|
|
312 void
|
|
313 if_endif_(){
|
|
314 }
|
|
315
|
|
316 void
|
609
|
317 if_then_(int cond){
|
69
|
318 fprintf(vout,")");
|
66
|
319 }
|
|
320
|
|
321 void
|
|
322 jump_(int env){
|
|
323
|
|
324 }
|
|
325
|
|
326 void
|
|
327 label_(){
|
72
|
328 fprintf(vout,"%s:",nptr->nm);
|
66
|
329 }
|
|
330
|
|
331 void
|
67
|
332 lpar_(){
|
|
333 fprintf(vout,"(");
|
|
334 }
|
|
335
|
|
336 void
|
|
337 rpar_(){
|
|
338 fprintf(vout,")");
|
|
339 }
|
|
340
|
|
341 void
|
66
|
342 lbra_(int sym){
|
67
|
343 fprintf(vout,"[");
|
|
344 }
|
66
|
345
|
67
|
346 void
|
|
347 rbra_(int sym){
|
|
348 fprintf(vout,"]");
|
66
|
349 }
|
|
350
|
|
351 void
|
|
352 lc_(){
|
67
|
353 fprintf(vout,"{");
|
|
354 }
|
66
|
355
|
67
|
356 void
|
|
357 rc_(){
|
|
358 fprintf(vout,"}");
|
66
|
359 }
|
|
360
|
|
361 void
|
|
362 localvar_end_(){
|
|
363
|
|
364 }
|
|
365
|
|
366 void
|
|
367 op_(int sym){
|
68
|
368 sym_print(sym,vout);
|
66
|
369 }
|
|
370
|
|
371 void
|
|
372 postfix_(int sym){
|
69
|
373 sym_print(sym,vout);
|
66
|
374 }
|
|
375
|
|
376 void
|
|
377 prefix_(int sym){
|
69
|
378 sym_print(sym,vout);
|
66
|
379 }
|
|
380
|
|
381 void
|
|
382 register_(){
|
67
|
383 fprintf(vout,"register ");
|
66
|
384 }
|
|
385
|
|
386 void
|
|
387 return_(){
|
69
|
388 fprintf(vout,"return ");
|
66
|
389 }
|
|
390
|
|
391 void
|
609
|
392 return_end_(int e){
|
70
|
393 fprintf(vout,";");
|
66
|
394 }
|
|
395
|
|
396 void
|
67
|
397 return_type_(int t,NMTBL *nptr,int cont){
|
74
|
398 type_print1(t,nptr,vout,cont);
|
66
|
399 }
|
|
400
|
|
401 void
|
|
402 sm_(){
|
67
|
403 fprintf(vout,";");
|
66
|
404 }
|
|
405
|
|
406 void
|
|
407 static_(){
|
67
|
408 fprintf(vout,"static ");
|
66
|
409 }
|
|
410
|
|
411 void
|
|
412 switch_(){
|
68
|
413 fprintf(vout,"switch (");
|
66
|
414 }
|
|
415
|
|
416 void
|
609
|
417 switch_body_(int v){
|
71
|
418 fprintf(vout,")");
|
66
|
419 }
|
|
420
|
|
421 void
|
609
|
422 switch_end_(int cases){
|
66
|
423 }
|
|
424
|
|
425 void
|
|
426 typedef_(){
|
67
|
427 fprintf(vout,"typedef ");
|
66
|
428 }
|
|
429
|
|
430 void
|
564
|
431 type_(int t){
|
|
432 type_print(t,0,vout);
|
|
433 }
|
|
434
|
|
435 void
|
70
|
436 sdecl_(int s)
|
|
437 {
|
|
438 fprintf(vout,s==UNION?"union ":"struct ");
|
|
439 }
|
|
440
|
|
441 void
|
66
|
442 while_(){
|
68
|
443 fprintf(vout,"while (");
|
66
|
444 }
|
|
445
|
|
446 void
|
609
|
447 while_body_(int cond){
|
69
|
448 fprintf(vout,")");
|
66
|
449 }
|
|
450
|
|
451 void
|
|
452 while_end_(){
|
69
|
453 }
|
|
454
|
|
455 void
|
|
456 decl_data_()
|
|
457 {
|
|
458 }
|
|
459
|
|
460 void
|
|
461 break_()
|
|
462 {
|
|
463 fprintf(vout,"break;");
|
|
464 }
|
|
465
|
|
466 void
|
|
467 continue_()
|
|
468 {
|
|
469 fprintf(vout,"continue;");
|
66
|
470 }
|
|
471
|
|
472 /* end */
|