Skip to content

Syntax: Hostcode

fabianheyer edited this page Jun 1, 2022 · 2 revisions

There are several ways to add hostcode to your models.

External References (hostcode#10)

You can declare a new object as external reference like before with (internal) references. Use the keyword extern to mark it as extern. Then use a string to set the name of the function and an object which you will use to reference this hostcode snippet. As it is handled as a call, you can add parameters and they will be recognized by the dependency analysis.

Additionally, you can annotate your code snippets with the name of the code generator (e.g. C, Java) to tell the code generation which one should be used when. See the example on the right for more details.

Function Calls

While an External Reference is the preferred way to declare a hostcode call, you can call any function without declaring it first. However, due to grammar limitations and platform-dependency this older features is considered deprecated. You should really use external references.

Nevertheless, you can still use this feature by using the extern keyword directly inside the expression (e.g. r = extern gettimeofday()) without declaring the call beforehand.

The dependency analysis will consider parameters.

Legacy Hostcode (hostcode#30)

You can simply place the host code in accent grave (`) quotes. The code is put as it is into the final code. There is no further processing and also no parameter list. You can of course put the parameters inside the host code quotes, but they will be copied as they are.

Host Types (hostcode#40)

You can create variables of non-built-in types by using host declarations. The type is given as a string that will be used in code generation.


C Example files

Related Synatx

There are pragmas that allow to include host code snippets, such as includes and imports, in the generated code (#hostcode). As well as ways to include header files or libraries in the simulation process to enable linking and execution of the SCCharts program (#resource). See: Annotations and Pragmas

Due to the introduction of object-orientation in SCCharts, there is also support for host classes and structs. See: Object Orientation

scchart hostcode#10 {
  output float r
  extern @C "rand", 
         @Java "Math.random"  rand
    
    entry do rand(0)
        
    initial state init 
    do r = rand() go to init
}
scchart hostcode#30 {
    entry do `srand();`
    
    initial state init
}
scchart hostcode#40 {
    host "int64" number

    initial state init
}