{-# LANGUAGE QuasiQuotes #-}
module Futhark.CodeGen.Backends.SequentialC.Boilerplate (generateBoilerplate) where
import Futhark.CodeGen.Backends.GenericC qualified as GC
import Language.C.Quote.OpenCL qualified as C
generateBoilerplate :: GC.CompilerM op s ()
generateBoilerplate :: forall op s. CompilerM op s ()
generateBoilerplate = do
String
cfg <- forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s String
GC.publicDef String
"context_config" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|struct $id:s;|],
[C.cedecl|struct $id:s { int debugging;
int in_use;
const char *cache_fname;
};|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_config_new" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|struct $id:cfg* $id:s(void);|],
[C.cedecl|struct $id:cfg* $id:s(void) {
struct $id:cfg *cfg = (struct $id:cfg*) malloc(sizeof(struct $id:cfg));
if (cfg == NULL) {
return NULL;
}
cfg->in_use = 0;
cfg->debugging = 0;
cfg->cache_fname = NULL;
return cfg;
}|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_config_free" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|void $id:s(struct $id:cfg* cfg);|],
[C.cedecl|void $id:s(struct $id:cfg* cfg) {
assert(!cfg->in_use);
free(cfg);
}|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_config_set_debugging" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|void $id:s(struct $id:cfg* cfg, int flag);|],
[C.cedecl|void $id:s(struct $id:cfg* cfg, int detail) {
cfg->debugging = detail;
}|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_config_set_profiling" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|void $id:s(struct $id:cfg* cfg, int flag);|],
[C.cedecl|void $id:s(struct $id:cfg* cfg, int flag) {
(void)cfg; (void)flag;
}|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_config_set_logging" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|void $id:s(struct $id:cfg* cfg, int flag);|],
[C.cedecl|void $id:s(struct $id:cfg* cfg, int detail) {
// Does nothing for this backend.
(void)cfg; (void)detail;
}|]
)
([FieldGroup]
fields, [Stm]
init_fields, [Stm]
free_fields) <- forall op s. CompilerM op s ([FieldGroup], [Stm], [Stm])
GC.contextContents
String
ctx <- forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s String
GC.publicDef String
"context" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|struct $id:s;|],
[C.cedecl|struct $id:s {
struct $id:cfg* cfg;
int detail_memory;
int debugging;
int profiling;
int logging;
typename lock_t lock;
char *error;
typename lock_t error_lock;
typename FILE *log;
int profiling_paused;
$sdecls:fields
};|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_new" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|struct $id:ctx* $id:s(struct $id:cfg* cfg);|],
[C.cedecl|struct $id:ctx* $id:s(struct $id:cfg* cfg) {
assert(!cfg->in_use);
struct $id:ctx* ctx = (struct $id:ctx*) malloc(sizeof(struct $id:ctx));
if (ctx == NULL) {
return NULL;
}
ctx->cfg = cfg;
ctx->cfg->in_use = 1;
ctx->detail_memory = cfg->debugging;
ctx->debugging = cfg->debugging;
ctx->profiling = cfg->debugging;
ctx->logging = cfg->debugging;
ctx->error = NULL;
create_lock(&ctx->error_lock);
ctx->log = stderr;
create_lock(&ctx->lock);
$stms:init_fields
init_constants(ctx);
return ctx;
}|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_free" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|void $id:s(struct $id:ctx* ctx);|],
[C.cedecl|void $id:s(struct $id:ctx* ctx) {
$stms:free_fields
free_constants(ctx);
free_lock(&ctx->lock);
ctx->cfg->in_use = 0;
free(ctx);
}|]
)
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_sync" HeaderSection
GC.MiscDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|int $id:s(struct $id:ctx* ctx);|],
[C.cedecl|int $id:s(struct $id:ctx* ctx) {
(void)ctx;
return 0;
}|]
)
forall op s. Definition -> CompilerM op s ()
GC.earlyDecl [C.cedecl|static const char *tuning_param_names[0];|]
forall op s. Definition -> CompilerM op s ()
GC.earlyDecl [C.cedecl|static const char *tuning_param_vars[0];|]
forall op s. Definition -> CompilerM op s ()
GC.earlyDecl [C.cedecl|static const char *tuning_param_classes[0];|]
forall op s.
String
-> HeaderSection
-> (String -> (Definition, Definition))
-> CompilerM op s ()
GC.publicDef_ String
"context_config_set_tuning_param" HeaderSection
GC.InitDecl forall a b. (a -> b) -> a -> b
$ \String
s ->
( [C.cedecl|int $id:s(struct $id:cfg* cfg, const char *param_name, size_t param_value);|],
[C.cedecl|int $id:s(struct $id:cfg* cfg, const char *param_name, size_t param_value) {
(void)cfg; (void)param_name; (void)param_value;
return 1;
}|]
)