1.学习内置函数编写方法。
verisys.cc文件内如下内容
s_tfcell verisystfs[] = {
/*** Template for an entry:
{ usertask|userfunction, data, checktf(), sizetf(), calltf(), misctf(),
"$tfname", forwref?, Vtool?, ErrMsg? },
Example:
{ usertask, 0, my_check, 0, my_func, my_misctf, "$my_task" },
***/
/*** add user entries here ***/
{usertask, 0, timeformatcheck, 0, timeformatcall, 0, "$timeformat"},
{usertask, 0, showallinstances_call, 0, showallinstances_call, 0,
"$showallinstances"},
{usertask, 0, pcheck, 0, p, pmisc, "$p"},
{usertask, 1, mon_check, 0, mon_call, mon_misc, "$my_monitor"},
{usertask, 0, dumpstructure_call, 0, dumpstructure_call,
dumpstructure_call,
"$$dumpstructure"},
{usertask, 0, 0, 0, abort_call, 0, "$$abort"},
{usertask, 0, 0, 0, setvalue_call, 0, "$$testsetvalue"},
{usertask, 0, sdf_check, 0, sdf_call, sdf_misc, "$sdf_annotate"},
{userfunction, 0, dist_uniform, dist_uniform, dist_uniform,
0, "$dist_uniform"},
{userfunction, 0, dist_normal, dist_normal, dist_normal,
0, "$dist_normal"},
{userfunction, 0, dist_exponential, dist_exponential, dist_exponential,
0, "$dist_exponential"},
{userfunction, 0, dist_poisson, dist_poisson, dist_poisson, 0,
"$dist_poisson"},
{userfunction, 0, dist_chi_square, dist_chi_square, dist_chi_square,
0, "$dist_chi_square"},
{userfunction, 0, dist_t, dist_t, dist_t, 0, "$dist_t"},
{userfunction, 0, dist_erlang, dist_erlang, dist_erlang, 0, "$dist_erlang"},
{0} /*** final entry must be 0 ***/
};
我们分析一下 dist_poisson,函数的内容
....
{userfunction, 0, dist_poisson, dist_poisson, dist_poisson, 0, "$dist_poisson"},
....
这个函数实现在 random.cc 文件中,实现如下
/********************************************
* dist_poisson
*******************************************/
1.int dist_poisson(int user, int reason)
2.{
3. const char name[] = "dist_poisson";
4. const int ARG_COUNT = 2;
5. int result = 0;
6. int argCount = tf_nump();
7. handle args[ARG_COUNT];
8. acc_initialize();
9. switch( reason ) {
10. case reason_sizetf:
11. result = 32;
12. break;
13. case reason_checktf: {
14. if( argCount != ARG_COUNT ) {
15. tf_error("illegal number of arguments to %s", name);
16. }
17. for (int i = 1; i <= argCount; i++) {
18. args[i-1] = acc_handle_tfarg(i);
19. if( acc_error_flag ) {
20. tf_error("illegal argument #%d to %s", i, name);
21. }
22. }
23. if( acc_fetch_type(args[0]) != accReg &&
24. acc_fetch_type(args[0]) != accTimeVar &&
25 acc_fetch_type(args[0]) != accIntVar ) {
26. tf_error("illegal argument 0 to %s", name);
27. }
28. } break;
29. case reason_calltf: {
30. int seed = acc_fetch_tfarg_int(1);
31. int mean = acc_fetch_tfarg_int(2);
32. int value = rtl_dist_poisson(&seed, mean);
33. tf_putp(1,seed);
34. tf_putp(0,value);
35. } break;
36. }
37. acc_close();
38. return result;
39.}
这个函数声明,int dist_poisson(int user, int reason)
第一个参数,什么作用,还没有搞明白,不过对比多个内置系统函数,没有显示发现对此参数引用
第二个参数,根据函数实现,猜测调用类型,是int (*checktf) (int, int); 、int (*sizetf) (int, int); 、int (*calltf) (int, int); 、int (*misctf) (int, int);对应常量
reason_checktf 对应 checktf
reason_sizetf 对应 sizetf
reason_calltf 对应 calltf
其中第6行 int argCount = tf_nump();
tf_nump(), 返回函数参数的个数