File: agent_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 (380 lines) | stat: -rw-r--r-- 9,360 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
-- 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 AGENT_POOL
   --
   -- Singleton object in charge of agents.
   -- This singleton is shared via the GLOBALS.`agent_pool' once function.
   --

inherit
   GLOBALS
   VISITABLE

feature {E_AGENT}

   register(e_agent: E_AGENT): INTEGER is
	 -- Registration of an "agent ..." creation call. The registered one
	 -- gets it's `creation_mold_id' as the `Result' of this function.
      local
	 model: E_AGENT
      do
	 if agent_set = Void then
	    create agent_set.make
	 end
	 model := agent_set.reference_at(e_agent)
	 if model = Void then
	    agent_set.add(e_agent)
	    Result := agent_set.count
	 else
	    Result := model.creation_mold_id
	 end
      end

feature {SMART_EIFFEL}

   falling_down is
      local
         i: INTEGER
      do
	 if agent_set /= Void then
	    from
	       i := agent_set.lower
	    until
	       i > agent_set.upper
	    loop
	       agent_set.item(i).falling_down
	       i := i + 1
	    end
	 end
      end

   c_header_pass1 is
      require
         cpp.on_h
      do
         if agent_set /= Void then
	    cpp.put_string(once "[
               typedef union _se_agent se_agent;
               typedef struct _se_agent0 se_agent0;

                                 ]")
	 end
      ensure
         cpp.on_h
      end

   c_define_missing_agent_launcher is
      local
	 i: INTEGER; agent_args: AGENT_ARGS
      do
	 from
	    i := 1
	 until
	    i > missing_call_set.count
	 loop
	    agent_args := missing_call_set.item(i)
	    agent_args.c_define_missing_agent_launcher
	    i := i + 1
	 end
      end

   customize_jvm_runtime is
      do
	 -- *** ???? ***
      end

feature {JVM}

   jvm_define_deferred_methods is
      local
	 i: INTEGER
      do
	 method_info.add_init(fz_java_lang_object)
	 from
	    i := 1
	 until
	    i > agent_call_set.count
	 loop
	    agent_call_set.item(i).jvm_define(False)
	    i := i + 1
	 end
	 method_info.finish
      end

   ajout_call_full(i:INTEGER) is
	 -- *** NOM MOCHE ***
      do
	 method_info.add_init(fz_java_lang_object)
	 agent_call_set.item(i).jvm_define(True)
	 method_info.finish
      end

feature {AGENT_INSTRUCTION, AGENT_EXPRESSION}

   register_agent_call(args: EFFECTIVE_ARG_LIST
		       rf: RUN_FEATURE): AGENT_ARGS is
	 -- Where `rf' is the `call' or the `item' feature.
      require
	 args.count = 1
      local
	 toa: TYPE_OF_AGENT
      do
	 check
	    rf.name.to_string = as_item or rf.name.to_string = as_call
	 end
	 toa ?= rf.current_type
	 create Result.make(toa, args, rf.result_type)
	 agent_call_set.add(Result)
      ensure
	 Result /= Void
      end

feature {C_PRETTY_PRINTER}

   customize_c_runtime is
      require
	 cpp.on_h
      local
	 i: INTEGER; boost: BOOLEAN; agent_args: AGENT_ARGS
      do
	 boost := ace.boost
	 if agent_set /= Void then
	    cpp.put_string(once "[
               /*The generic se_agent0 definition:*/
               struct _se_agent0{
               Tid id;
               Tid creation_mold_id;
               void*(*afp)(
				 ]")
	    if not boost then
	       cpp.put_string(once "se_dump_stack*,")
	    end
	    cpp.put_string(once "se_agent*);};%N")
	    from
	       i := agent_set.lower
	    until
	       i > agent_set.upper
	    loop
	       agent_set.item(i).c_define_type_1(boost)
	       i := i + 1
	    end
	    cpp.put_string(once "union _se_agent{T0 s0;se_agent0 u0;")
	    from
	       i := agent_set.lower
	    until
	       i > agent_set.upper
	    loop
	       agent_set.item(i).c_define_type_2
	       i := i + 1
	    end
	    cpp.put_string(once "};%N")
            cpp.sys_runtime_h_and_c(once "agents")
	    cpp.swap_on_c
	    echo.print_count(once "Agent definition wrapper", agent_set.count)
	    from
	       i := agent_set.lower
	    until
	       i > agent_set.upper
	    loop
	       agent_set.item(i).c_define_function(boost)
	       i := i + 1
	    end
	    echo.print_count(once "Agent call wrapper", agent_call_set.count)
	    from
	       i := 1
	    until
	       i > agent_call_set.count
	    loop
	       agent_args := agent_call_set.item(i)
	       agent_definition_set.add(agent_args.signature)
	       agent_args.c_define_agent_launcher
	       i := i + 1
	    end
	    cpp.swap_on_h
	 end
      ensure
	 cpp.on_h
      end

feature {GC_HANDLER}

   gc_info_in(body: STRING) is
         -- Produce C code to print GC information.
      do
         if agent_set /= Void then
	    body.append(once "[
               if(gc_info_nb_agent)
                  fprintf(SE_GCINFO,
                  "%d\tagent(s) created. (store_left=%d).\n",
                  gc_info_nb_agent,store_left_agent);

                              ]")
	 end
      end

feature {AGENT_ARGS}

   c_switch_in(buffer: STRING; type_of_agent: TYPE_OF_AGENT; rt: E_TYPE) is
      local
	 i, argi, open1_count, open2_count, id: INTEGER; e_agent: E_AGENT
	 toa: TYPE_OF_AGENT; base1, oat1, oat2: E_TYPE;
	 open1, open2: TYPE_TUPLE; match: BOOLEAN
      do
	 if agent_set /= Void then
	    from
	       i := agent_set.lower
	       base1 := type_of_agent.base
	       open1 := type_of_agent.open
	       open1_count := open1.count
	       buffer.extend('%N')
	    until
	       i > agent_set.upper
	    loop
	       e_agent := agent_set.item(i)
	       toa := e_agent.result_type
	       id := e_agent.creation_mold_id
	       -- Does this `e_agent' `match' the prototype:
	       match := False
	       open2 := toa.open; open2_count := open2.count
	       if open1_count >= open2_count then
		  -- (Because we allow to drop some effective args.)
		  from
		     match := True
		     argi := open2_count
		  until
		     not match or else argi <= 0
		  loop
		     oat1 := open1.type(argi)
		     oat2 := open2.type(argi)
		     if oat1.is_reference then
			match := oat2.is_reference
		     elseif oat1.is_a(oat2) then
		     else
			error_handler.cancel
			match := False
		     end
		     argi := argi - 1
		  end
	       end
	       if match and then rt /= Void then
		  match := toa.res /= Void
	       end
	       -- (End of `match' computation.)
	       if match then
		  buffer.append(once "case ")
		  id.append_in(buffer)
		  comment_in(buffer, toa.run_time_mark)
		  buffer.append(once ":{%N")
		  if rt /= Void then
		     buffer.append(once "return(")
		     rt.c_type_for_result_in(buffer)
		     buffer.append(fz_22)
		  end
		  buffer.append(once "((se_agent")
		  id.append_in(buffer)
		  buffer.append(once "*)a)->afp(")
		  if not ace.boost then buffer.append(once "&ds,") end
		  buffer.append(once "((/*agent*/void*)a)")
		  from
		     argi := 1
		  until
		     argi > open2_count
		  loop
		     buffer.extend(',')
		     buffer.extend('a')
		     argi.append_in(buffer)
		     argi := argi + 1
		  end
		  buffer.extend(')')
		  if rt /= Void then
		     buffer.extend(')')
                  else
                     buffer.append(once ";%Nbreak")
		  end
		  buffer.append(once ";%N}%N")
	       else
		  comment_in(buffer, toa.run_time_mark)
		  buffer.extend('%N')
	       end
	       i := i + 1
	    end
	 end
      end

   check_for_callee(agent_args: AGENT_ARGS; caller: POSITION) is
      do
	 if not agent_definition_set.has(agent_args.signature) then
	    error_handler.add_position(caller)
	    error_handler.append(
	       "There is no such agent ever defined in the live code.")
	    error_handler.print_as_warning
	    missing_call_set.add(agent_args)
	 end
      end

feature {AGENT_POOL_VISITOR}

   accept(visitor: AGENT_POOL_VISITOR) is
      do
         visitor.visit_agent_pool(Current)
      end

feature {NONE}

   agent_set: SET[E_AGENT]
	 -- The collection of already encountered agent (see `register'). 
   
   agent_call_set: SET[AGENT_ARGS] is
      once
	 create Result.make
      end

   missing_call_set: SET[AGENT_ARGS] is
      once
	 create Result.make
      end

   agent_definition_set: SET[STRING] is
      once
	 create Result.make
      end

   comment_in(buffer, type: STRING) is
      do
	 buffer.append(once "/* ")
	 buffer.append(type)
	 buffer.append(once "*/")
      end

   singleton_memory: AGENT_POOL is
      once
	 Result := Current
      end

invariant

   is_real_singleton: Current = singleton_memory

end -- AGENT_POOL