
(********************************************************************)
(*                                                                  *)
(*  pcs_act.s7i   Generate code for actions of process commands.    *)
(*  Copyright (C) 1990 - 1994, 2004 - 2014  Thomas Mertes           *)
(*                                                                  *)
(*  This file is part of the Seed7 compiler.                        *)
(*                                                                  *)
(*  This program 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 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program 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 this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const ACTION: PCS_CMP        is action "PCS_CMP";
const ACTION: PCS_CPY        is action "PCS_CPY";
const ACTION: PCS_CREATE     is action "PCS_CREATE";
const ACTION: PCS_DESTR      is action "PCS_DESTR";
const ACTION: PCS_EMPTY      is action "PCS_EMPTY";
const ACTION: PCS_EQ         is action "PCS_EQ";
const ACTION: PCS_EXIT_VALUE is action "PCS_EQ";
const ACTION: PCS_HASHCODE   is action "PCS_HASHCODE";
const ACTION: PCS_IS_ALIVE   is action "PCS_IS_ALIVE";
const ACTION: PCS_KILL       is action "PCS_KILL";
const ACTION: PCS_NE         is action "PCS_NE";
const ACTION: PCS_PIPE2      is action "PCS_PIPE2";
const ACTION: PCS_PTY        is action "PCS_PTY";
const ACTION: PCS_START      is action "PCS_START";
const ACTION: PCS_STR        is action "PCS_STR";
const ACTION: PCS_VALUE      is action "PCS_VALUE";
const ACTION: PCS_WAIT_FOR   is action "PCS_WAIT_FOR";


const proc: pcs_prototypes (inout file: c_prog) is func

  begin
    declareExtern(c_prog, "intType     pcsCmp (const const_processType, const const_processType);");
    declareExtern(c_prog, "intType     pcsCmpGeneric (const genericType, const genericType);");
    declareExtern(c_prog, "void        pcsCpy (processType *const, const processType);");
    declareExtern(c_prog, "void        pcsCpyGeneric (genericType *const, const genericType);");
    declareExtern(c_prog, "processType pcsCreate (const processType);");
    declareExtern(c_prog, "genericType pcsCreateGeneric (const genericType);");
    declareExtern(c_prog, "void        pcsDestr (const processType);");
    declareExtern(c_prog, "void        pcsDestrGeneric (const genericType);");
    declareExtern(c_prog, "boolType    pcsEq (const const_processType, const const_processType);");
    declareExtern(c_prog, "intType     pcsExitValue (const const_processType);");
    declareExtern(c_prog, "intType     pcsHashCode (const const_bigIntType);");
    declareExtern(c_prog, "boolType    pcsIsAlive (const processType);");
    declareExtern(c_prog, "void        pcsKill (const processType);");
    declareExtern(c_prog, "void        pcsPipe2 (const const_striType, const const_arrayType, fileType *, fileType *);");
    declareExtern(c_prog, "void        pcsPty (const const_striType, const const_arrayType, fileType *, fileType *);");
    declareExtern(c_prog, "processType pcsStart (const const_striType, const const_arrayType);");
    declareExtern(c_prog, "striType    pcsStr (const const_processType);");
    declareExtern(c_prog, "processType pcsValue (const const_objRefType);");
    declareExtern(c_prog, "void        pcsWaitFor (const processType);");
  end func;


const proc: process (PCS_CMP, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsCmp(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_CPY, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: c_param1 is expr_type.value;
    var expr_type: c_param3 is expr_type.value;
  begin
    process_expr(params[1], c_param1);
    c_param3.temp_num := c_param1.temp_num;
    process_expr(params[3], c_param3);
    if has_temp_values(c_param3) then
      c_expr.expr &:= "{\n";
      appendWithDiagnostic(c_param1.temp_decls, c_expr);
      appendWithDiagnostic(c_param3.temp_decls, c_expr);
      appendWithDiagnostic(c_param1.temp_assigns, c_expr);
      appendWithDiagnostic(c_param3.temp_assigns, c_expr);
    end if;
    if c_param3.result_expr <> "" then
      c_expr.expr &:= "{\n";
      setDiagnosticLine(c_expr);
      if isNormalVariable(params[1]) then
        c_expr.expr &:= "processType old_process=";
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= ";\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= "=";
      else
        c_expr.expr &:= "processType *process_ptr=&(";
        c_expr.expr &:= c_param1.expr;
        c_expr.expr &:= ");\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= "processType old_process=*process_ptr;\n";
        setDiagnosticLine(c_expr);
        c_expr.expr &:= "*process_ptr=";
      end if;
      c_expr.expr &:= c_param3.result_expr;
      c_expr.expr &:= ";\n";
      setDiagnosticLine(c_expr);
      c_expr.expr &:= "pcsDestr(old_process);\n";
      c_expr.expr &:= "}\n";
    else
      setDiagnosticLine(c_expr);
      c_expr.expr &:= "pcsCpy(&(";
      c_expr.expr &:= c_param1.expr;
      c_expr.expr &:= "), ";
      c_expr.expr &:= c_param3.expr;
      c_expr.expr &:= ");\n";
    end if;
    if has_temp_values(c_param3) then
      appendWithDiagnostic(c_param1.temp_frees, c_expr);
      appendWithDiagnostic(c_param3.temp_frees, c_expr);
      c_expr.expr &:= "}\n";
    end if;
  end func;


const proc: process (PCS_EQ, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsEq(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_EXIT_VALUE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsExitValue(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_HASHCODE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsHashCode(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_IS_ALIVE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsIsAlive(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_KILL, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "pcsKill(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ");\n";
  end func;


const proc: process (PCS_NE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "!pcsEq(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (PCS_PIPE2, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsPipe2(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[2], c_expr);
    c_expr.expr &:= ", &(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[4], c_expr);
    c_expr.expr &:= "));\n";
  end func;


const proc: process (PCS_PTY, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsPty(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ", ";
    getAnyParamToExpr(params[2], c_expr);
    c_expr.expr &:= ", &(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= "), &(";
    process_expr(params[4], c_expr);
    c_expr.expr &:= "));\n";
  end func;


const proc: process (PCS_START, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_process_result(c_expr);
    c_expr.result_expr := "pcsStart(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ", ";
    getAnyParamToResultExpr(params[2], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (PCS_STR, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    prepare_stri_result(c_expr);
    c_expr.result_expr := "pcsStr(";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (PCS_VALUE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "pcsValue(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;



const proc: process (PCS_WAIT_FOR, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    setDiagnosticLine(c_expr);
    c_expr.expr &:= "pcsWaitFor(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ");\n";
  end func;
