aboutsummaryrefslogtreecommitdiff
path: root/gcc/ch/timing.c
blob: aa0c7a6e079c221f83ba2a44faf47909c0bd0478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* Implement timing-related actions for CHILL.
   Copyright (C) 1992, 93, 1994 Free Software Foundation, Inc.

This file is part of GNU CC.

GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "config.h"
#include "system.h"
#include "tree.h"
#include "rtl.h"
#include "ch-tree.h"
#include "flags.h"
#include "input.h"
#include "obstack.h"
#include "lex.h"
#include "toplev.h"

#ifndef LONG_TYPE_SIZE
#define LONG_TYPE_SIZE BITS_PER_WORD
#endif

/* set non-zero if input text is forced to lowercase */
extern int ignore_case;

/* set non-zero if special words are to be entered in uppercase */
extern int special_UC;

/* timing modes */
tree abs_timing_type_node;
tree duration_timing_type_node;

/* rts time type */
static tree rtstime_type_node = NULL_TREE;

/* the stack for AFTER primval [ DELAY ] IN 
   and has following layout

   TREE_VALUE (TREE_VALUE (after_stack)) = current time or NULL_TREE (if DELAY specified)
   TREE_PURPOSE (TREE_VALUE (after_stack)) = the duration location
   TREE_VALUE (TREE_PURPOSE (after_stack)) = label at TIMEOUT
   TREE_PURPOSE (TREE_PURPOSE (after_stack)) = label at the end of AFTER action
*/
tree after_stack = NULL_TREE;

/* in pass 1 we need a seperate list for the labels */
static tree after_stack_pass_1 = NULL_TREE;
static tree after_help;

void
timing_init ()
{
  tree ptr_ftype_durt_ptr_int;
  tree int_ftype_abst_ptr_int;
  tree void_ftype_ptr;
  tree long_ftype_int_int_int_int_int_int_int_ptr_int;
  tree void_ftype_abstime_ptr;
  tree int_ftype_ptr_durt_ptr;
  tree void_ftype_durt_ptr;
  tree void_ftype_ptr_durt_ptr_int;
  tree temp;
  tree endlink;
  tree ulong_type;
  
  ulong_type = TREE_TYPE (lookup_name (
		          get_identifier ((ignore_case || ! special_UC ) ?
					  "ulong" : "ULONG")));

  /* build modes for TIME and DURATION */
  duration_timing_type_node = make_unsigned_type (LONG_TYPE_SIZE);
  temp = pushdecl (build_decl (TYPE_DECL, ridpointers[(int)RID_DURATION],
			       duration_timing_type_node));
  SET_CH_NOVELTY_NONNIL (duration_timing_type_node, temp);
  abs_timing_type_node = make_unsigned_type (LONG_TYPE_SIZE);
  temp = pushdecl (build_decl (TYPE_DECL, ridpointers[(int)RID_TIME],
			       abs_timing_type_node));
  SET_CH_NOVELTY_NONNIL (abs_timing_type_node, temp);

  /* the mode of time the runtimesystem returns */
  if (rtstime_type_node == NULL_TREE)
  {
      tree decl1, decl2, result;

      decl1 = build_decl (FIELD_DECL,
			  get_identifier ("secs"),
			  ulong_type);
      DECL_INITIAL (decl1) = NULL_TREE;
      decl2 = build_decl (FIELD_DECL,
			  get_identifier ("nsecs"),
			  ulong_type);
      DECL_INITIAL (decl2) = NULL_TREE;
      TREE_CHAIN (decl2) = NULL_TREE;
      TREE_CHAIN (decl1) = decl2;
      
      result = build_chill_struct_type (decl1);
      pushdecl (temp = build_decl (TYPE_DECL,
	get_identifier ("__tmp_rtstime"), result));
      DECL_SOURCE_LINE (temp) = 0;
      satisfy_decl (temp, 0);
      rtstime_type_node = TREE_TYPE (temp);
  }
  
  endlink = void_list_node;
  
  ptr_ftype_durt_ptr_int
    = build_function_type (ptr_type_node,
         tree_cons (NULL_TREE, duration_timing_type_node,
             tree_cons (NULL_TREE, ptr_type_node,
                 tree_cons (NULL_TREE, integer_type_node,
                     endlink))));

  int_ftype_abst_ptr_int
    = build_function_type (integer_type_node,
         tree_cons (NULL_TREE, abs_timing_type_node,
             tree_cons (NULL_TREE, ptr_type_node,
                 tree_cons (NULL_TREE, integer_type_node,
                     endlink))));

  void_ftype_ptr
     = build_function_type (void_type_node,
	   tree_cons (NULL_TREE, ptr_type_node,
	       endlink));

  long_ftype_int_int_int_int_int_int_int_ptr_int
    = build_function_type (abs_timing_type_node,
	 tree_cons (NULL_TREE, integer_type_node,
	    tree_cons (NULL_TREE, integer_type_node,
	       tree_cons (NULL_TREE, integer_type_node,
	          tree_cons (NULL_TREE, integer_type_node,
	             tree_cons (NULL_TREE, integer_type_node,
	                tree_cons (NULL_TREE, integer_type_node,
	                   tree_cons (NULL_TREE, integer_type_node,
	                      tree_cons (NULL_TREE, ptr_type_node,
	                         tree_cons (NULL_TREE, integer_type_node,
			            endlink))))))))));

  void_ftype_abstime_ptr
    = build_function_type (void_type_node,
          tree_cons (NULL_TREE, abs_timing_type_node,
              tree_cons (NULL_TREE, ptr_type_node,
                  endlink)));

  int_ftype_ptr_durt_ptr
    = build_function_type (integer_type_node,
          tree_cons (NULL_TREE, ptr_type_node,
              tree_cons (NULL_TREE, duration_timing_type_node,
                  tree_cons (NULL_TREE, ptr_type_node,
                      endlink))));

  void_ftype_durt_ptr
    = build_function_type (void_type_node,
          tree_cons (NULL_TREE, duration_timing_type_node,
              tree_cons (NULL_TREE, ptr_type_node,
                  endlink)));

  void_ftype_ptr_durt_ptr_int
    = build_function_type (void_type_node,
        tree_cons (NULL_TREE, ptr_type_node,
          tree_cons (NULL_TREE, duration_timing_type_node,
            tree_cons (NULL_TREE, ptr_type_node,
              tree_cons (NULL_TREE, integer_type_node,
                endlink)))));

  builtin_function ("_abstime", long_ftype_int_int_int_int_int_int_int_ptr_int,
		    NOT_BUILT_IN, NULL_PTR);
  builtin_function ("__check_cycle", void_ftype_ptr_durt_ptr_int,
		    NOT_BUILT_IN, NULL_PTR);
  builtin_function ("__convert_duration_rtstime", void_ftype_durt_ptr,
		    NOT_BUILT_IN, NULL_PTR);
  builtin_function ("__define_timeout", ptr_ftype_durt_ptr_int,
		    NOT_BUILT_IN, NULL_PTR);
  builtin_function ("_inttime", void_ftype_abstime_ptr,
		    NOT_BUILT_IN, NULL_PTR);
  builtin_function ("__remaintime", int_ftype_ptr_durt_ptr,
		    NOT_BUILT_IN, NULL_PTR);
  builtin_function ("__rtstime", void_ftype_ptr,
		    NOT_BUILT_IN, NULL_PTR);
  builtin_function ("__wait_until", int_ftype_abst_ptr_int,
		    NOT_BUILT_IN, NULL_PTR);
}

#if 0
 *
 * build AT action
 *
 * AT primval IN
 *  ok-actionlist
 * TIMEOUT
 *  to-actionlist
 * END;
 *
 * gets translated to
 *
 * if (__wait_until (primval) == 0)
 *   ok-actionlist
 * else
 *   to-action-list
 *
#endif

void
build_at_action (t)
     tree t;
{
  tree abstime, expr, filename, fcall;
  
  if (t == NULL_TREE || TREE_CODE (t) == ERROR_MARK)
    abstime = convert (abs_timing_type_node, build_int_2 (0, 0));
  else
    abstime = t;
  
  if (TREE_TYPE (abstime) != abs_timing_type_node)
    {
      error ("absolute time value must be of mode TIME.");
      abstime = convert (abs_timing_type_node, build_int_2 (0, 0));
    }
  filename = force_addr_of (get_chill_filename ());
  fcall = build_chill_function_call (
	    lookup_name (get_identifier ("__wait_until")),
	      tree_cons (NULL_TREE, abstime,
		tree_cons (NULL_TREE, filename,
		  tree_cons (NULL_TREE, get_chill_linenumber (), NULL_TREE))));
  expr = build (EQ_EXPR, integer_type_node, fcall, integer_zero_node);
  expand_start_cond (expr, 0);
  emit_line_note (input_filename, lineno);
}

#if 0
 *
 * build CYCLE action
 *
 * CYCLE primval IN
 *  actionlist
 * END;
 *
 * gets translated to
 *
 * {
 *    RtsTime  now; 
 *  label:
 *    __rtstime (&now); 
 *     actionlist
 *    __check_cycle (&now, primval, filename, lineno); 
 *    goto label;
 *  }
 *
#endif

tree
build_cycle_start (t)
    tree t;
{
  tree purpose = build_tree_list (NULL_TREE, NULL_TREE);
  tree toid = build_tree_list (purpose, NULL_TREE);

  /* define the label. Note: define_label needs to be called in
     pass 1 and pass 2. */
  TREE_VALUE (toid) = define_label (input_filename, lineno,
				    get_unique_identifier ("CYCLE_label"));
  if (! ignoring)
    {
      tree duration_value, now_location;
      
      if (t == NULL_TREE || TREE_CODE (t) == ERROR_MARK)
	duration_value = convert (duration_timing_type_node, build_int_2 (0,0));
      else
	duration_value = t;
      
      if (TREE_TYPE (duration_value) != duration_timing_type_node)
	{
	  error ("duration primitive value must be of mode DURATION.");
	  duration_value = convert (duration_timing_type_node, build_int_2 (0,0));
	}
      TREE_PURPOSE (TREE_PURPOSE (toid)) = duration_value;
      /* define the variable */
      now_location = decl_temp1 (get_unique_identifier ("CYCLE_var"),
				 rtstime_type_node, 0,
				 NULL_TREE, 0, 0);
      TREE_VALUE (TREE_PURPOSE (toid)) = force_addr_of (now_location);
      
      /* build the call to __rtstime */
      expand_expr_stmt (
        build_chill_function_call (lookup_name (get_identifier ("__rtstime")),
          build_tree_list (NULL_TREE, TREE_VALUE (TREE_PURPOSE (toid)))));
    }

  return toid;
}

void
build_cycle_end (toid)
     tree toid;
{
  tree filename, linenumber;
  
  /* here we call __check_cycle and then jump to beginning of this
     action */
  filename = force_addr_of (get_chill_filename ());
  linenumber = get_chill_linenumber ();
  expand_expr_stmt (
    build_chill_function_call (
      lookup_name (get_identifier ("__check_cycle")),
	tree_cons (NULL_TREE, TREE_VALUE (TREE_PURPOSE (toid)),
          tree_cons (NULL_TREE, TREE_PURPOSE (TREE_PURPOSE (toid)),
	    tree_cons (NULL_TREE, filename,
	      tree_cons (NULL_TREE, linenumber, NULL_TREE))))));
  expand_goto (TREE_VALUE (toid));
}

#if 0
 *
 * build AFTER ACTION
 *
 * AFTER primval [ DELAY ] IN
 *  action-list
 * TIMEOUT
 *  to-action-list
 * END
 *
 * gets translated to
 *
 * {
 *   struct chill_time __now; 
 *   duration dur = primval; 
 *   if (! delay_spceified)
 *     __rts_time (&__now); 
 *     .
 *     .
 *    goto end-label;
 *   to-label:
 *     .
 *     .
 *   end-label:
 * }
 *
#endif

void
build_after_start (duration, delay_flag)
    tree duration;
    int  delay_flag;
{
  tree value, purpose;
  
  if (! ignoring)
    {
      value = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
      purpose = after_stack_pass_1;
      after_stack_pass_1 = TREE_CHAIN (after_stack_pass_1);
      after_stack = tree_cons (purpose, value, after_stack);
      
      if (TREE_TYPE (duration) != duration_timing_type_node)
        {
	  error ("duration primitive value must be of mode DURATION.");
	  duration = convert (duration_timing_type_node, build_int_2 (0,0));
        }
      TREE_PURPOSE (value) = decl_temp1 (get_identifier ("AFTER_duration"),
					 duration_timing_type_node, 0,
					 duration, 0, 0);
      
      if (! delay_flag)
        {
	  /* in this case we have to get the current time */
	  TREE_VALUE (value) = decl_temp1 (get_unique_identifier ("AFTER_now"),
					   rtstime_type_node, 0,
					   NULL_TREE, 0, 0);
	  /* build the function call to initialize the variable */
	  expand_expr_stmt (
            build_chill_function_call (lookup_name (get_identifier ("__rtstime")),
              build_tree_list (NULL_TREE, force_addr_of (TREE_VALUE (value)))));
        }
    }
  else
    {
      /* in pass 1 we just save the labels */
      after_help = tree_cons (NULL_TREE, NULL_TREE, after_help);
      after_stack_pass_1 = chainon (after_stack_pass_1, after_help);
    }
}

void
build_after_timeout_start ()
{
  tree label_name;
  
  if (! ignoring)
    {
      /* jump to the end of AFTER action */
      lookup_and_expand_goto (TREE_PURPOSE (TREE_PURPOSE (after_stack)));
      label_name = TREE_VALUE (TREE_PURPOSE (after_stack));
      /* mark we are in TIMEOUT part of AFTER action */
      TREE_VALUE (TREE_PURPOSE (after_stack)) = NULL_TREE;
    }
  else
    {
      label_name = get_unique_identifier ("AFTER_tolabel");
      TREE_VALUE (after_help) = label_name;
    }
  define_label (input_filename, lineno, label_name);
}

void
build_after_end ()
{
  tree label_name;
    
  /* define the end label */
  if (! ignoring)
    {
      label_name = TREE_PURPOSE (TREE_PURPOSE (after_stack));
      after_stack = TREE_CHAIN (after_stack);
    }
  else
    {
      label_name = get_unique_identifier ("AFTER_endlabel");
      TREE_PURPOSE (after_help) = label_name;
      after_help = TREE_CHAIN (after_help);
    }
  define_label (input_filename, lineno, label_name);
}

tree
build_timeout_preface ()
{
  tree timeout_value = null_pointer_node;
  
  if (after_stack != NULL_TREE &&
      TREE_VALUE (TREE_PURPOSE (after_stack)) != NULL_TREE)
    {
      tree to_loc;
      
      to_loc = decl_temp1 (get_unique_identifier ("TOloc"),
			   rtstime_type_node, 0, NULL_TREE, 0, 0);
      timeout_value = force_addr_of (to_loc);

      if (TREE_VALUE (TREE_VALUE (after_stack)) == NULL_TREE)
        {
	  /* DELAY specified -- just call __convert_duration_rtstime for
	     given duration value */
	  expand_expr_stmt (
            build_chill_function_call (
              lookup_name (get_identifier ("__convert_duration_rtstime")),
                tree_cons (NULL_TREE, TREE_PURPOSE (TREE_VALUE (after_stack)),
                  tree_cons (NULL_TREE, timeout_value, NULL_TREE))));
        }
      else
        {
	  /* delay not specified -- call __remaintime which returns the 
	     remaining time of duration in rtstime format and check the 
	     result */
	  tree fcall = 
            build_chill_function_call (
              lookup_name (get_identifier ("__remaintime")),
                tree_cons (NULL_TREE, force_addr_of (TREE_VALUE (TREE_VALUE (after_stack))),
		  tree_cons (NULL_TREE, TREE_PURPOSE (TREE_VALUE (after_stack)),
                    tree_cons (NULL_TREE, timeout_value, NULL_TREE))));
	  tree expr = build (NE_EXPR, integer_type_node,
			     fcall, integer_zero_node);
	  expand_start_cond (expr, 0);
	  lookup_and_expand_goto (TREE_VALUE (TREE_PURPOSE (after_stack)));
	  expand_end_cond ();
        }
    }
  return timeout_value;
}

void
build_timesupervised_call (fcall, to_loc)
    tree fcall;
    tree to_loc;
{
  if (to_loc == null_pointer_node)
    expand_expr_stmt (fcall);
  else
    {
      tree expr = build (NE_EXPR, integer_type_node, fcall, integer_zero_node);
      expand_start_cond (expr, 0);
      lookup_and_expand_goto (TREE_VALUE (TREE_PURPOSE (after_stack)));
      expand_end_cond ();
    }
}