PreviousUpNext

15.4.238  src/lib/compiler/back/low/display/lowhalf-format-instruction-g.pkg

#
# This just provide a very simple pretty printing function.
# It is used for visualization.
#
# -- Allen Leung 

# Compiled by:
#     src/lib/compiler/back/low/lib/visual.lib



api Format_Instruction {
    #
    package mcf:  Machcode_Form;                                        # Machcode_Form         is from   src/lib/compiler/back/low/code/machcode-form.api

    to_string
        :
        note::Notes
        ->
        mcf::Machine_Op
        ->
        String;
};

# This generic is invoked (only) in:
#     src/lib/compiler/back/low/display/machcode-controlflow-graph-viewer-g.pkg
#     src/lib/compiler/back/low/glue/lowhalf-glue.pkg

generic package   format_instruction_g   (
    #             ====================
    #
    asm:  Machcode_Codebuffer                                           # Machcode_Codebuffer   is from   src/lib/compiler/back/low/emit/machcode-codebuffer.api
)
: (weak)  Format_Instruction                                            # Format_Instruction    is from   src/lib/compiler/back/low/display/lowhalf-format-instruction-g.pkg
{
    # Export to client packages:
    #   
    package mcf = asm::mcf;                                             # "mcf"  == "Machcode_Form" (abstract machine code).

    fun to_string  an  op
        =
        strip_nl text
        where

            buffer =   string_outstream::make_stream_buf();
            sss    =   string_outstream::open_string_out buffer;

            asm_stream::with_stream  sss
                #
                (asm::make_codebuffer an).put_op
                op;

            text   = string_outstream::get_string buffer;

            fun is_space ' '  =>   TRUE;
                is_space '\t' =>   TRUE;
                is_space _    =>   FALSE;
            end;

            text = fold_backward
                       fn (x, "") =>  x;
                          (x,  y) =>  x + " " + y;
                       end
                       ""
                       (string::tokens is_space text);

            fun strip_nl ""
                    =>
                    "";

                strip_nl s
                    =>
                    f (size s - 1)
                    where

                        fun f (0) => "";

                            f (i) => case (string::get (s, i))

                                          '\n' => f (i - 1);
                                          ' '  => f (i - 1);
                                          _    => string::extract (s, 0, THE (i+1));
                                     esac;
                        end;
                    end;
            end;  
         end;
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext