ic_cbe.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 1,305 行 · 第 1/3 页

ERL
1,305
字号
	    emit(Fd, "    if ((oe_error_code = ei_get_type(~s, "		 "oe_size_count_index, &oe_type, &~s)) < 0) {\n",		 [InBuffer, Tname]);	_ ->	    emit(Fd, "    int oe_type = 0;\n"),	    emit(Fd, "    int oe_temp = 0;\n\n"),	    emit(Fd, "    if ((oe_error_code = ei_get_type(~s, "		 "&oe_size_count_index, &oe_type, &oe_temp)) < 0) {\n",		 [InBuffer])    end,    ?emit_c_dec_rpt(Fd, "      ", "ei_get_type", []),    emit(Fd, "      return oe_error_code;\n    }\n"),    if	T#string.length == 0 ->	    ok;	true ->	    Length = ic_util:eval_c(G, N, T#string.length), 	    case CalcType of		generator ->		    emit(Fd, "  if (~s > ~s)\n",[Tname, Length]),		    emit(Fd, "    return -1;\n\n");		_ ->		    emit(Fd, "  if (oe_temp > ~s)\n",[Length]),		    emit(Fd, "    return -1;\n\n")	    end		        end,    case CalcType of	generator ->	    emit(Fd, "    if ((oe_error_code = ei_decode_string(~s, "		 "oe_size_count_index, NULL)) < 0) {\n", [InBuffer]);	_ ->	    emit(Fd, "    if ((oe_error_code = ei_decode_string(~s, "		 "&oe_size_count_index, NULL)) < 0) {\n", [InBuffer])    end,    ?emit_c_dec_rpt(Fd, "      ", "ei_decode_string", []),    emit(Fd, "      return oe_error_code;\n    }\n"),    case CalcType of	generator ->	    emit(Fd, "    oe_malloc_size = ~s;\n\n", 		 [ic_util:mk_align("oe_malloc_size + " ++ Tname ++"+1")]);	_ ->	    emit(Fd, "    oe_malloc_size = ~s;\n\n", 		 [ic_util:mk_align("oe_malloc_size + oe_temp+1")])    end;emit_malloc_size_stmt(G, N, Fd, T, InBuffer, _Align, 		      CalcType) when record(T, wstring) ->    Tname = mk_variable_name(op_variable_count),    store_tmp_decl("    int ~s = 0;\n",[Tname]),    case CalcType of	generator ->	    emit(Fd, "    if ((oe_error_code = ei_get_type(~s, "		 "oe_size_count_index, &oe_type, &~s)) < 0) {\n",		 [InBuffer, Tname]);	_ ->	    emit(Fd, "    int oe_type = 0;\n"),	    emit(Fd, "    int oe_temp = 0;\n\n"),	    emit(Fd, "    if ((oe_error_code = ei_get_type(~s, "		 "&oe_size_count_index, &oe_type, &oe_temp)) < 0) {\n",		 [InBuffer])    end,    ?emit_c_dec_rpt(Fd, "    ", "ei_get_type", []),    emit(Fd, "    return oe_error_code;\n    }\n"),    if	T#wstring.length == 0 ->	    ok;	true ->	    Length = ic_util:eval_c(G, N, T#wstring.length), 	    case CalcType of		generator ->		    emit(Fd, "  if (~s > ~s)\n",[Tname, Length]),		    emit(Fd, "    return -1;\n\n");		_ ->		    emit(Fd, "  if (oe_temp > ~s)\n",[Length]),		    emit(Fd, "    return -1;\n\n")	    end		        end,    case CalcType of	generator ->	    %% Note prefix: oe_ei 	    emit(Fd, "    if ((oe_error_code = oe_ei_decode_wstring(~s, "		 "oe_size_count_index, NULL)) < 0) {\n", [InBuffer]);	_ ->	    %% Note prefix: oe_ei 	    emit(Fd, "    if ((oe_error_code = oe_ei_decode_wstring(~s, "		 "&oe_size_count_index, NULL)) < 0) {\n", [InBuffer])    end,    ?emit_c_dec_rpt(Fd, "    ", "oe_ei_decode_wstring", []),    emit(Fd, "    return oe_error_code;\n    }\n"),    case CalcType of	generator ->	    emit(Fd, "    oe_malloc_size =\n      ~s;\n\n", 		 [ic_util:mk_align("oe_malloc_size + ((" 				   ++ Tname 				   ++"+ 1) * __OE_WCHAR_SIZE_OF__)")]);	_ ->	    emit(Fd, "    oe_malloc_size =\n      ~s;\n\n", 		 [ic_util:mk_align("oe_malloc_size + (("				   "oe_temp + 1) * __OE_WCHAR_SIZE_OF__)")])    end;emit_malloc_size_stmt(G, N, Fd, T, InBuffer, Align, CalcType) ->    case Align of	0 ->	    emit(Fd, "  oe_malloc_size += sizeof(~s);\n\n", 		 [mk_c_type(G, N, T)]);	_ -> 	    ok    end,    case normalize_type(T) of	{basic, Type} ->	    emit_malloc_size_stmt_for_basic_type(G, N, T, Fd, Type, InBuffer);	{void, _} ->	    ok;	{sequence, _, _} ->	    ok;	{_, {array, SId, _}} ->	    case CalcType of		generator ->		    emit(Fd, "    if ((oe_error_code = ~s~s(oe_env, "			 "oe_size_count_index, &oe_malloc_size)) < 0) {\n",			 [ic_util:mk_oe_name(G, "sizecalc_"), 			  ic_forms:get_id2(SId)]),		    ?emit_c_dec_rpt(Fd, "    ", "array1", []),		    emit(Fd, "    return oe_error_code;\n\n");		_ ->		    emit(Fd, "    if ((oe_error_code = ~s~s(oe_env, "			 "&oe_size_count_index, &oe_malloc_size)) < 0) {\n",			 [ic_util:mk_oe_name(G, "sizecalc_"), 			  ic_forms:get_id2(SId)]),		    ?emit_c_dec_rpt(Fd, "    ", "array2", []),		    emit(Fd, "    return oe_error_code;\n\n")	    end;	{union, UId, _, _, _} ->	    case CalcType of		generator ->		    emit(Fd, "    if ((oe_error_code = ~s~s(oe_env, "			 "oe_size_count_index, &oe_malloc_size)) < 0) {\n",			 [ic_util:mk_oe_name(G, "sizecalc_"), 			  ic_forms:get_id2(UId)]),		    ?emit_c_dec_rpt(Fd, "    ", "union1", []),		    emit(Fd, "    return oe_error_code;\n\n");		_ ->		    emit(Fd, "    if ((oe_error_code = ~s~s(oe_env, "			 "&oe_size_count_index, &oe_malloc_size)) < 0) {\n",			 [ic_util:mk_oe_name(G, "sizecalc_"), 			  ic_forms:get_id2(UId)]),		    ?emit_c_dec_rpt(Fd, "    ", "union2", []),		    emit(Fd, "    return oe_error_code;\n\n")	    end;	{struct, UId, _, _} -> %% Struct as a member in struct !	    case CalcType of		generator ->		    emit(Fd, "    if ((oe_error_code = ~s~s(oe_env, "			 "oe_size_count_index, &oe_malloc_size)) < 0) {\n",			 [ic_util:mk_oe_name(G, "sizecalc_"), 			  ic_forms:get_id2(UId)]),		    ?emit_c_dec_rpt(Fd, "    ", "struct1", []),		    emit(Fd, "    return oe_error_code;\n\n");		_ ->		    emit(Fd, "    if ((oe_error_code = ~s~s(oe_env, "			 "&oe_size_count_index, &oe_malloc_size)) < 0) {\n",			 [ic_util:mk_oe_name(G, "sizecalc_"), 			  ic_forms:get_id2(UId)]),		    ?emit_c_dec_rpt(Fd, "    ", "struct2", []),		    emit(Fd, "    return oe_error_code;\n\n")	    end;	{any, _} ->   %% Fix for any type	    emit(Fd, "    if ((oe_error_code = ei_decode_long(~s, "		 "oe_size_count_index, NULL)) < 0) {\n",		 [InBuffer]),	    ?emit_c_dec_rpt(Fd, "    ", "any", []),	    emit(Fd, "    return oe_error_code;\n    }\n");	_ ->	    ic_error:fatal_error(G, {illegal_typecode_for_c, T, N})    end.%%------------------------------------------------------------emit_malloc_size_stmt_for_basic_type(G, N, T, Fd, Type, InBuffer) ->    {Pre, DecType} = 	case Type of	    ushort ->		{"", "ulong"};	    ulong ->		{"", "ulong"};	    ulonglong ->	{"oe_", "ulonglong"};	    short ->		{"", "long"};	    long ->		{"", "long"};	    longlong ->		{"oe_", "longlong"};	    float ->		{"", "double"};	    double ->		{"", "double"};	    boolean ->		{"", "atom"};	    char ->		{"", "char"};	    wchar ->		{"oe_", "wchar"};	    octet ->		{"", "char"};	    any ->		{"", "long"}	end,    Fmt = 	"    if ((oe_error_code = ~sei_decode_~s(~s, oe_size_count_index, "	"NULL)) < 0) {\n",    emit(Fd, Fmt, [Pre, DecType, InBuffer]),    ?emit_c_dec_rpt(Fd, "      ", "~s", [DecType]),    emit(Fd, "      return oe_error_code;\n    }\n").%%------------------------------------------------------------%%    DECODING%%------------------------------------------------------------emit_decoding_stmt(G, N, Fd, T, LName, IndOp, InBuffer, Align, 		   NextPos, DecType) ->    emit_decoding_stmt(G, N, Fd, T, LName, IndOp, InBuffer, Align, 		       NextPos, DecType, []).emit_decoding_stmt(G, N, Fd, T, LName, IndOp, InBuffer, Align, NextPos,		   DecType, AllocedPars) when element(1, T) == scoped_id ->    Fmt = 	"  if ((oe_error_code = ei_decode_~s(~s, &oe_env->_iin, ~s~s)) < 0)"	" {\n",    Emit = fun(Type) ->		   emit(Fd, Fmt, [Type, InBuffer, IndOp, LName]),		   emit_dealloc_stmts(Fd, "    ", AllocedPars),		   ?emit_c_dec_rpt(Fd, "    ", "~s", [LName]),		   emit(Fd, "    return oe_error_code;\n"),		   emit(Fd, "  }\n")	   end,    case mk_c_type(G, N, T, evaluate_not) of	"erlang_pid" ->	    Emit("pid");	"erlang_port" ->	    Emit("port");	"erlang_ref" ->	    Emit("ref");	"ETERM*" ->	    Emit("term");	{enum, FSN} ->	    emit_decoding_stmt(G, N, Fd, FSN, LName, IndOp, InBuffer,			       Align, NextPos, DecType, AllocedPars);	FSN ->	    emit_decoding_stmt(G, N, Fd, FSN, LName, IndOp, InBuffer,			       Align, NextPos, DecType, AllocedPars)     end;%% XXX T is a stringemit_decoding_stmt(G, N, Fd, T, LName, IndOp, InBuffer, _Align, NextPos,		   DecType, AllocedPars)  when list(T) ->     %% Already a fullscoped name    Type = ictype:name2type(G,T),    case ictype:isBasicType(Type) of	true ->	    emit_decoding_stmt_for_basic_type(G, N, T, Fd, Type, InBuffer, IndOp, 					      LName, AllocedPars);	false ->	    case DecType of		generator ->		    emit(Fd, "  if ((oe_error_code = ~s~s(oe_env, oe_first, "			 "~s, ~s)) < 0) {\n",			 [ic_util:mk_oe_name(G, "decode_"),			  T, NextPos, LName]),		    emit_dealloc_stmts(Fd, "    ", AllocedPars),		    ?emit_c_dec_rpt(Fd, "    ", "~s", [LName]),		    emit(Fd, "    return oe_error_code;\n"),		    emit(Fd, "  }\n");		caller -> %% No malloc used, define oe_first		    emit(Fd, "    {\n"),		    emit(Fd, "      void *oe_first = NULL;\n"), 		    emit(Fd, "      int oe_outindex = 0;\n\n"),		    emit(Fd, "      if ((oe_error_code = ~s~s(oe_env, "			 "oe_first, ~s, ~s)) < 0) {\n",			 [ic_util:mk_oe_name(G, "decode_"),			  T, NextPos, LName]),		    emit_dealloc_stmts(Fd, "      ", AllocedPars),		    ?emit_c_dec_rpt(Fd, "         ", "~s", [LName]),		    emit(Fd, "        return oe_error_code;\n"),		    emit(Fd, "      }\n"),		    emit(Fd, "    }\n");		caller_dyn ->  %% Malloc used		    emit(Fd, "    {\n"),		    emit(Fd, "      int oe_outindex = 0;\n\n"),		    emit(Fd, "      if ((oe_error_code = ~s~s(oe_env, "			 "oe_first, ~s, ~s)) < 0) {\n",			 [ic_util:mk_oe_name(G, "decode_"),			  T, NextPos, LName]),		    emit_dealloc_stmts(Fd, "        ", AllocedPars),		    ?emit_c_dec_rpt(Fd, "        ", "~s", [LName]),		    emit(Fd, "        return oe_error_code;\n"),		    emit(Fd, "      }\n"),		    emit(Fd, "    }\n");		array_dyn ->  %% Malloc used		    emit(Fd, "    {\n"),		    emit(Fd, "      int oe_outindex = 0;\n\n"),		    emit(Fd, "      if ((oe_error_code = ~s~s(oe_env, "			 "oe_first, ~s, ~s)) < 0) {\n",			 [ic_util:mk_oe_name(G, "decode_"),			  T, NextPos, LName]),		    emit_dealloc_stmts(Fd, "    ", AllocedPars),		    ?emit_c_dec_rpt(Fd, "        ", "~s", [LName]),		    emit(Fd, "        return oe_error_code;\n"),		    emit(Fd, "      }\n"),		    emit(Fd, "    }\n");		array_fix_ret ->		    emit(Fd, "    {\n"),		    emit(Fd, "      int oe_outindex = 0;\n\n"),		    emit(Fd, "      if ((oe_error_code = ~s~s(oe_env, "			 "oe_first, ~s,*~s)) < 0) {\n",			 [ic_util:mk_oe_name(G, "decode_"),			  T, NextPos, LName]),		    emit_dealloc_stmts(Fd, "        ", AllocedPars),		    ?emit_c_dec_rpt(Fd, "         ", "~s", [LName]),		    emit(Fd, "        return oe_error_code;\n"),		    emit(Fd, "      }\n"),		    emit(Fd, "    }\n");		array_fix_out -> %% No malloc used, define oe_first		    emit(Fd, "    {\n"),		    emit(Fd, "      void *oe_first = NULL;\n"), 		    emit(Fd, "      int oe_outindex = 0;\n\n"),		    emit(Fd, "      if ((oe_error_code = ~s~s(oe_env, "			 "oe_first, ~s, ~s)) < 0) {\n",			 [ic_util:mk_oe_name(G, "decode_"),			  T, NextPos, LName]),		    emit_dealloc_stmts(Fd, "        ", AllocedPars),		    ?emit_c_dec_rpt(Fd, "        ", "~s", [LName]),		    emit(Fd, "        return oe_error_code;\n"),		    emit(Fd, "      }\n"),		    emit(Fd, "    }\n")	    end    end;emit_decoding_stmt(G, N, Fd, T, LName, IndOp, InBuffer, _Align, _NextPos,		   DecType, AllocedPars)  when record(T, string) ->    case DecType of	caller_dyn ->	    emit(Fd, "  if ((oe_error_code = ei_decode_string(~s, "		 "&oe_env->_iin, ~s~s)) < 0) {\n", 		 [InBuffer, IndOp, LName]),	    emit_dealloc_stmts(Fd, "    ", AllocedPars),	    ?emit_c_dec_rpt(Fd, "    ", "~s", [LName]),	    emit(Fd, "    return oe_error_code;\n"),	    emit(Fd, "  }\n");	_ ->	    emit(Fd, "  ~s~s = oe_first + *oe_outindex;\n\n", 		 [IndOp, LName]),	    emit(Fd, "  {\n"),	    emit(Fd, "    int oe_type=0;\n"),	    emit(Fd, "    int oe_string_ctr=0;\n\n"),	    emit(Fd, "    (int) ei_get_type(~s, "		 "&oe_env->_iin, &oe_type, &oe_string_ctr);\n\n",		 [InBuffer]),	    emit(Fd, "    if ((oe_error_code = ei_decode_string(~s, "		 "&oe_env->_iin, ~s~s)) < 0) {\n", 		 [InBuffer, IndOp, LName]),	    emit_dealloc_stmts(Fd, "      ", AllocedPars),	    ?emit_c_dec_rpt(Fd, "      ", "~s", [LName]),	    emit(Fd, "      return oe_error_code;\n"),	    emit(Fd, "    }\n"),	    emit(Fd, "  *oe_outindex = ~s;\n",		 [ic_util:mk_align("*oe_outindex+oe_string_ctr+1")]),	    emit(Fd, "  }\n\n")	    end;emit_decoding_stmt(G, N, Fd, T, LName, IndOp, InBuffer, _Align, _NextPos,		   DecType, AllocedPars)  when record(T, wstring) ->      case DecType of	caller_dyn ->	    %% Note prefix: oe_ei 	    emit(Fd, "  if ((oe_error_code = oe_ei_decode_wstring(~s, "		 "&oe_env->_iin, ~s~s)) < 0) {\n", 		 [InBuffer, IndOp, LName]),	    emit_dealloc_stmts(Fd, "    ", AllocedPars),	    ?emit_c_dec_rpt(Fd, "    ", "~s", [LName]),	    emit(Fd, "    return oe_error_code;\n"),	    emit(Fd, "  }/* --- */\n");		% XXX	_ ->	    emit(Fd, "  ~s~s = oe_first + *oe_outindex;\n\n", 		 [IndOp, LName]),	    emit(Fd, "  {\n"),	    emit(Fd, "    int oe_type=0;\n"),	    emit(Fd, "    int oe_string_ctr=0;\n\n"),	    emit(Fd, "    (int) ei_get_type(~s, "		 "&oe_env->_iin, &oe_type, &oe_string_ctr);\n\n",		 [InBuffer]),	    %% Note prefix: oe_ei 	    emit(Fd, "    if ((oe_error_code = oe_ei_decode_wstring(~s, "		 "&oe_env->_iin, ~s~s)) < 0) {\n", 		 [InBuffer, IndOp, LName]),	    emit_dealloc_stmts(Fd, "      ", AllocedPars),	    ?emit_c_dec_rpt(Fd, "      ", "~s", [LName]),	    emit(Fd, "      return oe_error_code;\n"),	    emit(Fd, "    }\n"),	    emit(Fd, "  *oe_outindex = ~s;\n",		 [ic_util:mk_align("*oe_outindex+oe_string_ctr+1")]),	    emit(Fd, "  }\n")	    end;emit_decoding_stmt(G, N, Fd, T, LName, IndOp, InBuffer, _Align, NextPos,		   _DecType, AllocedPars) ->    case normalize_type(T) of	{basic, Type} ->	    emit_decoding_stmt_for_basic_type(G, N, T, Fd, Type, InBuffer, IndOp, 					      LName, AllocedPars);	{void, _} ->	    emit(Fd, "  if ((oe_error_code = ei_decode_atom(~s, "		 "&oe_env->_iin, NULL)) < 0) {\n", 		 [InBuffer]),	    emit_dealloc_stmts(Fd, "    ", AllocedPars),	    ?emit_c_dec_rpt(Fd, "    ", "~s", [LName]),	    emit(Fd, "    return oe_error_code;\n"),	    emit(Fd, "  }\n");	{sequence, _, _} ->	    ok;	{_, {array, SId, Dims}} ->	    AName = ic_forms:get_id2({array, SId, Dims}),	    Ptr = "oe_out->"++AName,	    emit(Fd, "  if ((oe_error_code = ~s~s(oe_env, "		 "oe_first, ~s, ~s)) < 0) {\n",		 [ic_util:mk_oe_name(G, "decode_"),		  ic_forms:get_id2(SId), 		  NextPos, Ptr]),	    emit_dealloc_stmts(Fd, "    ", AllocedPars),	    ?emit_c_dec_rpt(Fd, "    ", "~s", [LName]),	    emit(Fd, "    return oe_error_code;\n"),	    emit(Fd, "  }\n");	{struct, _, _, _} -> %% Struct as a member in struct !  	    ok;	_ ->	    %%io:format("3 ------------> ~p~n", [T]),	    ic_error:fatal_error(G, {illegal_typecode_for_c, T, N})    end.%% XXX DecType used in two senses in this file. emit_decoding_stmt_for_basic_type(G, N, T, Fd, Type, InBuffer, IndOp, 				  LName, AllocedPars) ->    Fmt = 	"  if ((oe_error_code = ~sei_decode_~s(~s, &oe_env->_iin, "	"~s~s)) < 0) {\n",     Ret = 	"    return oe_error_code;\n"	"}\n",

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?