OSDN Git Service

* cccp.c, cexp.y, cexp.c, cccp.1: Removed.
[pf3gnuchains/gcc-fork.git] / gcc / ch / timing.c
1 /* Implement timing-related actions for CHILL.
2    Copyright (C) 1992, 93, 1994, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "tree.h"
24 #include "rtl.h"
25 #include "ch-tree.h"
26 #include "flags.h"
27 #include "input.h"
28 #include "obstack.h"
29 #include "lex.h"
30 #include "toplev.h"
31
32 #ifndef LONG_TYPE_SIZE
33 #define LONG_TYPE_SIZE BITS_PER_WORD
34 #endif
35
36 /* set non-zero if input text is forced to lowercase */
37 extern int ignore_case;
38
39 /* set non-zero if special words are to be entered in uppercase */
40 extern int special_UC;
41
42 /* timing modes */
43 tree abs_timing_type_node;
44 tree duration_timing_type_node;
45
46 /* rts time type */
47 static tree rtstime_type_node = NULL_TREE;
48
49 /* the stack for AFTER primval [ DELAY ] IN 
50    and has following layout
51
52    TREE_VALUE (TREE_VALUE (after_stack)) = current time or NULL_TREE (if DELAY specified)
53    TREE_PURPOSE (TREE_VALUE (after_stack)) = the duration location
54    TREE_VALUE (TREE_PURPOSE (after_stack)) = label at TIMEOUT
55    TREE_PURPOSE (TREE_PURPOSE (after_stack)) = label at the end of AFTER action
56 */
57 tree after_stack = NULL_TREE;
58
59 /* in pass 1 we need a separate list for the labels */
60 static tree after_stack_pass_1 = NULL_TREE;
61 static tree after_help;
62
63 void
64 timing_init ()
65 {
66   tree ptr_ftype_durt_ptr_int;
67   tree int_ftype_abst_ptr_int;
68   tree void_ftype_ptr;
69   tree long_ftype_int_int_int_int_int_int_int_ptr_int;
70   tree void_ftype_abstime_ptr;
71   tree int_ftype_ptr_durt_ptr;
72   tree void_ftype_durt_ptr;
73   tree void_ftype_ptr_durt_ptr_int;
74   tree temp;
75   tree endlink;
76   tree ulong_type;
77   
78   ulong_type = TREE_TYPE (lookup_name (
79                           get_identifier ((ignore_case || ! special_UC ) ?
80                                           "ulong" : "ULONG")));
81
82   /* build modes for TIME and DURATION */
83   duration_timing_type_node = make_unsigned_type (LONG_TYPE_SIZE);
84   temp = pushdecl (build_decl (TYPE_DECL, ridpointers[(int)RID_DURATION],
85                                duration_timing_type_node));
86   SET_CH_NOVELTY_NONNIL (duration_timing_type_node, temp);
87   abs_timing_type_node = make_unsigned_type (LONG_TYPE_SIZE);
88   temp = pushdecl (build_decl (TYPE_DECL, ridpointers[(int)RID_TIME],
89                                abs_timing_type_node));
90   SET_CH_NOVELTY_NONNIL (abs_timing_type_node, temp);
91
92   /* the mode of time the runtimesystem returns */
93   if (rtstime_type_node == NULL_TREE)
94   {
95       tree decl1, decl2, result;
96
97       decl1 = build_decl (FIELD_DECL,
98                           get_identifier ("secs"),
99                           ulong_type);
100       DECL_INITIAL (decl1) = NULL_TREE;
101       decl2 = build_decl (FIELD_DECL,
102                           get_identifier ("nsecs"),
103                           ulong_type);
104       DECL_INITIAL (decl2) = NULL_TREE;
105       TREE_CHAIN (decl2) = NULL_TREE;
106       TREE_CHAIN (decl1) = decl2;
107       
108       result = build_chill_struct_type (decl1);
109       pushdecl (temp = build_decl (TYPE_DECL,
110         get_identifier ("__tmp_rtstime"), result));
111       DECL_SOURCE_LINE (temp) = 0;
112       satisfy_decl (temp, 0);
113       rtstime_type_node = TREE_TYPE (temp);
114   }
115   
116   endlink = void_list_node;
117   
118   ptr_ftype_durt_ptr_int
119     = build_function_type (ptr_type_node,
120          tree_cons (NULL_TREE, duration_timing_type_node,
121              tree_cons (NULL_TREE, ptr_type_node,
122                  tree_cons (NULL_TREE, integer_type_node,
123                      endlink))));
124
125   int_ftype_abst_ptr_int
126     = build_function_type (integer_type_node,
127          tree_cons (NULL_TREE, abs_timing_type_node,
128              tree_cons (NULL_TREE, ptr_type_node,
129                  tree_cons (NULL_TREE, integer_type_node,
130                      endlink))));
131
132   void_ftype_ptr
133      = build_function_type (void_type_node,
134            tree_cons (NULL_TREE, ptr_type_node,
135                endlink));
136
137   long_ftype_int_int_int_int_int_int_int_ptr_int
138     = build_function_type (abs_timing_type_node,
139          tree_cons (NULL_TREE, integer_type_node,
140             tree_cons (NULL_TREE, integer_type_node,
141                tree_cons (NULL_TREE, integer_type_node,
142                   tree_cons (NULL_TREE, integer_type_node,
143                      tree_cons (NULL_TREE, integer_type_node,
144                         tree_cons (NULL_TREE, integer_type_node,
145                            tree_cons (NULL_TREE, integer_type_node,
146                               tree_cons (NULL_TREE, ptr_type_node,
147                                  tree_cons (NULL_TREE, integer_type_node,
148                                     endlink))))))))));
149
150   void_ftype_abstime_ptr
151     = build_function_type (void_type_node,
152           tree_cons (NULL_TREE, abs_timing_type_node,
153               tree_cons (NULL_TREE, ptr_type_node,
154                   endlink)));
155
156   int_ftype_ptr_durt_ptr
157     = build_function_type (integer_type_node,
158           tree_cons (NULL_TREE, ptr_type_node,
159               tree_cons (NULL_TREE, duration_timing_type_node,
160                   tree_cons (NULL_TREE, ptr_type_node,
161                       endlink))));
162
163   void_ftype_durt_ptr
164     = build_function_type (void_type_node,
165           tree_cons (NULL_TREE, duration_timing_type_node,
166               tree_cons (NULL_TREE, ptr_type_node,
167                   endlink)));
168
169   void_ftype_ptr_durt_ptr_int
170     = build_function_type (void_type_node,
171         tree_cons (NULL_TREE, ptr_type_node,
172           tree_cons (NULL_TREE, duration_timing_type_node,
173             tree_cons (NULL_TREE, ptr_type_node,
174               tree_cons (NULL_TREE, integer_type_node,
175                 endlink)))));
176
177   builtin_function ("_abstime", long_ftype_int_int_int_int_int_int_int_ptr_int,
178                     0, NOT_BUILT_IN, NULL_PTR);
179   builtin_function ("__check_cycle", void_ftype_ptr_durt_ptr_int,
180                     0, NOT_BUILT_IN, NULL_PTR);
181   builtin_function ("__convert_duration_rtstime", void_ftype_durt_ptr,
182                     0, NOT_BUILT_IN, NULL_PTR);
183   builtin_function ("__define_timeout", ptr_ftype_durt_ptr_int,
184                     0, NOT_BUILT_IN, NULL_PTR);
185   builtin_function ("_inttime", void_ftype_abstime_ptr,
186                     0, NOT_BUILT_IN, NULL_PTR);
187   builtin_function ("__remaintime", int_ftype_ptr_durt_ptr,
188                     0, NOT_BUILT_IN, NULL_PTR);
189   builtin_function ("__rtstime", void_ftype_ptr,
190                     0, NOT_BUILT_IN, NULL_PTR);
191   builtin_function ("__wait_until", int_ftype_abst_ptr_int,
192                     0, NOT_BUILT_IN, NULL_PTR);
193 }
194
195 #if 0
196  *
197  * build AT action
198  *
199  * AT primval IN
200  *  ok-actionlist
201  * TIMEOUT
202  *  to-actionlist
203  * END;
204  *
205  * gets translated to
206  *
207  * if (__wait_until (primval) == 0)
208  *   ok-actionlist
209  * else
210  *   to-action-list
211  *
212 #endif
213
214 void
215 build_at_action (t)
216      tree t;
217 {
218   tree abstime, expr, filename, fcall;
219   
220   if (t == NULL_TREE || TREE_CODE (t) == ERROR_MARK)
221     abstime = convert (abs_timing_type_node, build_int_2 (0, 0));
222   else
223     abstime = t;
224   
225   if (TREE_TYPE (abstime) != abs_timing_type_node)
226     {
227       error ("absolute time value must be of mode TIME.");
228       abstime = convert (abs_timing_type_node, build_int_2 (0, 0));
229     }
230   filename = force_addr_of (get_chill_filename ());
231   fcall = build_chill_function_call (
232             lookup_name (get_identifier ("__wait_until")),
233               tree_cons (NULL_TREE, abstime,
234                 tree_cons (NULL_TREE, filename,
235                   tree_cons (NULL_TREE, get_chill_linenumber (), NULL_TREE))));
236   expr = build (EQ_EXPR, integer_type_node, fcall, integer_zero_node);
237   expand_start_cond (expr, 0);
238   emit_line_note (input_filename, lineno);
239 }
240
241 #if 0
242  *
243  * build CYCLE action
244  *
245  * CYCLE primval IN
246  *  actionlist
247  * END;
248  *
249  * gets translated to
250  *
251  * {
252  *    RtsTime  now; 
253  *  label:
254  *    __rtstime (&now); 
255  *     actionlist
256  *    __check_cycle (&now, primval, filename, lineno); 
257  *    goto label;
258  *  }
259  *
260 #endif
261
262 tree
263 build_cycle_start (t)
264     tree t;
265 {
266   tree purpose = build_tree_list (NULL_TREE, NULL_TREE);
267   tree toid = build_tree_list (purpose, NULL_TREE);
268
269   /* define the label. Note: define_label needs to be called in
270      pass 1 and pass 2. */
271   TREE_VALUE (toid) = define_label (input_filename, lineno,
272                                     get_unique_identifier ("CYCLE_label"));
273   if (! ignoring)
274     {
275       tree duration_value, now_location;
276       
277       if (t == NULL_TREE || TREE_CODE (t) == ERROR_MARK)
278         duration_value = convert (duration_timing_type_node, build_int_2 (0,0));
279       else
280         duration_value = t;
281       
282       if (TREE_TYPE (duration_value) != duration_timing_type_node)
283         {
284           error ("duration primitive value must be of mode DURATION.");
285           duration_value = convert (duration_timing_type_node, build_int_2 (0,0));
286         }
287       TREE_PURPOSE (TREE_PURPOSE (toid)) = duration_value;
288       /* define the variable */
289       now_location = decl_temp1 (get_unique_identifier ("CYCLE_var"),
290                                  rtstime_type_node, 0,
291                                  NULL_TREE, 0, 0);
292       TREE_VALUE (TREE_PURPOSE (toid)) = force_addr_of (now_location);
293       
294       /* build the call to __rtstime */
295       expand_expr_stmt (
296         build_chill_function_call (lookup_name (get_identifier ("__rtstime")),
297           build_tree_list (NULL_TREE, TREE_VALUE (TREE_PURPOSE (toid)))));
298     }
299
300   return toid;
301 }
302
303 void
304 build_cycle_end (toid)
305      tree toid;
306 {
307   tree filename, linenumber;
308   
309   /* here we call __check_cycle and then jump to beginning of this
310      action */
311   filename = force_addr_of (get_chill_filename ());
312   linenumber = get_chill_linenumber ();
313   expand_expr_stmt (
314     build_chill_function_call (
315       lookup_name (get_identifier ("__check_cycle")),
316         tree_cons (NULL_TREE, TREE_VALUE (TREE_PURPOSE (toid)),
317           tree_cons (NULL_TREE, TREE_PURPOSE (TREE_PURPOSE (toid)),
318             tree_cons (NULL_TREE, filename,
319               tree_cons (NULL_TREE, linenumber, NULL_TREE))))));
320   expand_goto (TREE_VALUE (toid));
321 }
322
323 #if 0
324  *
325  * build AFTER ACTION
326  *
327  * AFTER primval [ DELAY ] IN
328  *  action-list
329  * TIMEOUT
330  *  to-action-list
331  * END
332  *
333  * gets translated to
334  *
335  * {
336  *   struct chill_time __now; 
337  *   duration dur = primval; 
338  *   if (! delay_spceified)
339  *     __rts_time (&__now); 
340  *     .
341  *     .
342  *    goto end-label;
343  *   to-label:
344  *     .
345  *     .
346  *   end-label:
347  * }
348  *
349 #endif
350
351 void
352 build_after_start (duration, delay_flag)
353     tree duration;
354     int  delay_flag;
355 {
356   tree value, purpose;
357   
358   if (! ignoring)
359     {
360       value = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
361       purpose = after_stack_pass_1;
362       after_stack_pass_1 = TREE_CHAIN (after_stack_pass_1);
363       after_stack = tree_cons (purpose, value, after_stack);
364       
365       if (TREE_TYPE (duration) != duration_timing_type_node)
366         {
367           error ("duration primitive value must be of mode DURATION.");
368           duration = convert (duration_timing_type_node, build_int_2 (0,0));
369         }
370       TREE_PURPOSE (value) = decl_temp1 (get_identifier ("AFTER_duration"),
371                                          duration_timing_type_node, 0,
372                                          duration, 0, 0);
373       
374       if (! delay_flag)
375         {
376           /* in this case we have to get the current time */
377           TREE_VALUE (value) = decl_temp1 (get_unique_identifier ("AFTER_now"),
378                                            rtstime_type_node, 0,
379                                            NULL_TREE, 0, 0);
380           /* build the function call to initialize the variable */
381           expand_expr_stmt (
382             build_chill_function_call (lookup_name (get_identifier ("__rtstime")),
383               build_tree_list (NULL_TREE, force_addr_of (TREE_VALUE (value)))));
384         }
385     }
386   else
387     {
388       /* in pass 1 we just save the labels */
389       after_help = tree_cons (NULL_TREE, NULL_TREE, after_help);
390       after_stack_pass_1 = chainon (after_stack_pass_1, after_help);
391     }
392 }
393
394 void
395 build_after_timeout_start ()
396 {
397   tree label_name;
398   
399   if (! ignoring)
400     {
401       /* jump to the end of AFTER action */
402       lookup_and_expand_goto (TREE_PURPOSE (TREE_PURPOSE (after_stack)));
403       label_name = TREE_VALUE (TREE_PURPOSE (after_stack));
404       /* mark we are in TIMEOUT part of AFTER action */
405       TREE_VALUE (TREE_PURPOSE (after_stack)) = NULL_TREE;
406     }
407   else
408     {
409       label_name = get_unique_identifier ("AFTER_tolabel");
410       TREE_VALUE (after_help) = label_name;
411     }
412   define_label (input_filename, lineno, label_name);
413 }
414
415 void
416 build_after_end ()
417 {
418   tree label_name;
419     
420   /* define the end label */
421   if (! ignoring)
422     {
423       label_name = TREE_PURPOSE (TREE_PURPOSE (after_stack));
424       after_stack = TREE_CHAIN (after_stack);
425     }
426   else
427     {
428       label_name = get_unique_identifier ("AFTER_endlabel");
429       TREE_PURPOSE (after_help) = label_name;
430       after_help = TREE_CHAIN (after_help);
431     }
432   define_label (input_filename, lineno, label_name);
433 }
434
435 tree
436 build_timeout_preface ()
437 {
438   tree timeout_value = null_pointer_node;
439   
440   if (after_stack != NULL_TREE &&
441       TREE_VALUE (TREE_PURPOSE (after_stack)) != NULL_TREE)
442     {
443       tree to_loc;
444       
445       to_loc = decl_temp1 (get_unique_identifier ("TOloc"),
446                            rtstime_type_node, 0, NULL_TREE, 0, 0);
447       timeout_value = force_addr_of (to_loc);
448
449       if (TREE_VALUE (TREE_VALUE (after_stack)) == NULL_TREE)
450         {
451           /* DELAY specified -- just call __convert_duration_rtstime for
452              given duration value */
453           expand_expr_stmt (
454             build_chill_function_call (
455               lookup_name (get_identifier ("__convert_duration_rtstime")),
456                 tree_cons (NULL_TREE, TREE_PURPOSE (TREE_VALUE (after_stack)),
457                   tree_cons (NULL_TREE, timeout_value, NULL_TREE))));
458         }
459       else
460         {
461           /* delay not specified -- call __remaintime which returns the 
462              remaining time of duration in rtstime format and check the 
463              result */
464           tree fcall = 
465             build_chill_function_call (
466               lookup_name (get_identifier ("__remaintime")),
467                 tree_cons (NULL_TREE, force_addr_of (TREE_VALUE (TREE_VALUE (after_stack))),
468                   tree_cons (NULL_TREE, TREE_PURPOSE (TREE_VALUE (after_stack)),
469                     tree_cons (NULL_TREE, timeout_value, NULL_TREE))));
470           tree expr = build (NE_EXPR, integer_type_node,
471                              fcall, integer_zero_node);
472           expand_start_cond (expr, 0);
473           lookup_and_expand_goto (TREE_VALUE (TREE_PURPOSE (after_stack)));
474           expand_end_cond ();
475         }
476     }
477   return timeout_value;
478 }
479
480 void
481 build_timesupervised_call (fcall, to_loc)
482     tree fcall;
483     tree to_loc;
484 {
485   if (to_loc == null_pointer_node)
486     expand_expr_stmt (fcall);
487   else
488     {
489       tree expr = build (NE_EXPR, integer_type_node, fcall, integer_zero_node);
490       expand_start_cond (expr, 0);
491       lookup_and_expand_goto (TREE_VALUE (TREE_PURPOSE (after_stack)));
492       expand_end_cond ();
493     }
494 }