1 =================================== 2 Customizing LLVMC: Reference Manual 3 =================================== 4 .. 5 This file was automatically generated by rst2html. 6 Please do not edit directly! 7 The ReST source lives in the directory 'tools/llvmc/doc'. 8 9 .. contents:: 10 11 .. raw:: html 12 13 <div class="doc_author"> 14 <p>Written by <a href="mailto:foldr (a] codedgers.com">Mikhail Glushenkov</a></p> 15 </div> 16 17 Introduction 18 ============ 19 20 LLVMC is a generic compiler driver, designed to be customizable and 21 extensible. It plays the same role for LLVM as the ``gcc`` program does for 22 GCC - LLVMC's job is essentially to transform a set of input files into a set of 23 targets depending on configuration rules and user options. What makes LLVMC 24 different is that these transformation rules are completely customizable - in 25 fact, LLVMC knows nothing about the specifics of transformation (even the 26 command-line options are mostly not hard-coded) and regards the transformation 27 structure as an abstract graph. The structure of this graph is described in 28 high-level TableGen code, from which an efficient C++ representation is 29 automatically derived. This makes it possible to adapt LLVMC for other 30 purposes - for example, as a build tool for game resources. 31 32 Because LLVMC employs TableGen_ as its configuration language, you 33 need to be familiar with it to customize LLVMC. 34 35 .. _TableGen: http://llvm.org/docs/TableGenFundamentals.html 36 37 38 Compiling with ``llvmc`` 39 ======================== 40 41 LLVMC tries hard to be as compatible with ``gcc`` as possible, 42 although there are some small differences. Most of the time, however, 43 you shouldn't be able to notice them:: 44 45 $ # This works as expected: 46 $ llvmc -O3 -Wall hello.cpp 47 $ ./a.out 48 hello 49 50 One nice feature of LLVMC is that one doesn't have to distinguish between 51 different compilers for different languages (think ``g++`` vs. ``gcc``) - the 52 right toolchain is chosen automatically based on input language names (which 53 are, in turn, determined from file extensions). If you want to force files 54 ending with ".c" to compile as C++, use the ``-x`` option, just like you would 55 do it with ``gcc``:: 56 57 $ # hello.c is really a C++ file 58 $ llvmc -x c++ hello.c 59 $ ./a.out 60 hello 61 62 On the other hand, when using LLVMC as a linker to combine several C++ 63 object files you should provide the ``--linker`` option since it's 64 impossible for LLVMC to choose the right linker in that case:: 65 66 $ llvmc -c hello.cpp 67 $ llvmc hello.o 68 [A lot of link-time errors skipped] 69 $ llvmc --linker=c++ hello.o 70 $ ./a.out 71 hello 72 73 By default, LLVMC uses ``llvm-gcc`` to compile the source code. It is also 74 possible to choose the ``clang`` compiler with the ``-clang`` option. 75 76 77 Predefined options 78 ================== 79 80 LLVMC has some built-in options that can't be overridden in the TableGen code: 81 82 * ``-o FILE`` - Output file name. 83 84 * ``-x LANGUAGE`` - Specify the language of the following input files 85 until the next -x option. 86 87 * ``-v`` - Enable verbose mode, i.e. print out all executed commands. 88 89 * ``--save-temps`` - Write temporary files to the current directory and do not 90 delete them on exit. This option can also take an argument: the 91 ``--save-temps=obj`` switch will write files into the directory specified with 92 the ``-o`` option. The ``--save-temps=cwd`` and ``--save-temps`` switches are 93 both synonyms for the default behaviour. 94 95 * ``--temp-dir DIRECTORY`` - Store temporary files in the given directory. This 96 directory is deleted on exit unless ``--save-temps`` is specified. If 97 ``--save-temps=obj`` is also specified, ``--temp-dir`` is given the 98 precedence. 99 100 * ``--check-graph`` - Check the compilation for common errors like mismatched 101 output/input language names, multiple default edges and cycles. Exit with code 102 zero if no errors were found, and return the number of found errors 103 otherwise. Hidden option, useful for debugging. 104 105 * ``--view-graph`` - Show a graphical representation of the compilation graph 106 and exit. Requires that you have ``dot`` and ``gv`` programs installed. Hidden 107 option, useful for debugging. 108 109 * ``--write-graph`` - Write a ``compilation-graph.dot`` file in the current 110 directory with the compilation graph description in Graphviz format (identical 111 to the file used by the ``--view-graph`` option). The ``-o`` option can be 112 used to set the output file name. Hidden option, useful for debugging. 113 114 * ``--help``, ``--help-hidden``, ``--version`` - These options have 115 their standard meaning. 116 117 Compiling LLVMC-based drivers 118 ============================= 119 120 It's easiest to start working on your own LLVMC driver by copying the skeleton 121 project which lives under ``$LLVMC_DIR/examples/Skeleton``:: 122 123 $ cd $LLVMC_DIR/examples 124 $ cp -r Skeleton MyDriver 125 $ cd MyDriver 126 $ ls 127 AutoGenerated.td Hooks.cpp Main.cpp Makefile 128 129 As you can see, our basic driver consists of only three files (not counting the 130 build script). ``AutoGenerated.td`` contains TableGen description of the 131 compilation graph; its format is documented in the following 132 sections. ``Hooks.cpp`` is an empty file that should be used for hook 133 definitions (see `below`__). ``Main.cpp`` is just a helper used to compile the 134 auto-generated C++ code produced from TableGen source. 135 136 __ hooks_ 137 138 The first thing that you should do is to change the ``LLVMC_BASED_DRIVER`` 139 variable in the ``Makefile``:: 140 141 LLVMC_BASED_DRIVER=MyDriver 142 143 It can also be a good idea to put your TableGen code into a file with a less 144 generic name:: 145 146 $ touch MyDriver.td 147 $ vim AutoGenerated.td 148 [...] 149 include "MyDriver.td" 150 151 If you have more than one TableGen source file, they all should be included from 152 ``AutoGenerated.td``, since this file is used by the build system to generate 153 C++ code. 154 155 To build your driver, just ``cd`` to its source directory and run ``make``. The 156 resulting executable will be put into ``$LLVM_OBJ_DIR/$(BuildMode)/bin``. 157 158 If you're compiling LLVM with different source and object directories, then you 159 must perform the following additional steps before running ``make``:: 160 161 # LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/ 162 # LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/ 163 $ mkdir $LLVMC_OBJ_DIR/examples/MyDriver/ 164 $ cp $LLVMC_SRC_DIR/examples/MyDriver/Makefile \ 165 $LLVMC_OBJ_DIR/examples/MyDriver/ 166 $ cd $LLVMC_OBJ_DIR/examples/MyDriver 167 $ make 168 169 170 Customizing LLVMC: the compilation graph 171 ======================================== 172 173 Each TableGen configuration file should include the common definitions:: 174 175 include "llvm/CompilerDriver/Common.td" 176 177 Internally, LLVMC stores information about possible source transformations in 178 form of a graph. Nodes in this graph represent tools, and edges between two 179 nodes represent a transformation path. A special "root" node is used to mark 180 entry points for the transformations. LLVMC also assigns a weight to each edge 181 (more on this later) to choose between several alternative edges. 182 183 The definition of the compilation graph (see file ``llvmc/src/Base.td`` for an 184 example) is just a list of edges:: 185 186 def CompilationGraph : CompilationGraph<[ 187 Edge<"root", "llvm_gcc_c">, 188 Edge<"root", "llvm_gcc_assembler">, 189 ... 190 191 Edge<"llvm_gcc_c", "llc">, 192 Edge<"llvm_gcc_cpp", "llc">, 193 ... 194 195 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"), 196 (inc_weight))>, 197 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"), 198 (inc_weight))>, 199 ... 200 201 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker", 202 (case (input_languages_contain "c++"), (inc_weight), 203 (or (parameter_equals "linker", "g++"), 204 (parameter_equals "linker", "c++")), (inc_weight))>, 205 ... 206 207 ]>; 208 209 As you can see, the edges can be either default or optional, where optional 210 edges are differentiated by an additional ``case`` expression used to calculate 211 the weight of this edge. Notice also that we refer to tools via their names (as 212 strings). This makes it possible to add edges to an existing compilation graph 213 without having to know about all tool definitions used in the graph. 214 215 The default edges are assigned a weight of 1, and optional edges get a weight of 216 0 + 2*N where N is the number of tests that evaluated to true in the ``case`` 217 expression. It is also possible to provide an integer parameter to 218 ``inc_weight`` and ``dec_weight`` - in this case, the weight is increased (or 219 decreased) by the provided value instead of the default 2. Default weight of an 220 optional edge can be changed by using the ``default`` clause of the ``case`` 221 construct. 222 223 When passing an input file through the graph, LLVMC picks the edge with the 224 maximum weight. To avoid ambiguity, there should be only one default edge 225 between two nodes (with the exception of the root node, which gets a special 226 treatment - there you are allowed to specify one default edge *per language*). 227 228 When multiple compilation graphs are defined, they are merged together. Multiple 229 edges with the same end nodes are not allowed (i.e. the graph is not a 230 multigraph), and will lead to a compile-time error. 231 232 To get a visual representation of the compilation graph (useful for debugging), 233 run ``llvmc --view-graph``. You will need ``dot`` and ``gsview`` installed for 234 this to work properly. 235 236 Describing options 237 ================== 238 239 Command-line options supported by the driver are defined by using an 240 ``OptionList``:: 241 242 def Options : OptionList<[ 243 (switch_option "E", (help "Help string")), 244 (alias_option "quiet", "q") 245 ... 246 ]>; 247 248 As you can see, the option list is just a list of DAGs, where each DAG is an 249 option description consisting of the option name and some properties. More than 250 one option list can be defined (they are all merged together in the end), which 251 can be handy if one wants to separate option groups syntactically. 252 253 * Possible option types: 254 255 - ``switch_option`` - a simple boolean switch without arguments, for example 256 ``-O2`` or ``-time``. At most one occurrence is allowed by default. 257 258 - ``parameter_option`` - option that takes one argument, for example 259 ``-std=c99``. It is also allowed to use spaces instead of the equality 260 sign: ``-std c99``. At most one occurrence is allowed. 261 262 - ``parameter_list_option`` - same as the above, but more than one option 263 occurrence is allowed. 264 265 - ``prefix_option`` - same as the parameter_option, but the option name and 266 argument do not have to be separated. Example: ``-ofile``. This can be also 267 specified as ``-o file``; however, ``-o=file`` will be parsed incorrectly 268 (``=file`` will be interpreted as option value). At most one occurrence is 269 allowed. 270 271 - ``prefix_list_option`` - same as the above, but more than one occurrence of 272 the option is allowed; example: ``-lm -lpthread``. 273 274 - ``alias_option`` - a special option type for creating aliases. Unlike other 275 option types, aliases are not allowed to have any properties besides the 276 aliased option name. 277 Usage example: ``(alias_option "preprocess", "E")`` 278 279 - ``switch_list_option`` - like ``switch_option`` with the ``zero_or_more`` 280 property, but remembers how many times the switch was turned on. Useful 281 mostly for forwarding. Example: when ``-foo`` is a switch option (with the 282 ``zero_or_more`` property), the command ``driver -foo -foo`` is forwarded 283 as ``some-tool -foo``, but when ``-foo`` is a switch list, the same command 284 is forwarded as ``some-tool -foo -foo``. 285 286 287 * Possible option properties: 288 289 - ``help`` - help string associated with this option. Used for ``--help`` 290 output. 291 292 - ``required`` - this option must be specified exactly once (or, in case of 293 the list options without the ``multi_val`` property, at least 294 once). Incompatible with ``optional`` and ``one_or_more``. 295 296 - ``optional`` - the option can be specified either zero times or exactly 297 once. The default for switch options. Useful only for list options in 298 conjunction with ``multi_val``. Incompatible with ``required``, 299 ``zero_or_more`` and ``one_or_more``. 300 301 - ``one_or_more`` - the option must be specified at least once. Can be useful 302 to allow switch options be both obligatory and be specified multiple 303 times. For list options is useful only in conjunction with ``multi_val``; 304 for ordinary it is synonymous with ``required``. Incompatible with 305 ``required``, ``optional`` and ``zero_or_more``. 306 307 - ``zero_or_more`` - the option can be specified zero or more times. Useful 308 to allow a single switch option to be specified more than 309 once. Incompatible with ``required``, ``optional`` and ``one_or_more``. 310 311 - ``hidden`` - the description of this option will not appear in 312 the ``--help`` output (but will appear in the ``--help-hidden`` 313 output). 314 315 - ``really_hidden`` - the option will not be mentioned in any help 316 output. 317 318 - ``comma_separated`` - Indicates that any commas specified for an option's 319 value should be used to split the value up into multiple values for the 320 option. This property is valid only for list options. In conjunction with 321 ``forward_value`` can be used to implement option forwarding in style of 322 gcc's ``-Wa,``. 323 324 - ``multi_val n`` - this option takes *n* arguments (can be useful in some 325 special cases). Usage example: ``(parameter_list_option "foo", (multi_val 326 3))``; the command-line syntax is '-foo a b c'. Only list options can have 327 this attribute; you can, however, use the ``one_or_more``, ``optional`` 328 and ``required`` properties. 329 330 - ``init`` - this option has a default value, either a string (if it is a 331 parameter), or a boolean (if it is a switch; as in C++, boolean constants 332 are called ``true`` and ``false``). List options can't have ``init`` 333 attribute. 334 Usage examples: ``(switch_option "foo", (init true))``; ``(prefix_option 335 "bar", (init "baz"))``. 336 337 .. _case: 338 339 Conditional evaluation 340 ====================== 341 342 The 'case' construct is the main means by which programmability is achieved in 343 LLVMC. It can be used to calculate edge weights, program actions and modify the 344 shell commands to be executed. The 'case' expression is designed after the 345 similarly-named construct in functional languages and takes the form ``(case 346 (test_1), statement_1, (test_2), statement_2, ... (test_N), statement_N)``. The 347 statements are evaluated only if the corresponding tests evaluate to true. 348 349 Examples:: 350 351 // Edge weight calculation 352 353 // Increases edge weight by 5 if "-A" is provided on the 354 // command-line, and by 5 more if "-B" is also provided. 355 (case 356 (switch_on "A"), (inc_weight 5), 357 (switch_on "B"), (inc_weight 5)) 358 359 360 // Tool command line specification 361 362 // Evaluates to "cmdline1" if the option "-A" is provided on the 363 // command line; to "cmdline2" if "-B" is provided; 364 // otherwise to "cmdline3". 365 366 (case 367 (switch_on "A"), "cmdline1", 368 (switch_on "B"), "cmdline2", 369 (default), "cmdline3") 370 371 Note the slight difference in 'case' expression handling in contexts of edge 372 weights and command line specification - in the second example the value of the 373 ``"B"`` switch is never checked when switch ``"A"`` is enabled, and the whole 374 expression always evaluates to ``"cmdline1"`` in that case. 375 376 Case expressions can also be nested, i.e. the following is legal:: 377 378 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...) 379 (default), ...) 380 381 You should, however, try to avoid doing that because it hurts readability. It is 382 usually better to split tool descriptions and/or use TableGen inheritance 383 instead. 384 385 * Possible tests are: 386 387 - ``switch_on`` - Returns true if a given command-line switch is provided by 388 the user. Can be given multiple arguments, in that case ``(switch_on "foo", 389 "bar", "baz")`` is equivalent to ``(and (switch_on "foo"), (switch_on 390 "bar"), (switch_on "baz"))``. 391 Example: ``(switch_on "opt")``. 392 393 - ``any_switch_on`` - Given a number of switch options, returns true if any of 394 the switches is turned on. 395 Example: ``(any_switch_on "foo", "bar", "baz")`` is equivalent to ``(or 396 (switch_on "foo"), (switch_on "bar"), (switch_on "baz"))``. 397 398 - ``parameter_equals`` - Returns true if a command-line parameter (first 399 argument) equals a given value (second argument). 400 Example: ``(parameter_equals "W", "all")``. 401 402 - ``element_in_list`` - Returns true if a command-line parameter list (first 403 argument) contains a given value (second argument). 404 Example: ``(element_in_list "l", "pthread")``. 405 406 - ``input_languages_contain`` - Returns true if a given language 407 belongs to the current input language set. 408 Example: ``(input_languages_contain "c++")``. 409 410 - ``in_language`` - Evaluates to true if the input file language is equal to 411 the argument. At the moment works only with ``command`` and ``actions`` (on 412 non-join nodes). 413 Example: ``(in_language "c++")``. 414 415 - ``not_empty`` - Returns true if a given option (which should be either a 416 parameter or a parameter list) is set by the user. Like ``switch_on``, can 417 be also given multiple arguments. 418 Examples: ``(not_empty "o")``, ``(not_empty "o", "l")``. 419 420 - ``any_not_empty`` - Returns true if ``not_empty`` returns true for any of 421 the provided options. 422 Example: ``(any_not_empty "foo", "bar", "baz")`` is equivalent to ``(or 423 (not_empty "foo"), (not_empty "bar"), (not_empty "baz"))``. 424 425 - ``empty`` - The opposite of ``not_empty``. Equivalent to ``(not (not_empty 426 X))``. Can be given multiple arguments. 427 428 - ``any_not_empty`` - Returns true if ``not_empty`` returns true for any of 429 the provided options. 430 Example: ``(any_empty "foo", "bar", "baz")`` is equivalent to ``(or 431 (not_empty "foo"), (not_empty "bar"), (not_empty "baz"))``. 432 433 - ``single_input_file`` - Returns true if there was only one input file 434 provided on the command-line. Used without arguments: 435 ``(single_input_file)``. 436 437 - ``multiple_input_files`` - Equivalent to ``(not (single_input_file))`` (the 438 case of zero input files is considered an error). 439 440 - ``default`` - Always evaluates to true. Should always be the last 441 test in the ``case`` expression. 442 443 - ``and`` - A standard logical combinator that returns true iff all of 444 its arguments return true. Used like this: ``(and (test1), (test2), 445 ... (testN))``. Nesting of ``and`` and ``or`` is allowed, but not 446 encouraged. 447 448 - ``or`` - A logical combinator that returns true iff any of its arguments 449 return true. 450 Example: ``(or (test1), (test2), ... (testN))``. 451 452 - ``not`` - Standard unary logical combinator that negates its 453 argument. 454 Example: ``(not (or (test1), (test2), ... (testN)))``. 455 456 457 Writing a tool description 458 ========================== 459 460 As was said earlier, nodes in the compilation graph represent tools, which are 461 described separately. A tool definition looks like this (taken from the 462 ``llvmc/src/Base.td`` file):: 463 464 def llvm_gcc_cpp : Tool<[ 465 (in_language "c++"), 466 (out_language "llvm-assembler"), 467 (output_suffix "bc"), 468 (command "llvm-g++ -c -emit-llvm"), 469 (sink) 470 ]>; 471 472 This defines a new tool called ``llvm_gcc_cpp``, which is an alias for 473 ``llvm-g++``. As you can see, a tool definition is just a list of properties; 474 most of them should be self-explanatory. The ``sink`` property means that this 475 tool should be passed all command-line options that aren't mentioned in the 476 option list. 477 478 The complete list of all currently implemented tool properties follows. 479 480 * Possible tool properties: 481 482 - ``in_language`` - input language name. Can be given multiple arguments, in 483 case the tool supports multiple input languages. Used for typechecking and 484 mapping file extensions to tools. 485 486 - ``out_language`` - output language name. Multiple output languages are 487 allowed. Used for typechecking the compilation graph. 488 489 - ``output_suffix`` - output file suffix. Can also be changed dynamically, see 490 documentation on `actions`__. 491 492 __ actions_ 493 494 - ``command`` - the actual command used to run the tool. You can use output 495 redirection with ``>``, hook invocations (``$CALL``), environment variables 496 (via ``$ENV``) and the ``case`` construct. 497 498 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a list of 499 input files and joins them together. Used for linkers. 500 501 - ``sink`` - all command-line options that are not handled by other tools are 502 passed to this tool. 503 504 - ``actions`` - A single big ``case`` expression that specifies how this tool 505 reacts on command-line options (described in more detail `below`__). 506 507 __ actions_ 508 509 - ``out_file_option``, ``in_file_option`` - Options appended to the 510 ``command`` string to designate output and input files. Default values are 511 ``"-o"`` and ``""``, respectively. 512 513 .. _actions: 514 515 Actions 516 ------- 517 518 A tool often needs to react to command-line options, and this is precisely what 519 the ``actions`` property is for. The next example illustrates this feature:: 520 521 def llvm_gcc_linker : Tool<[ 522 (in_language "object-code"), 523 (out_language "executable"), 524 (output_suffix "out"), 525 (command "llvm-gcc"), 526 (join), 527 (actions (case (not_empty "L"), (forward "L"), 528 (not_empty "l"), (forward "l"), 529 (not_empty "dummy"), 530 [(append_cmd "-dummy1"), (append_cmd "-dummy2")]) 531 ]>; 532 533 The ``actions`` tool property is implemented on top of the omnipresent ``case`` 534 expression. It associates one or more different *actions* with given 535 conditions - in the example, the actions are ``forward``, which forwards a given 536 option unchanged, and ``append_cmd``, which appends a given string to the tool 537 execution command. Multiple actions can be associated with a single condition by 538 using a list of actions (used in the example to append some dummy options). The 539 same ``case`` construct can also be used in the ``cmd_line`` property to modify 540 the tool command line. 541 542 The "join" property used in the example means that this tool behaves like a 543 linker. 544 545 The list of all possible actions follows. 546 547 * Possible actions: 548 549 - ``append_cmd`` - Append a string to the tool invocation command. 550 Example: ``(case (switch_on "pthread"), (append_cmd "-lpthread"))``. 551 552 - ``error`` - Exit with error. 553 Example: ``(error "Mixing -c and -S is not allowed!")``. 554 555 - ``warning`` - Print a warning. 556 Example: ``(warning "Specifying both -O1 and -O2 is meaningless!")``. 557 558 - ``forward`` - Forward the option unchanged. 559 Example: ``(forward "Wall")``. 560 561 - ``forward_as`` - Change the option's name, but forward the argument 562 unchanged. 563 Example: ``(forward_as "O0", "--disable-optimization")``. 564 565 - ``forward_value`` - Forward only option's value. Cannot be used with switch 566 options (since they don't have values), but works fine with lists. 567 Example: ``(forward_value "Wa,")``. 568 569 - ``forward_transformed_value`` - As above, but applies a hook to the 570 option's value before forwarding (see `below`__). When 571 ``forward_transformed_value`` is applied to a list 572 option, the hook must have signature 573 ``std::string hooks::HookName (const std::vector<std::string>&)``. 574 Example: ``(forward_transformed_value "m", "ConvertToMAttr")``. 575 576 __ hooks_ 577 578 - ``output_suffix`` - Modify the output suffix of this tool. 579 Example: ``(output_suffix "i")``. 580 581 - ``stop_compilation`` - Stop compilation after this tool processes its 582 input. Used without arguments. 583 Example: ``(stop_compilation)``. 584 585 586 Language map 587 ============ 588 589 If you are adding support for a new language to LLVMC, you'll need to modify the 590 language map, which defines mappings from file extensions to language names. It 591 is used to choose the proper toolchain(s) for a given input file set. Language 592 map definition looks like this:: 593 594 def LanguageMap : LanguageMap< 595 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>, 596 LangToSuffixes<"c", ["c"]>, 597 ... 598 ]>; 599 600 For example, without those definitions the following command wouldn't work:: 601 602 $ llvmc hello.cpp 603 llvmc: Unknown suffix: cpp 604 605 The language map entries are needed only for the tools that are linked from the 606 root node. A tool can have multiple output languages. 607 608 Option preprocessor 609 =================== 610 611 It is sometimes useful to run error-checking code before processing the 612 compilation graph. For example, if optimization options "-O1" and "-O2" are 613 implemented as switches, we might want to output a warning if the user invokes 614 the driver with both of these options enabled. 615 616 The ``OptionPreprocessor`` feature is reserved specially for these 617 occasions. Example (adapted from ``llvm/src/Base.td.in``):: 618 619 620 def Preprocess : OptionPreprocessor< 621 (case (not (any_switch_on "O0", "O1", "O2", "O3")), 622 (set_option "O2"), 623 (and (switch_on "O3"), (any_switch_on "O0", "O1", "O2")), 624 (unset_option "O0", "O1", "O2"), 625 (and (switch_on "O2"), (any_switch_on "O0", "O1")), 626 (unset_option "O0", "O1"), 627 (and (switch_on "O1"), (switch_on "O0")), 628 (unset_option "O0")) 629 >; 630 631 Here, ``OptionPreprocessor`` is used to unset all spurious ``-O`` options so 632 that they are not forwarded to the compiler. If no optimization options are 633 specified, ``-O2`` is enabled. 634 635 ``OptionPreprocessor`` is basically a single big ``case`` expression, which is 636 evaluated only once right after the driver is started. The only allowed actions 637 in ``OptionPreprocessor`` are ``error``, ``warning``, and two special actions: 638 ``unset_option`` and ``set_option``. As their names suggest, they can be used to 639 set or unset a given option. To set an option with ``set_option``, use the 640 two-argument form: ``(set_option "parameter", VALUE)``. Here, ``VALUE`` can be 641 either a string, a string list, or a boolean constant. 642 643 For convenience, ``set_option`` and ``unset_option`` also work with multiple 644 arguments. That is, instead of ``[(unset_option "A"), (unset_option "B")]`` you 645 can use ``(unset_option "A", "B")``. Obviously, ``(set_option "A", "B")`` is 646 only valid if both ``A`` and ``B`` are switches. 647 648 649 More advanced topics 650 ==================== 651 652 .. _hooks: 653 654 Hooks and environment variables 655 ------------------------------- 656 657 Normally, LLVMC searches for programs in the system ``PATH``. Sometimes, this is 658 not sufficient: for example, we may want to specify tool paths or names in the 659 configuration file. This can be achieved via the hooks mechanism. To write your 660 own hooks, add their definitions to the ``Hooks.cpp`` or drop a ``.cpp`` file 661 into your driver directory. Hooks should live in the ``hooks`` namespace and 662 have the signature ``std::string hooks::MyHookName ([const char* Arg0 [ const 663 char* Arg2 [, ...]]])``. They can be used from the ``command`` tool property:: 664 665 (command "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)") 666 667 To pass arguments to hooks, use the following syntax:: 668 669 (command "$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2") 670 671 It is also possible to use environment variables in the same manner:: 672 673 (command "$ENV(VAR1)/path/to/file -o $ENV(VAR2)") 674 675 To change the command line string based on user-provided options use 676 the ``case`` expression (documented `above`__):: 677 678 (command 679 (case 680 (switch_on "E"), 681 "llvm-g++ -E -x c $INFILE -o $OUTFILE", 682 (default), 683 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm")) 684 685 __ case_ 686 687 Debugging 688 --------- 689 690 When writing LLVMC-based drivers, it can be useful to get a visual view of the 691 resulting compilation graph. This can be achieved via the command line option 692 ``--view-graph`` (which assumes that Graphviz_ and Ghostview_ are 693 installed). There is also a ``--write-graph`` option that creates a Graphviz 694 source file (``compilation-graph.dot``) in the current directory. 695 696 Another useful ``llvmc`` option is ``--check-graph``. It checks the compilation 697 graph for common errors like mismatched output/input language names, multiple 698 default edges and cycles. When invoked with ``--check-graph``, ``llvmc`` doesn't 699 perform any compilation tasks and returns the number of encountered errors as 700 its status code. In the future, these checks will be performed at compile-time 701 and this option will disappear. 702 703 .. _Graphviz: http://www.graphviz.org/ 704 .. _Ghostview: http://pages.cs.wisc.edu/~ghost/ 705 706 Conditioning on the executable name 707 ----------------------------------- 708 709 For now, the executable name (the value passed to the driver in ``argv[0]``) is 710 accessible only in the C++ code (i.e. hooks). Use the following code:: 711 712 namespace llvmc { 713 extern const char* ProgramName; 714 } 715 716 namespace hooks { 717 718 std::string MyHook() { 719 //... 720 if (strcmp(ProgramName, "mydriver") == 0) { 721 //... 722 723 } 724 725 } // end namespace hooks 726 727 In general, you're encouraged not to make the behaviour dependent on the 728 executable file name, and use command-line switches instead. See for example how 729 the ``llvmc`` program behaves when it needs to choose the correct linker options 730 (think ``g++`` vs. ``gcc``). 731 732 .. raw:: html 733 734 <hr /> 735 <address> 736 <a href="http://jigsaw.w3.org/css-validator/check/referer"> 737 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" 738 alt="Valid CSS" /></a> 739 <a href="http://validator.w3.org/check?uri=referer"> 740 <img src="http://www.w3.org/Icons/valid-xhtml10-blue" 741 alt="Valid XHTML 1.0 Transitional"/></a> 742 743 <a href="mailto:foldr (a] codedgers.com">Mikhail Glushenkov</a><br /> 744 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br /> 745 746 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $ 747 </address> 748