home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ppcsmalleiffel / lib_se / manifest_string_pool.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  6.8 KB  |  326 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  4. --                       http://www.loria.fr/SmallEiffel
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it 
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later 
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License 
  11. -- for  more  details.  You  should  have  received a copy of the GNU General 
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class MANIFEST_STRING_POOL
  17.    --
  18.    -- Unique global object in charge of MANIFEST_STRING used.
  19.    --
  20.  
  21. inherit GLOBALS;
  22.  
  23. feature {NONE}
  24.    
  25.    ms_list: FIXED_ARRAY[MANIFEST_STRING] is
  26.       once
  27.      !!Result.with_capacity(2048);
  28.       end;
  29.  
  30.    dummy_ms_list: FIXED_ARRAY[MANIFEST_STRING] is
  31.       once
  32.      !!Result.with_capacity(16);
  33.       end;
  34.  
  35. feature
  36.  
  37.    count: INTEGER is
  38.       do
  39.      Result := ms_list.count;
  40.       end;
  41.  
  42. feature {MANIFEST_STRING}
  43.  
  44.    add_last(ms: MANIFEST_STRING) is
  45.       require
  46.      ms /= Void;
  47.      not small_eiffel.is_ready
  48.       do
  49.      check
  50.         not ms_list.has(ms)
  51.      end;
  52.      ms_list.add_last(ms);
  53.       end;
  54.    
  55. feature {C_PRETTY_PRINTER}
  56.  
  57.    used_for_inline(ms: MANIFEST_STRING) is
  58.       do
  59.      dummy_ms_list.add_last(ms);
  60.       end;
  61.  
  62.    define_se_ms is
  63.       require
  64.      small_eiffel.string_at_run_time
  65.       do
  66.      header.copy("T7*se_ms(int c,char*e)");
  67.      body.copy(fz_t7_star);
  68.      body.extend('s');
  69.      body.extend('=');
  70.      if gc_handler.is_on then
  71.         type_string.gc_call_new_in(body);
  72.         body.append(fz_00);
  73.      else
  74.         body.append("malloc(sizeof(T7));%N");
  75.         if type_string.run_class.is_tagged then
  76.            body.append("s->id=7;%N");
  77.         end;
  78.      end;
  79.      body.append(
  80.         "s->_count=c;%N%
  81.         %s->_capacity=c+1;%N%
  82.         %s->_storage=");
  83.      if gc_handler.is_on then
  84.         body.append(fz_new);
  85.         body.extend('9');
  86.      else
  87.         body.append(us_malloc);
  88.      end;
  89.      body.append(
  90.             "(c+1);%N%
  91.         %memcpy(s->_storage,e,c);%N%
  92.         %return s;");
  93.      cpp.put_c_function(header,body);
  94.      --
  95.      cpp.put_c_function("T7*e2s(char*e)",
  96.             "return se_ms(strlen(e),e);");
  97.      --
  98.      cpp.put_c_function("char*s2e(T7*s)",
  99.         "char*e=malloc(1+s->_count);%N%
  100.         %memcpy(e,s->_storage,s->_count);%N%
  101.         %e[s->_count]='\0';%N%
  102.         %return e;");
  103.       end;
  104.    
  105.    c_define is
  106.       require
  107.      cpp.on_c
  108.       local
  109.      i, j, nb: INTEGER;
  110.      ms: MANIFEST_STRING;
  111.       do
  112.      echo.print_count("Manifest String",ms_list.count);
  113.      from -- For *.h --
  114.         i := ms_list.upper;
  115.      until
  116.         i < 0
  117.      loop
  118.         ms := ms_list.item(i);
  119.         if not_dummy(ms) then
  120.            header.copy(fz_t7_star);
  121.            header.append(ms.mapping_c);
  122.            cpp.put_extern1(header);
  123.         end;
  124.         i := i - 1;
  125.      end;
  126.      from
  127.         i := ms_list.upper;
  128.         nb := 1;
  129.      until
  130.         i < 0
  131.      loop
  132.         header.copy(fz_void);
  133.         header.extend(' ');
  134.         header.append(fz_se_msi);
  135.         nb.append_in(header);
  136.         header.append(fz_c_void_args);
  137.         from 
  138.            body.clear;
  139.            j := nb_ms_per_function;
  140.         until
  141.            j = 0 or else i < 0
  142.         loop
  143.            ms := ms_list.item(i);
  144.            if not_dummy(ms) then
  145.           body.append(ms.mapping_c);
  146.           body.append("=se_ms(");
  147.           ms.count.append_in(body);
  148.           body.extend(',');
  149.           string_to_c_code(ms.to_string,body);
  150.           body.append(fz_14);
  151.            end;
  152.            j := j - 1;
  153.            i := i - 1;
  154.         end;
  155.         cpp.put_c_function(header,body);
  156.         nb := nb + 1;
  157.      end;
  158.       ensure
  159.      cpp.on_c
  160.       end;
  161.  
  162.    c_call_initialize is
  163.       require
  164.      cpp.on_c
  165.       local 
  166.      i, j, nb: INTEGER;
  167.       do
  168.      from
  169.         i := ms_list.upper;
  170.         nb := 1;
  171.      until
  172.         i < 0
  173.      loop
  174.         cpp.put_string(fz_se_msi);
  175.         cpp.put_integer(nb);
  176.         cpp.put_string(fz_c_no_args_procedure);
  177.         i := i - nb_ms_per_function;
  178.         nb := nb + 1;
  179.      end;
  180.       ensure
  181.      cpp.on_c
  182.       end;
  183.  
  184. feature {GC_HANDLER}
  185.  
  186.    gc_mark_in(str: STRING) is
  187.       local
  188.      i: INTEGER;
  189.      ms: MANIFEST_STRING;
  190.       do
  191.      from
  192.         i := ms_list.upper;
  193.      until
  194.         i < 0
  195.      loop
  196.         ms := ms_list.item(i);
  197.         if not_dummy(ms) then
  198.            str.append(fz_gc_mark);
  199.            str.extend('7');
  200.            str.extend('(');
  201.            str.append(ms.mapping_c);
  202.            str.extend(')');
  203.            str.append(fz_00);
  204.         end;
  205.         i := i - 1;
  206.      end;
  207.       end;
  208.  
  209. feature {JVM}
  210.  
  211.    jvm_define_fields is
  212.       local
  213.      cp: like constant_pool;
  214.      ms: MANIFEST_STRING;
  215.      name_idx, string_idx, i: INTEGER;
  216.       do
  217.      if not ms_list.empty then
  218.         cp := constant_pool;
  219.         string_idx := cp.idx_eiffel_string_descriptor;
  220.         from
  221.            i := ms_list.upper;
  222.         until
  223.            i < 0
  224.         loop
  225.            ms := ms_list.item(i);
  226.            if not_dummy(ms) then
  227.           name_idx := cp.idx_uft8(ms.mapping_c);
  228.           field_info.add(9,name_idx,string_idx);
  229.            end;
  230.            i := i - 1;
  231.         end;
  232.      end;
  233.       end;
  234.  
  235.    jvm_initialize_fields is
  236.       local
  237.      cp: like constant_pool;
  238.      ca: like code_attribute;
  239.      ms: MANIFEST_STRING;
  240.      i: INTEGER;
  241.       do
  242.      if not ms_list.empty then
  243.         cp := constant_pool;
  244.         ca := code_attribute;
  245.         from
  246.            i := ms_list.upper;
  247.         until
  248.            i < 0
  249.         loop
  250.            ms := ms_list.item(i);
  251.            if not_dummy(ms) then
  252.           ca.opcode_push_manifest_string(ms.to_string);
  253.           ca.opcode_putstatic(ms.fieldref_idx,-1);
  254.            end;
  255.            i := i - 1;
  256.         end;
  257.      end;
  258.       end;
  259.  
  260. feature {C_PRETTY_PRINTER}
  261.  
  262.    string_to_c_code(s: STRING; c_code: STRING) is
  263.       local
  264.      i: INTEGER;
  265.       do
  266.      c_code.extend('%"');
  267.      from  
  268.         i := 1;
  269.      until
  270.         i > s.count
  271.      loop
  272.         character_to_c_code(s.item(i),c_code);
  273.         i := i + 1;
  274.      end;
  275.      c_code.extend('%"');
  276.       end;
  277.    
  278.    character_to_c_code(c: CHARACTER; c_code: STRING) is
  279.       do
  280.      if c = '%N' then
  281.         c_code.extend('\');
  282.         c_code.extend('n');
  283.      elseif c = '\' then
  284.         c_code.extend('\');
  285.         c_code.extend('\');
  286.      elseif c = '%"' then
  287.         c_code.extend('\');
  288.         c_code.extend('%"');
  289.      elseif c = '%'' then
  290.         c_code.extend('\');
  291.         c_code.extend('%'');
  292.      elseif c.code < 32 or else 122 < c.code then
  293.         c_code.extend('\');
  294.         c.code.to_octal.append_in(c_code);
  295.         c_code.extend('%"');
  296.         c_code.extend('%"');
  297.      else
  298.         c_code.extend(c);
  299.      end;
  300.       end;
  301.    
  302.  
  303. feature {NONE}
  304.  
  305.    not_dummy(ms: MANIFEST_STRING): BOOLEAN is
  306.       do
  307.      Result := not dummy_ms_list.fast_has(ms);
  308.       end;
  309.    
  310.    header: STRING is
  311.       once
  312.      !!Result.make(32);
  313.       end;
  314.  
  315.    body: STRING is
  316.       once
  317.      !!Result.make(512);
  318.       end;
  319.  
  320.    fz_se_msi: STRING is "se_msi";
  321.  
  322.    nb_ms_per_function: INTEGER is 20;
  323.  
  324. end -- MANIFEST_STRING_POOL
  325.  
  326.