OSDN Git Service

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