1 # Program Preprocessor {#preprocessor}
3 gclib's program downloader provides a preprocessor for DMC code. The
4 preprocessor modifies the program prior to download providing a number of
5 language features not present in native DMC code.
7 The preprocessor is invoked in the following two ways.
9 -# With both GProgramDownload() and GProgramDownloadFile() via the
10 `preprocessor` argument. Downloading code with null for the preprocessor
11 argument uses defaults.
12 -# From within DMC code via in-band preprocessor directives.
15 --------------------------------------------------------------------------------
17 ##The `preprocessor` argument
19 GProgramDownload() and GProgramDownloadFile() can be called with a string passed
20 to the `preprocessor` argument. The program will be modified based on this
21 string prior to download. See *Preprocessor Options* below for syntax.
23 --------------------------------------------------------------------------------
27 DMC code can be written with special markup to signal the preprocessor to take
28 actions prior to download.
30 For example, the following program will invoke the in-band preprocessor. The
31 specifics are described below.
34 ## Author: Zaphod Beeblebrox
35 ## Project: Total Perspective Vortex
36 //the above 4 hashmarks enable the preprocessor
37 ##option "--min 4" //use a minimum of level four compression
38 REM REM-style comments are supported at all times
47 Lines beginning with the string `REM` are removed prior to download. `REM`
48 comments are always removed regardless of whether the other preprocessor options
53 Most preprocessor statements begin with a double hash, `##`. When proceeded by
54 a space, the double hash acts like a `REM` comment.
56 When proceeded by a character other than space, `##` is interpreted as a
57 preprocessor directive. For example, see `##option` below.
59 \note Double hash lines are removed from the program only when the
60 preprocessor is enabled with a quad hash.
62 ###Quad Hash to enable
64 In order to enable the in-band preprocessor, the first two lines of the DMC
65 program must start with a double hash. This syntax of using two lines with
66 double hashmarks is called a *quad hash*.
68 Content may follow the hash marks. For example, a good code writing style is to
69 use double hash comments as a comment header showing author, project name, etc.
73 With the preprocessor enabled, C-style comments may be used with the `//` prefix.
74 These comments are very similar to `REM` comments. The primary advantage of
75 using this comment over `REM` is that `//` comments may occur anywhere in a
76 line. This is helpful for line comments such as the following.
79 SIA= 1,25,25,0<4>1 //SSI 25 bits total, all single turn, no status
82 Strings containing `//` are not interpreted as comments.
84 \note `//` comments are removed from the program only when the preprocessor
85 is enabled with a quad hash.
87 ### Preprocessor Directives
89 \note Directives are only followed when the preprocessor is enabled with a
94 The `option` directive allows passing switches directly to the preprocessor with
95 the same syntax as the `preprocessor` argument in GProgramDownload() and
96 GProgramDownloadFile(). The syntax of the `option` directive is the following.
99 ##option "{preprocessor switches}"
102 For example, the following line will disable compression in the program.
108 See *Preprocessor Options* below for other switches.
112 The `include` directive provides a way to include the contents of another DMC
113 file in the current program. This is useful for reusing code such as automatic
114 subroutines, homing operations, or controller initilization routines.
116 The contents of the file will be inserted in place of the `include` line. The
117 insertion occurs prior to code compression.
119 The syntax of the `include` directive is the following.
122 ##include "{filename}"
128 ##include "c:\galil\initialize.dmc"
129 ##include "homing.dmc"
132 To write more portable code, use the `include` directive with just the
133 file name, no absolute path. The path to find the file on the system is set
135 1. In the <a href="http://galil.com/downloads/software/gdk">Galil Design Kit</a>,
136 specify the include path in GDK's
137 <a href="http://galil.com/sw/pub/all/doc/gdk/man/settings.html">settings</a>
138 with the `--search` or `-I` switch as defined below.
139 2. When downloading code via GProgramDownload() or GProgramDownloadFile(), use
140 the `--search` or `-I` switch in the `preprocessor` argument.
141 3. Finally, if the file is in the executable search path, the file
142 will be found. However, one of the previous two options is more reliable.
146 In addition to gclib,
147 <a href="http://galil.com/downloads/software/gdk">Galil Design Kit</a> supports
148 the preprocessor. Proper preprocessor usage will be colored in the Editor's
149 syntax highlighter. If the quad hash is not present, preprocessor syntax will be
150 colored differently to indicate improper usage.
152 The preprocessor is not supported in software prior to GDK/gclib. DMC code
153 downloads using the in-band preprocessor in prior generation software (e.g.
154 GalilTools or SmartTerm) will fail with a TC code of 61, <em>Duplicate or bad label</em>.
156 --------------------------------------------------------------------------------
158 ##Preprocessor Options
160 ###Compression, `--min`, `--max`
162 * Default uses minimum compression needed to fit the program.
163 * `--max` *n* provides compression up to and including level *n*.
164 Only the necessary compression will be performed up to level *n*.
165 * `--min` *n* will compress at least up to and including *n*. *n* defined as with `--max`.
167 ####Compression Levels, *n*
169 * Level 0 (mandatory)
170 -# Remove lines beginning with `REM`.
171 -# Remove trailing semicolons.
172 -# Comment blank lines with '.
173 -# Remove white space (space/tab) in front of # (label declarations).
174 -# Remove white space after commands.
175 -# Line ends changed to carriage return.
176 -# Replace leading tabs with double space.
177 -# Replace non-leading tabs with single space.
178 -# A backslash (`\`) character on a line other than a preprocessor line will result in an error.
180 -# Remove unnecessary spaces. Strings, comments ('), and no-ops (NO) are not changed.
182 -# Remove comments (') but not no-ops (NO).
184 -# Remove no-ops (NO).
186 -# Break apart compound lines that are too long.
187 -# Compact lines of code to maximize line usage.
188 -# Use backtick to support long lines where applicable.
190 ###Code insertion, `--insert`
191 * Default begins at line zero and overwrites anything present.
192 * `--insert` *arg* invokes the insert option of the firmware's *DL* command. *arg* can be one of the following.
193 -# Line number, e.g. `100`. Program insertion will occur on the line after the line specified.
194 -# Variable name, e.g. `myvar`. Program insertion will occur on the line after the line equal to the value of the variable.
195 -# Label callout, e.g. `#mylabel`. Program insertion will occur on the line after the label.
196 -# A lone `#` symbol. Program insertion will occur on the line after the last line in the program buffer.
197 * Compression directives `--max` and `--min` are followed.
198 * All original code following the point of insertion is cleared.
199 * Not all products support the `--insert` operation, e.g. DMC-30010.
200 See the <a href="http://www.galil.com/download/comref/comall/index.html#download.html">DL</a> command for support.
203 \warning It is the user's responsibility to ensure that the code
204 will fit in the inserted location. The preprocessor will not check line numbers
205 when executing the `--insert` option.
207 ### Include Search Paths, `--search`, `-I`
208 * The `##include` directive will attempt to open its string argument
209 directly. The open will succeed if the argument is the absolute path, or if
210 the argument is in the executable's path, e.g. in the same directory.
211 * `--search` *path* allows the user to specify a directory or directories
212 to be searched for the `include` file in case the first open fails.
213 * For historical reasons, `-I` is shorthand for `--search`.
214 * Multiple directories may be specified with multiple `-I` directives.
215 * For in-band code, `-I` must be specified prior to the include.
216 * A common use for `-I` is to specify only the filename in the DMC source code
217 and use the `preprocessor` argument during download to specify the path to the
218 files. This allows the files to be moved without a change to source code.
220 -# The `##include` argument is checked first as-is.
221 -# Then each `-I` argument in the `preprocessor` argument, in the order specified.
222 -# Then `##option` directives in the DMC file, in the order specified.
223 * If the search path contains spaces, enclose the path in double quotes, escaped with a backslash. See example below.
228 ##option "-I /code/dmc/homing"
229 ##option "-I \"/code/dmc/other code\""
231 //executable's directory will be checked
232 //then c:\code\dmc\homing
233 //then c:\code\dmc\other code
236 ### Macro Definition, `--define`, `-D`
237 * `--define` provides a way to substitute one token for another. This
238 is useful for writing code that is generic until program download. Wherever
239 the token is found in code, it is substituted by the replacement. The replacement
240 occurs right before code compression.
241 * `-D` is shorthand for `--define`.
242 * The token should consist of a starting backslash character, followed by upper
243 or lower case alphanumeric characters, underscores, and an ending backslash.
244 * The common usage for this feature is to write code with a token, and then call the
245 program download with the `-D` switch.
247 In this example, an axis is defined at download time. Specifying the
248 following for the preprocessor argument
254 would cause the following code
270 This causes the *A* axis to be addressed.
272 \note The macro `\pid\` is reserved for exclusive use by GDK.
274 ### Conditional Directives, `--ifdef`, `--ifndef`
276 To specify a preprocessor directive should be executed only if a macro is
277 defined, use `--ifdef`.
280 ##option "--ifdef \minify\ --min 4" //maximally compress code if minify macro set
283 To specify a preprocessor directive should be executed only if a macro is NOT
284 defined, use `--ifndef`.
287 ##option "--ifndef \axis\ -D \axis\:A" //Default to axis A
293 * See the `preprocessor` text box in the *Editor* settings page to set the
294 desired preprocessor setting for developing in GDK's editor.