home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / manifest_string_pool.e < prev    next >
Text File  |  1999-06-05  |  10KB  |  341 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://SmallEiffel.loria.fr
  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 {MANIFEST_STRING}
  24.  
  25.    register(ms: MANIFEST_STRING): STRING is
  26.          -- To register a runnable one.
  27.          -- Choose the appropriate mangling.
  28.       require
  29.          ms /= Void;
  30.          not small_eiffel.is_ready
  31.       local
  32.          x: INTEGER;
  33.       do
  34.          header.copy("ms");
  35.          x := ms.start_position.base_class.id;
  36.          x.append_in(header);
  37.          header.extend('_');
  38.          x := ms.to_string.hash_code;
  39.          x.append_in(header);
  40.          from
  41.          until
  42.             not mangling_dictionary.has(header)
  43.          loop
  44.             header.extend('a');
  45.          end;
  46.          Result := header.twin;
  47.          mangling_dictionary.put(ms,Result);
  48.       end;
  49.  
  50. feature {SMALL_EIFFEL}
  51.  
  52.    falling_down is
  53.       do
  54.          if mangling_dictionary.count > 0 then
  55.             type_string.set_at_run_time;
  56.          end;
  57.       end;
  58.  
  59.    c_define1(string_at_run_time: BOOLEAN) is
  60.       do
  61.          if not string_at_run_time then
  62.             cpp.put_string(
  63.                "typedef struct S7 T7;%N%
  64.                %struct S7{T9 _storage;T2 _count;T2 _capacity;};%N");
  65.          end;
  66.       end;
  67.  
  68.    c_define2(string_at_run_time: BOOLEAN) is
  69.       require
  70.          cpp.on_c
  71.       local
  72.          i, j, function_count, mdc: INTEGER;
  73.          ms: MANIFEST_STRING;
  74.       do
  75.          mdc := mangling_dictionary.count;
  76.          echo.print_count("Manifest String",mdc);
  77.          cpp.starting_manifest_string_definition(mdc);
  78.          if mdc > 0 then
  79.             from -- For *.h :
  80.                i := 1;
  81.             until
  82.                i > mdc
  83.             loop
  84.                ms := mangling_dictionary.item(i);
  85.                header.copy(fz_t7_star);
  86.                header.append(ms.mangling);
  87.                cpp.put_extern1(header);
  88.                i := i + 1;
  89.             end;
  90.          end;
  91.          define_se_ms(string_at_run_time);
  92.          if mdc > 0 then
  93.             from -- For *.c :
  94.                i := 1;
  95.                function_count := 1;
  96.             until
  97.                function_count > 1 and then i > mdc
  98.             loop
  99.                header.copy(fz_void);
  100.                header.extend(' ');
  101.                header.append(fz_se_msi);
  102.                function_count.append_in(header);
  103.                header.append(fz_c_void_args);
  104.                from
  105.                   body.clear;
  106.                   j := nb_ms_per_function;
  107.                until
  108.                   j = 0 or else i > mdc
  109.                loop
  110.                   ms := mangling_dictionary.item(i);
  111.                   body.append(ms.mangling);
  112.                   body.append("=se_ms(");
  113.                   ms.count.append_in(body);
  114.                   body.extend(',');
  115.                   string_to_c_code(ms.to_string,body);
  116.                   body.append(fz_14);
  117.                   j := j - 1;
  118.                   i := i + 1;
  119.                end;
  120.                function_count := function_count + 1;
  121.                if i <= mdc then
  122.                   body.append(fz_se_msi);
  123.                   function_count.append_in(body);
  124.                   body.append(fz_c_no_args_procedure);
  125.                end;
  126.                cpp.put_c_function(header,body);
  127.             end;
  128.          end;
  129.       ensure
  130.          cpp.on_c
  131.       end;
  132.  
  133. feature {C_PRETTY_PRINTER}
  134.  
  135.    string_to_c_code(s: STRING; c_code: STRING) is
  136.       local
  137.          i: INTEGER;
  138.       do
  139.          c_code.extend('%"');
  140.          from
  141.             i := 1;
  142.          until
  143.             i > s.count
  144.          loop
  145.             character_to_c_code(s.item(i),c_code);
  146.             i := i + 1;
  147.          end;
  148.          c_code.extend('%"');
  149.       end;
  150.  
  151.    character_to_c_code(c: CHARACTER; c_code: STRING) is
  152.       do
  153.          if c = '%N' then
  154.             c_code.extend('\');
  155.             c_code.extend('n');
  156.          elseif c = '\' then
  157.             c_code.extend('\');
  158.             c_code.extend('\');
  159.          elseif c = '%"' then
  160.             c_code.extend('\');
  161.             c_code.extend('%"');
  162.          elseif c = '%'' then
  163.             c_code.extend('\');
  164.             c_code.extend('%'');
  165.          elseif c.code < 32 or else 122 < c.code then
  166.             c_code.extend('\');
  167.             c.code.to_octal.append_in(c_code);
  168.             c_code.extend('%"');
  169.             c_code.extend('%"');
  170.          else
  171.             c_code.extend(c);
  172.          end;
  173.       end;
  174.  
  175.    c_call_initialize is
  176.       require
  177.          cpp.on_c
  178.       do
  179.          if mangling_dictionary.count > 0 then
  180.             cpp.put_string(fz_se_msi);
  181.             cpp.put_character('1');
  182.             cpp.put_string(fz_c_no_args_procedure);
  183.          end;
  184.       ensure
  185.          cpp.on_c
  186.       end;
  187.  
  188. feature {PROC_CALL_1}
  189.  
  190.    used_for_c_inline(ms: MANIFEST_STRING) is
  191.       require
  192.          ms /= Void
  193.       local
  194.          mangling: STRING;
  195.       do
  196.          mangling := ms.mangling;
  197.          if mangling_dictionary.has(mangling) then
  198.             mangling_dictionary.remove(mangling);
  199.          end;
  200.       end;
  201.  
  202. feature {GC_HANDLER}
  203.  
  204.    define_manifest_string_mark is
  205.       local
  206.          i, mdc, ms_count, function_count: INTEGER;
  207.          ms: MANIFEST_STRING;
  208.       do
  209.          mdc := mangling_dictionary.count;
  210.          function_count := 1;
  211.          define_manifest_string_mark_header(function_count);
  212.          from
  213.             i := 1;
  214.          until
  215.             i > mdc
  216.          loop
  217.             if ms_count > 300 then
  218.                ms_count := 0;
  219.                function_count := function_count + 1;
  220.                cpp.put_string(fz_manifest_string_mark);
  221.                cpp.put_integer(function_count);
  222.                cpp.put_string(fz_c_no_args_procedure);
  223.                cpp.put_string(fz_12);
  224.                define_manifest_string_mark_header(function_count);
  225.             end;
  226.             ms := mangling_dictionary.item(i);
  227.             cpp.put_string("((rsoh*)(");
  228.             cpp.put_string(ms.mangling);
  229.             cpp.put_string(
  230.                "->_storage)-1)->header.magic_flag=RSOH_MARKED;%N");
  231.             ms_count := ms_count + 1;
  232.             i := i + 1;
  233.          end;
  234.          cpp.put_string(fz_12);
  235.       end;
  236.  
  237. feature {JVM}
  238.  
  239.    jvm_define_fields is
  240.       local
  241.          cp: like constant_pool;
  242.          ms: MANIFEST_STRING;
  243.          name_idx, string_idx, i, mdc: INTEGER;
  244.       do
  245.          mdc := mangling_dictionary.count;
  246.          if mdc > 0 then
  247.             cp := constant_pool;
  248.             string_idx := cp.idx_eiffel_string_descriptor;
  249.             from
  250.                i := 1;
  251.             until
  252.                i > mdc
  253.             loop
  254.                ms := mangling_dictionary.item(i);
  255.                name_idx := cp.idx_utf8(ms.mangling);
  256.                field_info.add(9,name_idx,string_idx);
  257.                i := i + 1;
  258.             end;
  259.          end;
  260.       end;
  261.  
  262.    jvm_initialize_fields is
  263.       local
  264.          cp: like constant_pool;
  265.          ca: like code_attribute;
  266.          ms: MANIFEST_STRING;
  267.          i, mdc: INTEGER;
  268.       do
  269.          mdc := mangling_dictionary.count;
  270.          if mdc > 0 then
  271.             cp := constant_pool;
  272.             ca := code_attribute;
  273.             from
  274.                i := 1;
  275.             until
  276.                i > mdc
  277.             loop
  278.                ms := mangling_dictionary.item(i);
  279.                ca.opcode_push_manifest_string(ms.to_string);
  280.                ca.opcode_putstatic(ms.fieldref_idx,-1);
  281.                i := i + 1;
  282.             end;
  283.          end;
  284.       end;
  285.  
  286. feature {NONE}
  287.  
  288.    mangling_dictionary: DICTIONARY[MANIFEST_STRING,STRING] is
  289.       once
  290.          !!Result.with_capacity(4096);
  291.       end;
  292.  
  293.    body: STRING is
  294.       once
  295.          !!Result.make(2048);
  296.       end;
  297.  
  298.    define_se_ms(string_at_run_time: BOOLEAN) is
  299.       do
  300.          header.copy("T7*se_ms(int c,char*e)");
  301.          body.copy(fz_t7_star);
  302.          body.extend('s');
  303.          body.extend('=');
  304.          gc_handler.new_manifest_string_in(body,string_at_run_time);
  305.          body.append(
  306.             "s->_count=c;%N%
  307.             %s->_capacity=c+1;%N%
  308.             %s->_storage=");
  309.          gc_handler.new_native9_in(body,string_at_run_time);
  310.          body.append(
  311.             "(c+1);%N%
  312.             %memcpy(s->_storage,e,c);%N%
  313.             %return s;");
  314.          cpp.put_c_function(header,body);
  315.          --
  316.          cpp.put_c_function("T7*se_string_from_external_copy(char*e)",
  317.             "/* Creation of an Eiffel STRING by copying C string e. */%N%
  318.             %return se_ms(strlen(e),e);");
  319.       end;
  320.  
  321.    define_manifest_string_mark_header(number: INTEGER) is
  322.       do
  323.          header.copy(fz_void);
  324.          header.extend(' ');
  325.          header.append(fz_manifest_string_mark);
  326.          number.append_in(header);
  327.          header.append(fz_c_void_args);
  328.          cpp.put_c_heading(header);
  329.       end;
  330.  
  331.    fz_manifest_string_mark: STRING is "manifest_string_mark";
  332.  
  333.    header: STRING is "................................";
  334.  
  335.    fz_se_msi: STRING is "se_msi";
  336.  
  337.    nb_ms_per_function: INTEGER is 50;
  338.  
  339. end -- MANIFEST_STRING_POOL
  340.  
  341.