File: once_routine_pool.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (675 lines) | stat: -rw-r--r-- 19,397 bytes parent folder | download | duplicates (2)
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
-- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries
--
-- SmartEiffel 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.
-- SmartEiffel 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 SmartEiffel;  see the file COPYING.  If not,  write to
-- the Free Software Foundation,  Inc., 59 Temple Place - Suite 330,  Boston, 
-- MA 02111-1307, USA.
--
-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.
--			   - University of Nancy 1 - FRANCE
-- Copyright(C) 2003:      INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne
--			   - University of Nancy 2 - FRANCE
--
--		 Dominique COLNET, Suzanne COLLIN, Olivier ZENDRA,
--			   Philippe RIBET, Cyril ADRIAN
--
-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
--
class ONCE_ROUTINE_POOL
   --
   -- Singleton object in charge of runnable once routines.
   -- This singleton is shared via the GLOBALS.`once_routine_pool' once function.
   --

inherit
   GLOBALS
   VISITABLE

feature {ONCE_ROUTINE_POOL_VISITOR}

   accept(visitor: ONCE_ROUTINE_POOL_VISITOR) is
      do
         visitor.visit_once_routine_pool(Current)
      end

feature {RUN_FEATURE} -- For `compile_to_c' as well as `compile_to_jvm':

   register_procedure(rf: RUN_FEATURE) is
      require
         rf.is_once_procedure
      do
         check
            not procedure_list.has(rf)
         end
         procedure_list.add_last(rf)
      end

   register_function(rf: RUN_FEATURE) is
      require
         rf.is_once_function
      local
         rc: RUN_CLASS
      do
         check not function_list.has(rf) end
         function_list.add_last(rf)
	 if rf.name.to_string = as_std_output then
	    if std_output_flush_atexit = Void then
	       if rf.base_feature.base_class.name.to_string = as_general then
		  rc := rf.result_type.run_class
		  std_output_flush_atexit := rc.get_feature_with(as_flush)
	       end
	    end
	 end
      end

feature {GC_HANDLER}

   gc_mark_in(c_code: STRING) is
	 -- To mark results of once functions because they are part of the root.
      local
         i: INTEGER; rf: RUN_FEATURE; of: ONCE_FUNCTION
         mem: FIXED_ARRAY[ONCE_FUNCTION]; rt: E_TYPE; oresult: STRING
      do
         if function_list.count > 0 then
            from
               i := function_list.upper
               create mem.with_capacity(i // 2)
            until
               i < 0
            loop
               rf := function_list.item(i)
               of ?= rf.base_feature
	       check
		  of /= Void
	       end
               if not mem.fast_has(of) then
                  if rf.current_type.run_class.at_run_time then
                     mem.add_last(of)
                     rt := rf.result_type
                     if rt.need_gc_mark_function then
                        oresult := o_result(of)
			gc_handler.mark_for(c_code,oresult,rt.run_class)
                     end
                  end
               end
               i := i - 1
            end
         end
      end

feature {JVM}

   fields_count: INTEGER

   jvm_define_fields is
      local
         byte_idx, idx_flag, i: INTEGER; rf: RUN_FEATURE
         bf: E_FEATURE; name_list: FIXED_ARRAY[INTEGER]
      do
         create name_list.with_capacity(fields_count)
         if function_list.count > 0 then
            from
               i := function_list.upper
               byte_idx := constant_pool.idx_utf8(fz_41)
            until
               i < 0
            loop
               rf := function_list.item(i)
               bf := rf.base_feature
               idx_flag := idx_name_for_flag(bf)
               if name_list.fast_has(idx_flag) then
               else
                  name_list.add_last(idx_flag)
                  -- ---------- Static field for flag:
                  field_info.add(9,idx_flag,byte_idx)
                  -- ---------- Static field for result:
                  field_info.add(9,
                                 idx_name_for_result(bf),
                                 idx_descriptor(rf.result_type.run_type))
               end
               i := i - 1
            end
         end
         if procedure_list.count > 0 then
            from
               i := procedure_list.upper
               byte_idx := constant_pool.idx_utf8(fz_41)
            until
               i < 0
            loop
               rf := procedure_list.item(i)
               bf := rf.base_feature
               idx_flag := idx_name_for_flag(bf)
               if name_list.fast_has(idx_flag) then
               else
                  name_list.add_last(idx_flag)
                  -- ---------- Static field for flag:
                  field_info.add(9,idx_flag,byte_idx)
               end
               i := i - 1
            end
         end
      end

   jvm_initialize_fields is
      local
         i: INTEGER
      do
         if jvm_flag_list.count > 0 then
            from
               i := jvm_flag_list.upper
            until
               i < 0
            loop
               -- Set once flag :
               code_attribute.opcode_iconst_0
               code_attribute.opcode_putstatic(jvm_flag_list.item(i),-1)
               i := i - 1
            end
         end
      end

feature {RUN_FEATURE} -- For `compile_to_jvm' only:

   idx_fieldref_for_flag(rf: RUN_FEATURE): INTEGER is
      require
         rf.is_once_routine
      local
	 cp: CONSTANT_POOL
      do
	 cp := constant_pool
         Result := cp.idx_fieldref3(jvm_root_class,
				    o_flag(rf.base_feature),
				    fz_41)
      end

feature {NONE} -- For `compile_to_c' as well as `compile_to_jvm':

   idx_fieldref_for_result(rf: RUN_FEATURE): INTEGER is
      require
         rf.is_once_function
      local
	 cp: CONSTANT_POOL
	 rt: E_TYPE
      do
	 cp := constant_pool
         rt := rf.result_type.run_type
         Result := cp.idx_fieldref3(jvm_root_class,
				    o_result(rf.base_feature),
				    jvm_descriptor(rt))
      end

   procedure_list: FIXED_ARRAY[RUN_FEATURE] is
	 -- Live set of once procedures.
      once
         create Result.with_capacity(32)
      end

   function_list: FIXED_ARRAY[RUN_FEATURE] is
	 -- Live set of once functions.
      once
         create Result.with_capacity(32)
      end

   o_flag(bf: E_FEATURE): STRING is
	 -- Compute the only one corresponding `flag': fBCxxKey used
	 -- to check that execution is done.
      require
	 bf /= Void
      do
	 Result := o_flag_buffer
         Result.clear
         Result.append(once "fBC")
         bf.base_class.id.append_in(Result)
         Result.append(bf.first_name.to_key)
	 Result := string_aliaser.item(Result)
      ensure
	 Result = string_aliaser.item(Result)
      end

   buffer: STRING is
      once
	 create Result.make(64)
      end

   c_define_o_result_buffer: STRING is
      once
	 create Result.make(64)
      end

   o_flag_buffer: STRING is
      once
	 create Result.make(32)
      end

   o_result_buffer: STRING is
      once
	 create Result.make(32)
      end

   jvm_descriptor_buffer: STRING is
      once
	 create Result.make(32)
      end

feature {E_RESULT, RUN_FEATURE, RUN_CLASS}

   o_result(bf: E_FEATURE): STRING is
      require
	 bf /= Void
      do
	 Result := o_result_buffer
         Result.clear
         Result.append(once "oBC")
         bf.base_class.id.append_in(Result)
         Result.append(bf.first_name.to_key)
	 Result := string_aliaser.item(Result)
      ensure
	 Result = string_aliaser.item(Result)
      end

   c_put_o_result(rf: RUN_FEATURE) is
      require
	 rf.is_once_function
      do
	 cpp.put_string(o_result(rf.base_feature))
      end

   jvm_result_load(rf: RUN_FEATURE) is
      require
	 rf.is_once_function
      local
         result_space, idx_result: INTEGER
      do
         result_space := rf.result_type.jvm_stack_space
         idx_result := idx_fieldref_for_result(rf)
         code_attribute.opcode_getstatic(idx_result,result_space)
      end

   jvm_result_store(rf: RUN_FEATURE) is
      require
	 rf.is_once_function
      local
         result_space, idx_result: INTEGER
      do
         result_space := rf.result_type.jvm_stack_space
         idx_result := idx_fieldref_for_result(rf)
         code_attribute.opcode_putstatic(idx_result,- result_space)
      end

feature {C_PRETTY_PRINTER} -- For `compile_to_c':

   c_pre_compute is
	 -- Generate C code to pre_compute some functions.
      local
	 i: INTEGER; memory: FIXED_ARRAY[E_FEATURE]
	 rf: RUN_FEATURE; bf: E_FEATURE
      do
	 echo.put_string(fz_04)
	 echo.put_string(fz_05)
	 cpp.put_string(fz_pco_comment)
	 from
	    i := function_list.upper
	    create memory.with_capacity(64)
	 until
	    i < 0
	 loop
	    rf := function_list.item(i)
	    if rf.run_class.at_run_time then
	       if rf.once_pre_computable then
		  bf := rf.base_feature
		  if not memory.fast_has(bf) then
		     memory.add_last(bf)
		     echo.put_character('%T')
		     echo.put_string(bf.base_class.name.to_string)
		     echo.put_character('.')
		     echo.put_string(rf.name.to_string)
		     echo.put_character('%N')
		     c_pre_compute_of(rf,bf)
		     cpp.put_string(fz_pco_comment)
		  end
	       end
	    end
	    i := i - 1
	 end
	 echo.print_count(fz_04,memory.count)
      end

   std_output_flush_atexit: RUN_FEATURE
	 -- To be called by the atexit() function if any.

   rf_frozen_general(rf_name: STRING): RUN_FEATURE is
         -- One one the registered functions corresponding to the "std_output"
         -- GENERAL feature. All are equivalent, returning the first is well
         -- enough.
      require
         frozen_general.fast_has(rf_name)
      local
         i: INTEGER
         rf: RUN_FEATURE
      do
         from
            i := function_list.lower
         until
            Result /= Void or else i > function_list.upper
         loop
            rf := function_list.item(i)
            if rf.name.to_string = rf_name and then rf.base_feature.base_class.name.to_string = as_general then
               Result := rf
            end
            i := i + 1
         end
      end

   frozen_general: ARRAY[STRING] is
      once
         Result := <<as_std_error, as_std_input, as_io, as_std_output>>
      end

feature {RUN_FEATURE} -- For `compile_to_c':

   c_define_o_flag(rf: RUN_FEATURE) is
	 -- Add the definition/initialization of the corresponding
	 -- `o_flag' if not yet done.
      require
	 rf.is_once_routine
      local
	 bf: E_FEATURE
	 bcbf: BASE_CLASS
	 flag: STRING
      do
         if not smart_eiffel.scoop or else rf.result_type.is_separate then
            bf := rf.base_feature
            bcbf := bf.base_class
            flag := o_flag(bf)
            if not bcbf.once_flag(flag) then
               buffer.copy(once "int ")
               buffer.append(flag)
               cpp.put_extern2(buffer,'0')
            end
         end
      end

   c_define_o_result(rf: RUN_FEATURE) is
      require
	 rf.is_once_function
      local
	 bf: E_FEATURE; bcbf: BASE_CLASS; oresult: STRING; rt: E_TYPE
      do
         if not smart_eiffel.scoop or else rf.result_type.is_separate then
            bf := rf.base_feature
            bcbf := bf.base_class
            oresult := o_result(bf)
            if not bcbf.once_flag(oresult) then
               buffer.clear
               rt := rf.result_type
               if rt.is_separate then
                  buffer.extend('s')
               end
               buffer.extend('T')
               if rt.is_expanded then
                  rt.id.append_in(buffer)
                  buffer.extend(' ')
               else
                  buffer.extend('0')
                  buffer.extend('*')
               end
               buffer.append(oresult)
               c_define_o_result_buffer.clear
               rt.c_initialize_in(c_define_o_result_buffer)
               cpp.put_extern5(buffer,c_define_o_result_buffer)
            end
         end
     end

   c_test_o_flag(rf: RUN_FEATURE) is
      require
	 rf.is_once_routine
      local
	 flag: STRING; rt: E_TYPE
      do
         if not smart_eiffel.scoop or else rf.result_type.is_separate then
            flag := o_flag(rf.base_feature)
            cpp.put_string(once "if(")
            cpp.put_string(flag)
            cpp.put_string(once "==0){")
            cpp.put_string(flag)
            cpp.put_string(once "=1;{%N")
         else
            rt := rf.result_type
            buffer.clear
            buffer.extend('T')
            if rt.is_expanded then
               rt.id.append_in(buffer)
            else
               buffer.extend('0')
               buffer.extend('*')
            end
            flag := o_result(rf.base_feature)
            cpp.put_string(buffer)
            cpp.put_character(' ')
            cpp.put_string(flag)
            if not rt.is_expanded then
               cpp.put_string(once "=NULL")
            end
            cpp.put_string(fz_00)
            cpp.put_string(once "[
                se_subsystem_t* self=se_current_subsystem_thread();
                if(!self->vft.is_set_once(self,"
                                ]") --"
            cpp.put_string(flag)
            cpp.put_string(once "%")){{%N")
         end
      end

   c_test_o_flag_introspect(rf: RUN_FEATURE) is
      require
	 rf.is_once_routine
      local
	 flag: STRING; rt: E_TYPE
      do
         if not smart_eiffel.scoop or else rf.result_type.is_separate then
            flag := o_flag(rf.base_feature)
            cpp.put_string(once "if(")
            cpp.put_string(flag)
            cpp.put_string(once "!=0){%N")
         else
            rt := rf.result_type
            buffer.clear
            buffer.extend('T')
            if rt.is_expanded then
               rt.id.append_in(buffer)
            else
               buffer.extend('0')
               buffer.extend('*')
            end
            flag := o_result(rf.base_feature)
            cpp.put_string(buffer)
            cpp.put_character(' ')
            cpp.put_string(flag)
            if not rt.is_expanded then
               cpp.put_string(once "=NULL")
            end
            cpp.put_string(fz_00)
            cpp.put_string(once "[
                se_subsystem_t* self=se_current_subsystem_thread();
                if(self->vft.is_set_once(self,"
                                ]") --"
            cpp.put_string(flag)
            cpp.put_string(once "%")){%N")
         end
      end

   c_return_o_result(rf: RUN_FEATURE) is
      require
	 rf.is_once_function
      local
         oresult: STRING; rt: E_TYPE
      do
         oresult := o_result(rf.base_feature)
         if smart_eiffel.scoop and then not rf.result_type.is_separate then
            rt := rf.result_type
            if rt.is_expanded then
               cpp.put_string(fz_11)
               cpp.put_string(buffer)
               cpp.put_character('*')
               cpp.put_string(once "p_")
               cpp.put_string(oresult)
               cpp.put_string(once "=memcpy(malloc(sizeof(")
               cpp.put_string(buffer)
               cpp.put_string(once ")),")
               cpp.put_string(oresult)
               cpp.put_string(once ",sizeof(")
               cpp.put_string(buffer)
               cpp.put_string(once "));%N");
            end
            cpp.put_string(once "self->vft.set_once(self,%"")
            cpp.put_string(oresult)
            cpp.put_string(once "%", ")
            if rt.is_expanded then
               cpp.put_string(once "p_")
            end
            cpp.put_string(oresult)
            cpp.put_string(once ");%N}%N")
            if rt.is_expanded then
               cpp.put_string(fz_12)
            end
            cpp.put_string(once "else {%N")
            cpp.put_string(oresult)
            cpp.put_character('=')
            if rt.is_expanded then
               cpp.put_character('*')
            end
            cpp.put_character('(')
            cpp.put_string(buffer)
            if rt.is_expanded then
               cpp.put_character('*')
            end
            cpp.put_string(once ")(self->vft.get_once(self,%"")
            cpp.put_string(oresult)
            cpp.put_string(once "%"));%N}%N")
         else
            cpp.put_string(once "}}%N")
         end
         cpp.put_string(once "return ")
         cpp.put_string(oresult)
         cpp.put_string(once ";%N}%N")
      end

feature {NONE} -- For `compile_to_jvm':

   jvm_flag_list: FIXED_ARRAY[INTEGER] is
      once
         create Result.with_capacity(32)
      end

   idx_descriptor(rt: E_TYPE): INTEGER is
      require
         rt /= Void
      do
         Result := constant_pool.idx_utf8(jvm_descriptor(rt))
      end

   idx_name_for_result(bf: E_FEATURE): INTEGER is
      require
         bf /= Void
      do
         Result := constant_pool.idx_utf8(o_result(bf))
      end

   idx_name_for_flag(bf: E_FEATURE): INTEGER is
      require
         bf /= Void
      do
         Result := constant_pool.idx_utf8(o_flag(bf))
      end

   jvm_descriptor(rt: E_TYPE): STRING is
      do
	 Result := jvm_descriptor_buffer
         Result.clear
         if rt.is_reference then
            Result.append(jvm_root_descriptor)
         else
            rt.jvm_descriptor_in(Result)
         end
      end

feature {RUN_FEATURE_6, RUN_FEATURE_11}

   once_pre_computable(once_function: ONCE_FUNCTION): BOOLEAN is
	 -- Actually, only once function may be pre-computed and it seems to be 
	 -- the right choice.
      require
         smart_eiffel.is_ready
	 once_function /= Void
      do
         if not smart_eiffel.scoop then
            if frozen_general.fast_has(once_function.first_name.to_string) then
               Result := True
	    elseif once_function.once_pre_computable then
	       Result := True
	    end
	 end
      end

feature {NONE}

   c_pre_compute_of(rf: RUN_FEATURE; bf: E_FEATURE) is
      require
	 rf.is_once_function
         rf.once_pre_computable
         cpp.on_c
      local
	 result_type: E_TYPE; local_vars: LOCAL_VAR_LIST; rf3: RUN_FEATURE_3
      do
	 if rf.require_assertion /= Void then
	    rf.require_assertion.compile_to_c
	 end
	 result_type := rf.result_type
	 if result_type.is_user_expanded then
	    rf3 := result_type.run_class.a_default_create
	    if rf3 /= Void then
	       cpp.put_proc_call_0(rf3,Void,o_result(rf.base_feature))
	    end
	 end
	 --
	 local_vars := rf.local_vars
         if local_vars /= Void then
            cpp.put_character('{')
            local_vars.c_declare(False)
         end
	 --
         if rf.routine_body /= Void then
            rf.routine_body.compile_to_c
         end
	 --
	 if rf.ensure_assertion /= Void then
	    rf.ensure_assertion.compile_to_c
         end
	 --
         if local_vars /= Void then
            cpp.put_character('}')
         end
      end

   fz_pco_comment: STRING is "/*PCO*/%N"

   singleton_memory: ONCE_ROUTINE_POOL is
      once
	 Result := Current
      end

invariant

   is_real_singleton: Current = singleton_memory

end -- ONCE_ROUTINE_POOL