Next: , Up: Extending simulation


5.1 CHP Function Calls

In CHP, function calls may appear in expressions or as standalone statements. Function call syntax is similar to that of C, and functions may take arbitrarily many arguments, or no arguments at all.

     defproc func(chan?(int) A, B, chan!(int) C) {
       int a, b;
       chp {
         *[ A?(a), B?(b);
            alert_me();        // alert_me is a yet undefined function
            C!(twiddle(a,b))   // twiddle is a yet undefined function
          ]
       }
     }

Definitions such as the above can be compiled (by haco) all the way through creation (haccreate) and state allocation (hacalloc) without errors. All such nonmeta (run-time) functions are only bound at run-time. A consequence of such late binding is that the types and number of parameters of function calls cannot be checked at compile time.

With the above example, instantiate some environment of sources and sinks:

     chan(int) X, Y, Z;
     func F(X, Y, Z);
     
     chp { *[X!(1)] }    // value source
     chp { *[Y!(2)] }
     chp { *[Z?]    }    // value sink

If your file is called chptest.hac, run make chptest.haco-a. This will compile, create, and allocate the state in an object file. If you only make the .haco or .haco-c object files, hacchpsim will automatically compile the object file as much as necessary.

You can now run the simulation:

     $ hacchpsim chptest.haco-a
     chpsim> watchall-events
     chpsim> run
     ...
     Eventually tries to call unbound function alert_me.error-->

Such errors can be caught earlier using the dlassertfunc command.

Next we compile a libary to provide these missing functions.