root/branches/mcstas-1.x/mcstas.h

Revision 2027, 17.1 KB (checked in by farhi, 5 years ago)

New %include "instr" grammar/mechanism to include an instrument in an other, spreading bits by section. To avoid instance name clashes, the syntax
INSTRUMENT COMPONENT is required to make a component not includable (e.g.
Origin, Source, Progress_bar...). In master instrument, only components *after*
the include are ignored if specified as INSTRUMENT COMPONENT (e.g. Virtual_output or unwanted detectors).
The included first components should be RELATIVE PREVIOUS.

Line 
1/*******************************************************************************
2*
3* McStas, neutron ray-tracing package
4*         Copyright 1997-2002, All rights reserved
5*         Risoe National Laboratory, Roskilde, Denmark
6*         Institut Laue Langevin, Grenoble, France
7*
8* Kernel: mcstas.h
9*
10* %Identification
11* Written by: K.N.
12* Date: Jul  1, 1997
13* Origin: Risoe
14* Release: McStas 1.6
15* Version: $Revision: 1.50 $
16*
17* Main header file containing declarations of external functions and
18* variables. This file is included by all modules.
19*
20* $Id: mcstas.h,v 1.50 2007-07-30 12:25:41 farhi Exp $
21*
22*******************************************************************************/
23
24#ifndef MCSTAS_H
25#define MCSTAS_H "$Revision: 1.50 $"
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <time.h>
30
31#include "port.h"
32
33#ifndef FALSE
34#define FALSE 0
35#endif
36
37#ifndef TRUE
38#define TRUE 1
39#endif
40
41#define MCSTAS_VERSION "X.Y.Z, Month Day, Year"
42
43
44/* Functions defined in memory.c */
45
46typedef struct Pool_header *Pool;
47
48void *mem(size_t);        /* Allocate memory. */
49void memfree(void *);     /* Free memory. */
50char *str_dup(char *);    /* Allocate new copy of string. */
51char *str_dup_n(char *string, int n); /* Copies only first N chars. */
52char *str_cat(char *first, ...);/* Concatenate strings to allocated string. */
53char *str_quote(char *string);  /* Quote string for inclusion in C code */
54void str_free(char *);    /* Free memory for string. */
55
56Pool pool_create(void);   /* Create pool. */
57void pool_free(Pool p);   /* Free pool and associated memory. */
58void *pool_mem(Pool p, size_t size); /* Allocate memory in pool. */
59
60
61/* Allocate memory to a pointer. If p is a pointer to type t, palloc(p) will
62   make p point to dynamically allocated memory for one element of type
63   t. Used to dynamicaaly allocate structures, eg.
64   `struct S *p; palloc(p);'. */
65#define palloc(p) ((p) = mem(sizeof(*(p))))
66
67/* Allocate an array to a pointer. If p is a pointer to type t, nalloc(p, n)
68   will make p point to a dynamically allocated array with n elements of type
69   t. */
70#define nalloc(p, n) ((p) = mem((n)*sizeof(*(p))))
71
72
73
74/* Functions defined in symtab.c */
75
76/* Structure for symbol table entries, returned by symtab_lookup() and the
77   like.  */
78struct Symtab_entry
79  {
80    char *name;
81    void *val;
82  };
83
84/* Symbol table abstract data type. */
85typedef struct Symbol_table *Symtab;
86/* Abstract handle for symbol table traversals. */
87typedef struct Symtab_position *Symtab_handle;
88
89/* Create symbol table. */
90Symtab symtab_create(void);
91/* Lookup name in symbol table. */
92struct Symtab_entry *symtab_lookup(Symtab, char *);
93/* Add name to symbol table. */
94struct Symtab_entry *symtab_add(Symtab, char *, void *);
95/* Free memory for symbol table. */
96void symtab_free(Symtab, void (*)(void *));
97/* Prepare to traverse table (in no particular order). */
98Symtab_handle symtab_iterate(Symtab s);
99/* Get next entry in a traversal. */
100struct Symtab_entry *symtab_next(Symtab_handle sh);
101/* End a traversal. */
102void symtab_iterate_end(Symtab_handle sh);
103/* get previous symtab entry */
104struct Symtab_entry *symtab_previous(Symtab st, int index);
105
106
107/* Definitions for list.c */
108
109/* Abstract data type for lists. */
110typedef struct List_header *List;
111typedef struct List_position *List_handle;
112
113List list_create(void);             /* Create list. */
114void list_add(List, void *);        /* Add element at end. */
115void list_free(List, void (*)(void *)); /* Deallocate a list. */
116int list_len(List l);               /* Get list length. */
117List_handle list_iterate(List);     /* Prepare to traverse list. */
118void *list_next(List_handle);       /* Get next element in list. */
119void list_iterate_end(List_handle); /* End list traversal. */
120
121
122/*******************************************************************************
123* Definitions for cexp.c
124*******************************************************************************/
125
126/* Type for expressions. The implementation is private and values of this type
127   must only be accessed through the proper function calls. */
128typedef struct cexp *CExp;
129
130/* Extern functions defined in cexp.c */
131CExp exp_id(char *id);    /* Make normal identifier. */
132CExp exp_extern_id(char *id); /* Make extern identifier. */
133CExp exp_number(char *n); /* Make expression from number. */
134CExp exp_string(char *s); /* Make expression from string. */
135CExp exp_ctoken(char *s); /* Make expression from generic C token */
136CExp exp_compound(int n, ...);  /* Make compound expression */
137void exp_free(CExp e);    /* Free memory for expression */
138char *exp_tostring(CExp e); /* Convert expression to string. */
139void exp_fprint(FILE *f, CExp e); /* Output an expression to file. */
140int exp_isvalue(CExp e);  /* Ask if expression is a value. */
141void exp_setlineno(CExp e, int n); /* Set line number of expression */
142int exp_getlineno(CExp e);  /* Get line number of expression, or zero */
143
144/*******************************************************************************
145* Definitions in coords.c
146*******************************************************************************/
147
148/* Type for coordinates. Public. */
149struct coords
150  {
151    double x,y,z;
152  };
153typedef struct coords Coords;
154struct coords_exp
155  {
156    CExp x,y,z;
157  };
158typedef struct coords_exp Coords_exp;
159
160/* Get all-zero coordinate. */
161Coords_exp coords_exp_origo(void);
162
163
164/*******************************************************************************
165* Definitions for position
166*******************************************************************************/
167
168/*******************************************************************************
169* A component position consists in a place and an orientation. Place is the
170* location in 3D space of the origo of the components local coordinate
171* system, and orientation is the rotation transformation that transforms the
172* global coordinate system into the component local one.
173*
174* At runtime, place is a 3-vector and orientation is a 3-by-3
175* matrix. However, at compile time the actual values are not known. Instead,
176* code is generated to compute the actual values for the position at
177* runtime.
178*******************************************************************************/
179
180struct comp_position
181  {
182    Coords_exp place;          /* (x,y,z) coordinate. */
183    struct comp_inst *place_rel;       /* Instance relative to, or NULL. */
184    Coords_exp orientation;        /* X/Y/Z rotation. */
185    struct comp_inst *orientation_rel;
186  };
187
188/* During parsing, individual structures are used for place and orientation. */
189struct comp_place
190  {
191    Coords_exp place;
192    struct comp_inst *place_rel;
193  };
194struct comp_orientation
195  {
196    Coords_exp orientation;
197    struct comp_inst *orientation_rel;
198    int isdefault;  /* True if this is a default orientation, generated
199         when no ROTATED modifier is given. */
200  };
201
202/*******************************************************************************
203* Definitions in instrument.y
204*******************************************************************************/
205
206/* Name of the file currently being parsed. */
207extern char *instr_current_filename;
208/* Line number currently being scanned. */
209extern int instr_current_line;
210/* Result from parsing instrument definition. */
211extern struct instr_def *instrument_definition;
212/* Map from names to component instances. */
213extern Symtab comp_instances;
214/* List of component instances in declaration order. */
215extern List comp_instances_list;
216/* Map from names to component group instances. */
217extern Symtab group_instances;
218/* List of component group instances in declaration order. */
219extern List group_instances_list;
220/* Map from names to embedded libraries */
221extern Symtab lib_instances;
222/* Flag set to TRUE when scanning autoloaded component definitions. */
223extern int parse_restricted;
224/* Map of already-read components. */
225extern Symtab read_components;
226/* Verbose parsing/code generation */
227extern char verbose;
228/* Will store component instance for PREVIOUS reference */
229extern struct comp_inst *previous_comp;
230/* current instance index */
231extern long comp_current_index;
232
233/* Check that component definition and setting parameters are unique. */
234void check_comp_formals(List deflist, List setlist, char *compname);
235/* Check that instrument parameters are unique. */
236void check_instrument_formals(List formallist, char *instrname);
237/* Handle assignment of actual to formal component parameters. */
238void comp_formals_actuals(struct comp_inst *comp, Symtab actuals);
239
240/* Get component definition, reading from file if necessary. */
241struct comp_def *read_component(char *name);
242
243
244/*******************************************************************************
245* Definitions in instrument.l
246*******************************************************************************/
247
248/* Prepare to run lexical analysis on new file. */
249void lex_new_file(FILE *file);
250/* Handle a new autoincluded file (uses recursive parser call). */
251void push_autoload(FILE *file);
252
253/*******************************************************************************
254* Definitions for file.c
255*******************************************************************************/
256
257
258extern char *component_pathname;
259
260/* Open file, searching the full search path. */
261FILE *open_file_search(char *name);
262/* Open component definition, searching the full search path. */
263FILE *open_component_search(char *name);
264/* Open file, searching only the system directory. */
265FILE *open_file_search_sys(char *name);
266/* Add a directory to the search path. */
267void add_search_dir(char *name);
268/* get default system directory (where libraries are) */
269char *get_sys_dir(void);
270
271
272/*******************************************************************************
273* Definitions for cogen.c
274*******************************************************************************/
275
276#define ID_PRE "mc"
277
278/* Allocate a new, empty codeblock. */
279struct code_block *codeblock_new(void);
280/* Generate code for instrument definition. */
281void cogen(char *output_name, struct instr_def *instr);
282
283
284/*******************************************************************************
285* Functions and variables defined in debug.c
286*******************************************************************************/
287
288extern int error_encountered; /* Set to 1 when print_error called. */
289
290void print_error(char *, ...);  /* Normal error messages. */
291void print_warn(int *flag, char *format, ...); /* Warning. */
292void fatal_error(char *, ...);  /* Report a fatal error and exit the program. */
293
294#ifdef DEBUG
295
296void debug_printf(char *, ...); /* Internal; use debug macro instead. */
297void debugn_printf(int, char *, ...); /* Internal; use debugn macro instead. */
298
299/*******************************************************************************
300* Debugging information. When the preprosessor flag DEBUG is defined,
301* debugging messages are printed to stderr. This uses the 'debug' macro. A
302* statement of the form debug((format, ...)) (note the double parenthesis)
303* does nothing when debugging is disabled, and outputs debugging information
304* printf-style when debigging is enabled. The macro 'debugn' takes an
305* additional argument LEVEL; a compile-time option can be used to select
306* output only up to a certain level.
307*******************************************************************************/
308
309#define debug(msg) debug_printf msg
310#define debugn(msg) debugn_printf msg
311
312/* 'Standard' debugging levels. */
313#define DEBUG_ALWAYS  0   /* Always shown (if debugging enabled). */
314#define DEBUG_HIGH   10
315#define DEBUG_MEDIUM 20
316#define DEBUG_LOW    30   /* Only shown at high debugging level. */
317
318/*******************************************************************************
319* Macro used to change the current debugging level. Useful to enable
320* high-volume debugging output in a specific part of the program.
321*******************************************************************************/
322extern int debug_current_level;
323#define debug_level(n) (debug_current_level = (n))
324
325#else  /* !defined(DEBUG) */
326
327#define debug(msg)
328#define debugn(msg)
329#define DEBUG_ALWAYS
330#define DEBUG_HIGH
331#define DEBUG_MEDIUM
332#define DEBUG_LOW
333#define debug_level(n)
334
335#endif /* !defined(DEBUG) */
336
337
338
339/* Common structure definitions. */
340
341/* Code blocks. */
342struct code_block
343  {
344    char *filename;   /* Name of origin source file. */
345    char *quoted_filename;  /* Same, quoted for inclusion in C code. */
346    int linenum;    /* Line number of first line. */
347    List lines;     /* List of lines (strings with \n at end). */
348  };
349
350/* Note: the enum instr_formal_types definition MUST be kept
351   synchronized with the one in mcstas-r.h. */
352enum instr_formal_types
353  {
354    instr_type_double, instr_type_int, instr_type_string
355  };
356
357/* Component formal input parameters. */
358struct comp_iformal
359  {
360    enum instr_formal_types type; /* Type (string, int, double) */
361    char *id;     /* Parameter name */
362    int isoptional;   /* True if default value is available */
363    CExp default_value;   /* Default value if isoptional is true */
364  };
365
366/* Component definitions. */
367struct comp_def
368  {
369    char *name;                     /* Component name. */
370    char *source;                   /*  Name of source file for definition */
371    int  comp_inst_number;          /* Number of this comp in the instrument  */
372    List def_par, set_par, out_par, state_par; /* Formal parameters. */
373    char **polarisation_par;        /* Polarisation state formal parameters. */
374    struct code_block *share_code;  /* Unique Declaration code (shared). */
375    struct code_block *decl_code;   /* Declaration code. */
376    struct code_block *init_code;   /* Initializeation code. */
377    struct code_block *trace_code;  /* Ray-trace simulation code. */
378    struct code_block *save_code;   /* Code executed to save data */
379    struct code_block *finally_code;    /* Code for simulation end. */
380    struct code_block *mcdisplay_code;  /* Code for drawing components. */
381  };
382
383/* Component group instances */
384struct group_inst
385  {
386    char *name;
387    int   index;
388    char *first_comp;
389    char *last_comp;
390    CExp  split;
391  };
392
393struct when_condition {
394  CExp condition;
395
396};
397/* Component instance. */
398struct comp_inst
399  {
400    char *name;     /* Instance name. */
401    char *type;     /* type of component */
402    struct comp_def *def;           /* Pointer to definition. */
403    struct comp_position *pos;      /* Component position (place & orientation). */
404    struct code_block *extend;      /* NULL or code following comp instance */
405    int    index;   /* index of comp instance */
406    struct group_inst *group;       /* NULL or group name in which comp is */
407    Symtab defpar, setpar;          /* Parameter values. */
408    List jump;      /* NULL or list of jumps to execute after trace/extend */
409    CExp when;      /* NULL or condition to execute TRACE */
410    Symtab actuals; /* save actual/given parameters for COPY */
411    CExp split;     /* NULL or number of SPLITs as an Expr */
412    int removable;  /* this comp is removed when included from an %include "instr" */
413  };
414
415/* Instrument formal parameters. */
416struct instr_formal
417  {
418    enum instr_formal_types type; /* Type (string, int, double) */
419    char *id;                     /* Parameter name */
420    int isoptional;               /* True if default value is available */
421    CExp default_value;           /* Default value if isoptional is true */
422  };
423
424/* NeXus information. NeXus is supported through the NeXus API */
425struct NXinfo
426  {
427    int any;           /* True only if any NEXUS decls. */
428    char *hdfversion;  /* may be 4 or 5, or xml, with optionally compression */
429  };
430
431/* Instrument definition. */
432struct instr_def
433  {
434    char *name;     /* Instrument name */
435    char *source;   /* Name of source file for definition */
436    char *quoted_source;      /* File name quoted for inclusion in C */
437    struct code_block *decls; /* Code for declarations */
438    struct code_block *inits; /* Code for initializations */
439    struct code_block *saves; /* Code executed to save data */
440    struct code_block *finals;/* Code for simulation end */
441    List formals;             /* List of formal parameters */
442    Symtab compmap;           /* Map of component names to instances */
443    Symtab groupmap;          /* Map of component group names */
444    List complist;            /* List of components in declaration order */
445    List grouplist;           /* List of component groups in declaration order */
446    struct NXinfo *nxinfo;    /* NeXus declarations */
447    int use_default_main;     /* If set, output a main() function */
448    int include_runtime;      /* If set, include runtime in output */
449    int enable_trace;         /* If set, enable output of neutron traces */
450    int portable;             /* If set, emit strictly portable ANSI C */
451    int polarised;            /* If set, handle neutron polarisation */
452    int has_included_instr;   /* Flag set when instruments are %included in instr */
453  };
454
455struct jump_struct
456{
457  char *target;        /* name of component to jump to */
458  int   target_index;  /* index of component to jump to */
459  CExp  condition;     /* condition for jump or number of iterations */
460  char  iterate;       /* 1:iteration, 0:single conditional jump */
461  int   index;
462};
463
464struct jump_condition
465{
466  CExp  condition;     /* condition for jump or number of iterations */
467  char  iterate;       /* true for iteration, false for single conditional jump */
468};
469
470struct jump_name
471  {
472    char *name;
473    int  index;
474  };
475
476#endif /* MCSTAS_H */
Note: See TracBrowser for help on using the browser.