
(********************************************************************)
(*                                                                  *)
(*  chr_act.s7i   Generate code for actions of the type char.       *)
(*  Copyright (C) 1990 - 1994, 2004 - 2015  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: CHR_CLIT      is action "CHR_CLIT";
const ACTION: CHR_CMP       is action "CHR_CMP";
const ACTION: CHR_CONV      is action "CHR_CONV";
const ACTION: CHR_CPY       is action "CHR_CPY";
const ACTION: CHR_EQ        is action "CHR_EQ";
const ACTION: CHR_DECR      is action "CHR_DECR";
const ACTION: CHR_GE        is action "CHR_GE";
const ACTION: CHR_GT        is action "CHR_GT";
const ACTION: CHR_HASHCODE  is action "CHR_HASHCODE";
const ACTION: CHR_ICONV1    is action "CHR_ICONV1";
const ACTION: CHR_ICONV3    is action "CHR_ICONV3";
const ACTION: CHR_INCR      is action "CHR_INCR";
const ACTION: CHR_IS_LETTER is action "CHR_IS_LETTER";
const ACTION: CHR_LE        is action "CHR_LE";
const ACTION: CHR_LOW       is action "CHR_LOW";
const ACTION: CHR_LT        is action "CHR_LT";
const ACTION: CHR_NE        is action "CHR_NE";
const ACTION: CHR_ORD       is action "CHR_ORD";
const ACTION: CHR_PRED      is action "CHR_PRED";
const ACTION: CHR_STR       is action "CHR_STR";
const ACTION: CHR_SUCC      is action "CHR_SUCC";
const ACTION: CHR_UP        is action "CHR_UP";
const ACTION: CHR_VALUE     is action "CHR_VALUE";
const ACTION: CHR_WIDTH     is action "CHR_WIDTH";


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

  begin
    declareExtern(c_prog, "striType    chrCLit (charType);");
    declareExtern(c_prog, "striType    chrCLitToBuffer (charType, striType);");
    declareExtern(c_prog, "intType     chrCmp (charType, charType);");
    declareExtern(c_prog, "intType     chrCmpGeneric (const genericType, const genericType);");
    declareExtern(c_prog, "void        chrCpyGeneric (genericType *const, const genericType);");
    declareExtern(c_prog, "boolType    chrIsLetter (charType);");
    declareExtern(c_prog, "charType    chrLow (charType);");
    declareExtern(c_prog, "striType    chrStr (charType);");
    if ccConf.ALLOW_STRITYPE_SLICES then
      writeln(c_prog, "#define     chrStrMacro(ch,str) (str.size=1,str.mem=str.mem1,str.mem1[0]=(strElemType)(ch),&str)");
    else
      writeln(c_prog, "#define     chrStrMacro(ch,str) (str.size=1,str.mem[0]=(strElemType)(ch),&str)");
    end if;
    declareExtern(c_prog, "charType    chrUp (charType);");
    declareExtern(c_prog, "charType    chrValue (const const_objRefType);");
    declareExtern(c_prog, "intType     chrWidth (charType);");
  end func;


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

  local
    var string: buffer_name is "";
  begin
    if ccConf.ALLOW_STRITYPE_SLICES and c_expr.demand < ASSIGN_RESULT then
      incr(c_expr.temp_num);
      buffer_name := "buffer_" & str(c_expr.temp_num);
      c_expr.temp_decls &:= "union {\n";
      c_expr.temp_decls &:= "  struct striStruct striBuf;\n";
      c_expr.temp_decls &:= "  char charBuf[SIZ_STRI(INTTYPE_DECIMAL_SIZE)];\n";
      c_expr.temp_decls &:= "} ";
      c_expr.temp_decls &:= buffer_name;
      c_expr.temp_decls &:= ";\n";
      c_expr.expr &:= "chrCLitToBuffer(";
      process_expr(params[1], c_expr);
      c_expr.expr &:= ", &";
      c_expr.expr &:= buffer_name;
      c_expr.expr &:= ".striBuf)";
    else
      prepare_stri_result(c_expr);
      c_expr.result_expr := "chrCLit(";
      getStdParamToResultExpr(params[1], c_expr);
      c_expr.result_expr &:= ")";
    end if;
  end func;


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

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


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

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


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

  local
    var expr_type: statement is expr_type.value;
  begin
    process_expr(params[1], statement);
    statement.expr &:= "=";
    process_expr(params[3], statement);
    statement.expr &:= ";\n";
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


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

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


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

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


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

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


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

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


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

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


const proc: process_const_chr_iconv (in integer: number, inout expr_type: c_expr) is func

  begin
    incr(countOptimizations);
    if number < pred(-2147483647) or number > 2147483647 then
      warning(DOES_RAISE, "RANGE_ERROR", c_expr);
      c_expr.expr &:= intRaiseError("RANGE_ERROR");
    else
      c_expr.expr &:= "(charType) ";
      c_expr.expr &:= str(number);
    end if;
  end func;


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

  local
    var reference: evaluatedParam is NIL;
    var string: number_name is "";
  begin
    if getConstant(params[1], INTOBJECT, evaluatedParam) then
      process_const_chr_iconv(getValue(evaluatedParam, integer), c_expr);
    elsif conversion_range_check and ccConf.INTTYPE_SIZE > 32 then
      incr(countRangeChecks);
      c_expr.expr &:= "(";
      number_name := getParameterAsVariable("intType", "tmp_", params[1], c_expr);
      c_expr.expr &:= "rngChk(";
      c_expr.expr &:= number_name;
      c_expr.expr &:= "<INT32TYPE_MIN||";
      c_expr.expr &:= number_name;
      c_expr.expr &:= ">INT32TYPE_MAX)?";
      c_expr.expr &:= intRaiseError("RANGE_ERROR");
      c_expr.expr &:= ":(charType)";
      c_expr.expr &:= number_name;
      c_expr.expr &:= ")";
    else
      if ccConf.INTTYPE_SIZE > 32 then
        incr(countNoRangeChecks);
      end if;
      c_expr.expr &:= "(charType)(";
      process_expr(params[1], c_expr);
      c_expr.expr &:= ")";
    end if;
  end func;


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

  local
    var reference: evaluatedParam is NIL;
    var string: number_name is "";
  begin
    if getConstant(params[3], INTOBJECT, evaluatedParam) then
      process_const_chr_iconv(getValue(evaluatedParam, integer), c_expr);
    elsif conversion_range_check and ccConf.INTTYPE_SIZE > 32 then
      incr(countRangeChecks);
      c_expr.expr &:= "(";
      number_name := getParameterAsVariable("intType", "tmp_", params[3], c_expr);
      c_expr.expr &:= "rngChk(";
      c_expr.expr &:= number_name;
      c_expr.expr &:= "<INT32TYPE_MIN||";
      c_expr.expr &:= number_name;
      c_expr.expr &:= ">INT32TYPE_MAX)?";
      c_expr.expr &:= intRaiseError("RANGE_ERROR");
      c_expr.expr &:= ":(charType)";
      c_expr.expr &:= number_name;
      c_expr.expr &:= ")";
    else
      if ccConf.INTTYPE_SIZE > 32 then
        incr(countNoRangeChecks);
      end if;
      c_expr.expr &:= "(charType)(";
      process_expr(params[3], c_expr);
      c_expr.expr &:= ")";
    end if;
  end func;


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

  local
    var reference: evaluatedParam is NIL;
    var string: striStruct_name is "";
  begin
    if getConstant(params[1], CHAROBJECT, evaluatedParam) then
      incr(countOptimizations);
      c_expr.expr &:= "/*chrStr*/";
      c_expr.expr &:= stringLiteral(str(getValue(evaluatedParam, char)));
    elsif not c_expr.demand < REQUIRE_RESULT then
      incr(c_expr.temp_num);
      striStruct_name := "stri_" & str(c_expr.temp_num);
      c_expr.temp_decls &:= "struct striStruct ";
      c_expr.temp_decls &:= striStruct_name;
      c_expr.temp_decls &:= ";\n";
      c_expr.expr &:= "chrStrMacro(";
      process_expr(params[1], c_expr);
      c_expr.expr &:= ",";
      c_expr.expr &:= striStruct_name;
      c_expr.expr &:= ")";
    else
      c_expr.result_expr &:= "chrStr(";
      getStdParamToResultExpr(params[1], c_expr);
      c_expr.result_expr &:= ")";
    end if;
  end func;


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

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


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

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


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

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


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

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