1 @c Copyright (C) 2000, 2009 Red Hat, Inc.
2 @c This file is part of the CGEN manual.
3 @c For copying conditions, see the file cgen.texi.
5 @node Preprocessor macros
6 @chapter Preprocessor macros
7 @cindex Preprocessor macros
10 Preprocessor macros provide a way of simplifying the writing of
14 * Pmacros introduction:: Introduction to pmacros
15 * Defining pmacros:: @code{define-pmacro}
16 * Using pmacros:: Using preprocessor macros
17 * Pmacro expansion:: How pmacros are expanded
18 * Syntactic forms:: Pmacros that defer argument expansion
19 * Default argument values:: Specifying default values of arguments
20 * Multiple result statements:: Using @code{begin}
21 * Symbols and strings:: Symbols and strings
22 * Number utilities:: Manipulating numbers
23 * List utilities:: Manipulating lists
24 * Iteration utilities:: Iterating over lists
25 * Conditional macros:: Conditional execution
26 * Pmacro utilities:: Utilities for using macros
27 * Debugging utilities:: Pmacros to assist debugging
28 * Comparisons:: Comparing things
29 * Arithmetic functions:: Math
30 * Logical functions:: Shifts, bitwise logical functions
31 * Internal use pmacros:: For internal use only
34 @node Pmacros introduction
35 @section Pmacros introduction
37 The macro facility provided by CGEN is quite extensive.
38 This is to give @file{.cpu} file writers the freedom to
39 write the file in ways that work for the architecture in question.
40 Not all architectures are best described in the same way.
42 The macros are called @samp{pmacros} because the word @samp{macro}
43 has become overloaded. For clarity, we give them a unique name:
46 One important point to keep in mind regarding pmacros is that
47 when loading @file{.cpu} files all pmacros are expanded and discarded,
48 their only purpose is to simplify writing the @code{dni}s and other
49 elements of the architecture description.
51 Therefore, do not try to write RTL as pmacros.
52 You can of course use pmacros to assist in the writing of RTL,
53 but remember that the resulting RTL @emph{cannot} use pmacros.
54 By the time the RTL is processed all pmacros have been expanded
57 A simple picture may help.
58 Here is a basic diagram of the steps in processing cpu descriptions.
65 (3) define-@{insn,operand,etc.@} processing
67 (4) instruction set analysis
69 (5) application source file generation
72 Once CGEN gets to step (3) pmacros no longer exist.
74 @node Defining pmacros
75 @section Defining pmacros
78 There are two kinds of macros:
85 Preprocessor function macros are defined with:
88 (define-pmacro (name [parm1 parm2 ... parmN])
94 @samp{expansion} must be exactly one expression.
96 Preprocessor variable macros are just global variables, nothing special.
97 When invoked their value is used in place of their name.
99 Variable macros are defined with:
109 @section Using pmacros
111 Functional macros are invoked in either of two ways: positional arguments
112 or specifying arguments by name.
115 (define-pmacro (foo arg1 arg2) (bar arg1 arg2))
117 ;; Invoke by positional arguments.
119 (foo abc def) ==> (bar abc def)
121 ;; Invoke by naming arguments.
123 (foo #:arg1 ghi #:arg2 jkl) ==> (bar ghi jkl)
126 Variable macros are invoked simply by specifying their name.
129 (define-pmacro foo "abc")
131 (.str foo "def") ==> "abcdef"
134 @node Pmacro expansion
135 @section Pmacro expansion
137 Most@footnote{Syntactic form pmacros don't pre-evaluate their arguments.
138 @xref{Syntactic forms}.}
139 function pmacros are expanded by first processing any macros in the invocation,
140 binding the resulting expressions to the pmacro's parameters,
141 processing the pmacro according to its definition, and returning the result.
142 Free variables are left unchanged.@footnote{A "free variable",
143 as defined here, is one that is not already bound, be it to
144 parameter, macro, or local variable within a macro.
145 Note that to pmacros, cpu description file elements like
146 @code{reg}, @code{sequence}, @code{VOID}, etc. are just symbols;
147 they have no special meaning.}
149 Variable macros are expanded simply by replacing their name with their value.
151 After a pmacro has been expanded, if the result is a symbol that names
152 another pmacro, it is in turn processed. This happens just once,
153 not repeatedly.@footnote{This behaviour will go away eventually, do not
156 Here is a simple example that uses pmacros to simplify the
157 definition of several instructions.
160 ;; OP1_*,OP2_* are previously defined enums
161 (define-pmacro arithmetic-insns
162 ((add OP2_0) (sub OP2_1) (mul OP2_2) (div OP2_3))
164 (define-pmacro (make-arith-reg/reg-format opcode)
165 (+ OP1_0 opcode dr sr)
167 (define-pmacro (make-arith-reg/imm-format opcode)
168 (+ OP1_1 opcode dr sr)
170 (define-pmacro (define-arith-insn ispec)
173 (.str (.ref ispec 0) " reg/reg")
175 (.str (.ref ispec 0) " $dr,$sr")
176 (make-arith-reg/reg-format (.ref ispec 1))
177 (set dr ((.ref ispec 0) dr sr))
181 (.str (.ref ispec 0) " reg/imm")
183 (.str (.ref ispec 0) " $dr,$imm")
184 (make-arith-reg/imm-format (.ref ispec 1))
185 (set dr ((.ref ispec 0) dr imm))
191 ;; Create dnis for each kind of instruction.
192 ;; The result of this is:
193 ;; (begin (begin (dni ...) ...) (begin (dni ...) ...) ...)
194 (.splice begin (.unsplice (.map define-arith-insn arithmetic-insns)))
197 The @code{.splice}, @code{.unsplice} are necessary to pass properly
198 formed expressions to the @file{.cpu} file reader.
199 If we just used @samp{(.map define-arith-insn arithmetic-insns)}
200 the reader would see @samp{((begin (dni ...) ...) (begin (dni ...) ...))}.
201 Note how the first @code{begin} is nested within a list, and does not
202 appear in the ``top level'' list.
204 Another way to accomplish the same thing that doesn't require
205 @code{.splice}, @code{.unsplice} is to use @code{.for-each}, @code{.exec}.
206 @code{.for-each} is only used for its side-effects, it does not
207 return a result. Therefore, in order to actually cause the
208 @file{.cpu} file reader to see any definitions we need to use
209 @code{.exec} to pass the dnis to the reader.
212 (define-pmacro (define-arith-insn ispec)
213 (.exec (dni (.ref ispec 0)
214 (.str (.ref ispec 0) " reg/reg")
216 (.str (.ref ispec 0) " $dr,$sr")
217 (make-arith-reg/reg-format (.ref ispec 1))
218 (set dr ((.ref ispec 0) dr sr))
221 (.exec (dni (.ref ispec 0)
222 (.str (.ref ispec 0) " reg/imm")
224 (.str (.ref ispec 0) " $dr,$imm")
225 (make-arith-reg/imm-format (.ref ispec 1))
226 (set dr ((.ref ispec 0) dr imm))
230 (.for-each define-arith-insn arithmetic-insns)
233 @node Syntactic forms
234 @section Syntactic forms
236 Some function pmacros are called @samp{syntactic forms}.
237 These pmacros are processed differently in that parameters are
238 @emph{not} evaluated first. Instead it is up to the pmacro
239 to decide when, and if, the parameters are evaluated.
241 The syntactic forms are:
245 @item @code{.pmacro}.
246 @xref{Defining a pmacro inline}.
247 @item @code{.let}, @code{.let*}.
248 @xref{Defining a block of locals}.
250 @xref{Traditional @code{if}}.
252 @xref{Traditional @code{case}}.
254 @xref{Extended if/elseif/else}.
256 @xref{A block of statements}.
264 The docs for each syntactic pmacro describes when it evaluates its arguments.
266 All syntactic form pmacros are pre-defined.
267 The user can not currently define his/her own.
269 @node Default argument values
270 @section Default argument values
272 Invoking pmacros by specifying argument names allows some, or all,
273 arguments to be elided and thus allows for arguments to have default values.
275 Specify default values with the following syntax.
278 (define-pmacro (macro-name (arg1 . default-value)
279 (arg2 . default value) ...)
284 To invoke a pmacro with default values for some, or all,
285 arguments, you @emph{must} specify arguments by name.
290 (define-pmacro (foo (arg1 . 1) (arg2 . 2))
294 (foo #:arg1 4) ==> (bar 4 2)
296 (foo 4) ==> ERROR, must invoke pmacro by specifying arguments by name
299 @node Multiple result statements
300 @section Multiple result statements
303 The result of a preprocessor macro is exactly one expression.
304 It is often useful, however, to return multiple expressions, say for
305 example when you want one macro to define several instructions.
307 The way to do this is to enclose all the expressions with @code{begin}.
308 @code{begin} is only valid at the top [definition] level.
310 Note that this is @emph{not} the @code{.begin} builtin pmacro.
311 We want to pass a list of statements to the @file{.cpu} file reader,
312 and pmacros have all been evaluated and discarded by this point.
314 @node Symbols and strings
315 @section Symbols and strings
317 There are several builtin macros for symbol and string manipulation.
320 * Symbol concatenation:: The @code{.sym} builtin
321 * String concatenation:: The @code{.str} builtin
322 * Convert a number to a hex:: The @code{.hex} builtin
323 * Convert a string to uppercase:: The @code{.upcase} builtin
324 * Convert a string to lowercase:: The @code{.downcase} builtin
325 * Getting part of a string:: The @code{.substring} builtin
326 * Symbol or string length:: The @code{.length} builtin
329 @node Symbol concatenation
330 @subsection Symbol concatenation
333 Symbol and string concatenation are supported. Symbol concatenation is
336 @code{(.sym arg1 arg2 ...)}
338 Acceptable arguments are symbols, strings, and numbers.
339 The result is a symbol with the arguments concatenated together.
340 Numbers are converted to a string, base 10, and then to a symbol.
341 The result must be a valid Scheme symbol with the additional restriction
342 that the first character must be a letter. The resulting symbol
343 is recursively macro-expanded.
345 @node String concatenation
346 @subsection String concatenation
349 String concatenation is done with
351 @code{(.str arg1 arg2 ...)}
353 Acceptable arguments are symbols, strings, and numbers. The result is a
354 string with the arguments concatenated together.
355 Numbers are converted base 10.
360 (define-pmacro (bin-op mnemonic op2-op sem-op)
362 (.str mnemonic " reg/reg")
364 (.str mnemonic " $dr,$sr")
365 (+ OP1_0 op2-op dr sr)
366 (set dr (sem-op dr sr))
369 (bin-op and OP2_12 and)
370 (bin-op or OP2_14 or)
371 (bin-op xor OP2_13 xor)
374 @node Convert a number to a hex
375 @subsection Convert a number to a hex
378 Convert a number to a lowercase hex string with @code{.hex}. If
379 @code{width} is present, the result is that many characters beginning
380 with the least significant digit. Zeros are prepended as necessary.
382 Syntax: @code{(.hex number [width])}
389 (.hex 42 4) --> "002a"
392 @node Convert a string to uppercase
393 @subsection Convert a string to uppercase
396 Convert a string to uppercase with @code{.upcase}.
398 Syntax: @code{(.upcase string)}
403 (.upcase "foo!") --> "FOO!"
406 @node Convert a string to lowercase
407 @subsection Convert a string to lowercase
410 Convert a string to lowercase with @code{.downcase}.
412 Syntax: @code{(.downcase string)}
417 (.downcase "BAR?") --> "bar?"
420 @node Getting part of a string
421 @subsection Getting part of a string
424 Extract a part of a string with @code{.substring}.
426 Syntax: @samp{(.substring string start end)}
428 where @samp{start} is the starting character, and @samp{end} is one past
429 the ending character. Character numbering begins at position 0.
430 If @samp{start} and @samp{end} are the same, and both valid, the empty
436 (.substring "howzitgoineh?" 2 6) --> "wzit"
439 @node Symbol or string length
440 @subsection Symbol or string length
441 @c @cindex .length - the @cindex for this is in the list section
443 Compute the length, in characters, of a symbol or string.
445 Syntax: @samp{(.length symbol-or-string)}
451 (.length "def") --> 3
455 @node Number utilities
456 @section Number utilities
458 Builtin macros for manipulating numbers.
461 * Number generation:: The @code{.iota} builtin
464 @node Number generation
465 @subsection Number generation
467 @cindex Number generation
469 Machine descriptions often require a list of sequential numbers.
470 Generate a list of numbers with the @code{.iota} builtin macro.
472 Syntax: @samp{(.iota count [start [incr]])}.
477 (.iota 5) --> 0 1 2 3 4
478 (.iota 5 4) --> 4 5 6 7 8
479 (.iota 5 5 -1) --> 5 4 3 2 1
483 @section List utilities
485 Builtin macros for maninpulating lists.
488 * Creating lists:: The @code{.list} builtin
489 * List splicing:: The @code{.splice} builtin
490 * Referencing a list element:: The @code{.ref} builtin
491 * List length:: The @code{.length} builtin
492 * Lists of repeated elements:: The @code{.replicate} builtin
493 * Finding a subset of a list:: The @code{.find} builtin
494 * car/cdr:: car, cdr, etc. from Scheme/Lisp
498 @subsection Creating lists
501 Lists can be created with the @code{.list} builtin.
503 Syntax: @samp{(.list elm1 elm2 ...)}
505 It's somewhat redundant as lists can also be created simply writing
506 @samp{(elm1 elm2 ...)}.
509 @subsection List splicing
512 Syntax: @samp{(.splice [expr1] [expr2] [(.unsplice list1)]
513 [(.unsplice list2)] [expr3] ...)}
515 It is often useful to splice a list into a "parent" list.
516 This is best explained with an example.
519 (define-pmacro (splice-test a b c)
520 (.splice a (.unsplice b) c))
521 (pmacro-dump (splice-test 1 (2) 3))
526 Note that a level of parentheses around @code{2} has been removed.
528 This is useful, for example, when one wants to pass a list of fields to
529 a macro that defines an instruction. For example:
532 (define-pmacro (cond-move-1 name comment mnemonic cc-prefix cc-name cc-opcode
533 src-name src-opcode cond test)
535 (.str "move %" cc-name " " comment ", v9 page 191")
537 (.str mnemonic " " cc-prefix cc-name ",$" src-name ",$rd")
538 (.splice + OP_2 rd OP3_MOVCC cond
539 (.unsplice cc-opcode) (.unsplice src-opcode))
546 This macro, taken from @file{sparc64.cpu}, defines a conditional move
547 instruction. Arguments @code{cc-opcode} and @code{src-opcode} are lists
548 of fields. The macro is invoked with (simplified from @file{sparc64.cpu}):
551 (cond-move-1 mova-icc "blah ..." mova
552 "%" icc ((f-fmt4-cc2 1) (f-fmt4-cc1-0 0))
553 rs2 ((f-i 0) (f-fmt4-res10-6 0) rs2)
555 (cond-move-1 mova-imm-icc "blah ..." mova
556 "%" icc ((f-fmt4-cc2 1) (f-fmt4-cc1-0 0))
557 simm11 ((f-i 1) simm11)
561 Macro @code{cond-move-1} is being used here to define both the register
562 and the immediate value case. Each case has a slightly different list
563 of opcode fields. Without the use of @code{.splice}/@code{.unsplice},
564 the resulting formats would be:
567 (+ OP_2 rd OP3_MOVCC CC_A ((f-fmt4-cc2-1) (f-fmt4-cc1-0 0))
568 ((f-i 0) (f-fmt4-res10-6 0) rs2))
572 (+ OP_2 rd OP3_MOVCC CC_A ((f-fmt4-cc2-1) (f-fmt4-cc1-0 0))
576 respectively. This is not what is wanted. What is wanted is
579 (+ OP_2 rd OP3_MOVCC CC_A (f-fmt4-cc2-1) (f-fmt4-cc1-0 0)
580 (f-i 0) (f-fmt4-res10-6 0) rs2)
584 (+ OP_2 rd OP3_MOVCC CC_A (f-fmt4-cc2-1) (f-fmt4-cc1-0 0)
588 respectively, which is what @code{.splice} achieves.
590 @code{.unsplice} is a special reserved symbol that is only recognized inside
591 @code{.splice}. There can be any number of @code{.unsplice} expressions
592 in a @code{.splice} but they all must be at the ``top level''.
594 I.e. this is not supported:
595 @samp{(.splice 1 (2 3 (.unsplice (4 5))))}.
597 Note that @code{.splice} without any @code{.unsplice} expressions
598 behaves identically to @code{.list}.
600 Also note that the arguments to @code{.splice} and @code{.unsplice} are
601 evaluted first. Any macro invocations are first expanded, and then
602 @code{.unsplice} is processed.
604 @node Referencing a list element
605 @subsection Referencing a list element
608 Reference elements of a list with @code{.ref}.
610 Syntax: @samp{(.ref list element-number)}
615 (.ref (1 2 3) 1) --> 2
619 @subsection List length
622 The length of a list is computed with @code{.length}.
624 Syntax: @samp{(.length list)}.
629 (.length (1 2 3)) --> 3
632 @node Lists of repeated elements
633 @subsection Lists of repeated elements
636 Create a list of repeated elements with @code{.replicate}.
638 Syntax: @samp{(.replicate n expr)}
643 (.replicate 4 5) --> (5 5 5 5)
646 @node Finding a subset of a list
647 @subsection Finding a subset of a list
650 Compute a subset of a list matching a specified predicate with @code{.find}.
652 Syntax: @samp{(.find predicate list)}
657 (.find (.pmacro (n) (.lt n 2)) (.iota 4)) --> (0 1)
669 CGEN provides a small set of pmacros for those familiar with
675 Equivalent to @samp{(.ref list 0)}.
685 Return all elements of the list after the first one.
690 (.cdr (1 2 3)) --> (2 3)
695 Return the first element of the first element of the list.
697 I.e., the @code{car} of the @code{car} of the list.
702 (.caar ((1 2 3) (4 5 6))) --> 1
707 Return the second element of the list.
709 I.e., the @code{car} of the @code{cdr} of the list.
714 (.cadr (1 2 3)) --> 2
719 Return all elements after the first element of the first element of the list.
720 That's a bit of a mouthful, it's easier to understand by applying
721 @code{car} and @code{cdr} in turn.
723 I.e., the @code{cdr} of the @code{car} of the list.
728 (.cadr ((1 2 3) (4 5 6))) --> (2 3)
733 I.e., the @code{cdr} of the @code{cdr} of the list.
735 Return all elements of the list after the first two.
740 (.cddr (1 2 3)) --> (3)
745 @node Iteration utilities
746 @section Iteration utilities
748 Macros for iterating over lists
751 * Mapping a macro over a list:: The @code{.map} builtin
752 * Iterating over a list:: The @code{.for-each} builtin
755 @node Mapping a macro over a list
756 @subsection Mapping a macro over a list
759 Apply a macro to each element of a list, or set of lists, with @code{.map}.
760 The order in which each element of the list is processed is unspecified.
762 The syntax is @samp{(.map macro-name list1 [list2 ...])}.
763 @samp{macro} must take as many arguments as there are lists.
765 The result is a list with @samp{macro} applied to each element of
766 @samp{listN}. This is often useful in constructing enum and register name lists.
771 (define-pmacro (foo name number) ((.sym X name) number))
772 (.map foo (A B C D E) (.iota 5))
776 ((XA 0) (XB 1) (XC 2) (XD 3) (XE 4))
779 @node Iterating over a list
780 @subsection Iterating over a list
783 Apply a macro to each element of a list, or set of lists,
784 with @code{.for-each}.
785 Each element of the list is guaranteed to be processed in order.
787 The syntax is @samp{(.for-each macro list1 [list2 ...])}.
788 @samp{macro} must take as many arguments as there are lists.
790 There is no result, or rather the result is always the empty list ().
791 Note that this macro is therefore useless for macro expansion.
792 It's purpose is to process @code{macro} for its side-effects.
793 The @code{.exec} builtin pmacro is useful here.
795 @node Conditional macros
796 @section Conditional macros
798 Macros for conditional execution.
801 * Traditional @code{if}:: The @code{.if} builtin
802 * Traditional @code{case}:: The @code{.case} builtin
803 * Extended if/elseif/else:: The @code{.cond} builtin
806 @node Traditional @code{if}
807 @subsection Traditional @code{if}
810 Syntax: @samp{(.if condition then-expr [else-expr])}.
812 The @code{condition} is evaluated, and if it is non-#f then
813 @code{then-expr} is evaluated and returned.
814 Otherwise, if @code{else-expr} is present it is evaluated and returned.
815 Otherwise, the empty list @code{()} is returned.
817 @node Traditional @code{case}
818 @subsection Traditional @code{case}
821 Syntax: @samp{(.case expr ((case1-list) expr-list) [case-list] [(else expr-list)])}
823 The expression @code{expr} is evaluated, and then
824 each case list is examined in turn to look for a match.
825 The first case list with an element that matches @code{expr} wins,
826 its @code{expr-list} is evaluated and the result of the last expression
827 in the expression list is returned.
829 If there is no successful match and no @code{else} part,
830 then the empty list @code{()} is returned.
832 @node Extended if/elseif/else
833 @subsection Extended if/elseif/else
836 Syntax: @samp{(.cond (expr1 expr-list) [cond-list] [(else expr-list)])}
838 Each condition's expression is evaluated in turn.
839 The first condition to evaluate to non-#f wins,
840 its @code{expr-list} is evaluated and the result of the last expression
841 in the expression list is returned.
843 If there is no successful condition and no @code{else} part,
844 then the empty list @code{()} is returned.
846 @node Pmacro utilities
847 @section Pmacro utilities
849 Pmacros for working with pmacros.
852 * Re-evaluating an expression:: The @code{.eval} builtin
853 * Immediate execution of a command:: The @code{.exec} builtin
854 * Applying a pmacro to a list:: The @code{.apply} builtin
855 * Defining a pmacro inline:: The @code{.pmacro} builtin
856 * Passing pmacros as arguments:: Passing a pmacro to another pmacro
857 * Defining a block of locals:: The @code{.let}, @code{.let*} builtins
858 * A block of statements:: The @code{.begin} builtin
859 * Testing if something is a pmacro:: The @code{.pmacro?} builtin
862 @node Re-evaluating an expression
863 @subsection Re-evaluating an expression
866 Syntax: @samp{(.eval expr)}
868 Sometimes one wishes to build up an expression in non-trivial ways
869 and then have the expression evaluated.
870 Use the @code{.eval} builtin pmacro for this purpose,
871 it re-evaluates @samp{expr}, invoking any pmacros contained therein.
873 A perhaps contrived example is when one wants to construct the pmacro's
874 name from a set of parameters.
879 (define-pmacro (do-foo a b) (foo a b))
880 (define-pmacro (do-bar a b) (bar a b))
881 (define-pmacro (doer what a b) (.eval (.list (.sym do- what) a b)))
882 (doer foo 1 2) ;; --> (foo 1 2)
883 (doer bar 3 4) ;; --> (bar 3 4)
886 @node Immediate execution of a command
887 @subsection Immediate execution of a command
890 Syntax: @samp{(.exec expr)}
892 Sometimes one wishes to pass an expression to the @file{.cpu} file reader
893 immediately, rather than waiting for it to process the expression
894 that is the result of a pmacro. This typically happens with the
895 @code{.for-each} builtin pmacro.
896 Use the @code{.exec} builtin pmacro for this purpose.
897 It immediately passes @samp{expr} to the @file{.cpu} file reader
898 for processing and returns @code{()} as a result.
900 @node Applying a pmacro to a list
901 @subsection Applying a pmacro to a list
904 Invoke a macro with each argument coming from an element of a list,
907 The syntax is @samp{(.apply macro-name list)}.
909 The result is the result of invoking macro @samp{macro-name}.
910 @samp{macro-name} should take as many arguments as there elements in
911 @samp{list}. If @samp{macro-name} takes a variable number of trailing
912 arguments, there must be at least as many list elements as there are
914 @c clumsily worded or what
917 @c need a more useful example
922 (.apply .str (.iota 5)) --> "01234"
925 Note that @code{(.str (.iota 5))} is an error. Here the list
926 @samp{(0 1 2 3 4)} is passed as the first argument of @code{.str},
929 @node Defining a pmacro inline
930 @subsection Defining a pmacro inline
933 Define a macro inline with @code{.pmacro}.
934 This is only supported when passing macros as arguments to other macros,
935 and as values for local variables in @code{.let} or @code{.let*}.
940 (define-pmacro (load-op suffix op2-op mode ext-op)
942 (dni (.sym ld suffix) (.str "ld" suffix)
944 (.str "ld" suffix " $dr,@@$sr")
945 (+ OP1_2 op2-op dr sr)
946 (set dr (ext-op WI (mem mode sr)))
951 (load-op "" OP2_12 WI (.pmacro (mode expr) expr))
952 (load-op b OP2_8 QI (.pmacro (mode expr) (ext mode expr)))
953 (load-op h OP2_10 HI (.pmacro (mode expr) (ext mode expr)))
954 (load-op ub OP2_9 QI (.pmacro (mode expr) (zext mode expr)))
955 (load-op uh OP2_11 HI (.pmacro (mode expr) (zext mode expr)))
958 .pmacro's bind the same way Scheme lambda expressions do.
959 In the following example, arg2 in the second pmacro is bound
960 to the arg2 argument of the outer pmacro.
963 (define-pmacro (foo arg1 arg2) ((.pmacro (bar) (+ arg2 bar)) arg1))
964 (foo 3 4) ==> (+ 4 3)
967 The contents of a @code{.pmacro} are not evaluated until the pmacro
970 @node Passing pmacros as arguments
971 @subsection Passing pmacros as arguments
973 Macros may be passed to other macros.
978 (define-pmacro (no-ext-expr mode expr) expr)
979 (define-pmacro (ext-expr mode expr) (ext mode expr))
980 (define-pmacro (zext-expr mode expr) (zext mode expr))
982 (define-pmacro (load-op suffix op2-op mode ext-op)
984 (dni (.sym ld suffix) (.str "ld" suffix)
986 (.str "ld" suffix " $dr,@@$sr")
987 (+ OP1_2 op2-op dr sr)
988 (set dr (ext-op WI (mem mode sr)))
993 (load-op "" OP2_12 WI no-ext-expr)
994 (load-op b OP2_8 QI ext-expr)
995 (load-op h OP2_10 HI ext-expr)
996 (load-op ub OP2_9 QI zext-expr)
997 (load-op uh OP2_11 HI zext-expr)
1000 @node Defining a block of locals
1001 @subsection Defining a block of locals
1005 It is often handy to assign expressions to local variables,
1006 if only to improve readability.
1007 This is accomplished with the @code{.let} and @code{.let*} builtin pmacros.
1009 @code{.let} and @code{.let*} have the same syntax:
1011 @samp{(.let local-list expr1 [expr2 ...])}
1013 @samp{(.let* local-list expr1 [expr2 ...])}
1015 where @samp{local-list} is a list of local variable assignments,
1016 with the syntax @samp{(name expr)}. All variable names must be distinct.
1018 The difference is that in @code{.let} all expressions of all locals
1019 are evaluated @emph{first} and @emph{then} assigned to the locals,
1020 whereas in @code{.let*} each local is evaluated and assigned in turn.
1021 This means that expressions in @code{.let} cannot refer to other locals
1022 in the @code{.let}. If they do, they will get the values the variables had
1023 before the @code{.let}. Also remember that symbols in pmacros are
1024 ``self-quoting'', so if a symbol isn't bound to any value, its value is
1030 (define-pmacro (load-op suffix op2-op mode ext-op)
1032 (no-ext-expr (.pmacro (mode expr) expr))
1033 (ext-expr (.pmacro (mode expr) (ext mode expr)))
1034 (zext-expr (.pmacro (mode expr) (zext mode expr)))
1037 (dni (.sym ld suffix) (.str "ld" suffix)
1039 (.str "ld" suffix " $dr,@@$sr")
1040 (+ OP1_2 op2-op dr sr)
1041 (set dr (ext-op WI (mem mode sr)))
1047 (load-op "" OP2_12 WI no-ext-expr)
1048 (load-op b OP2_8 QI ext-expr)
1049 (load-op h OP2_10 HI ext-expr)
1050 (load-op ub OP2_9 QI zext-expr)
1051 (load-op uh OP2_11 HI zext-expr)
1054 Note that one can also assign pmacros to local variables.
1056 Note that @code{.let*} is equivalent to a set of nested @code{.let}
1060 (.let* ((x 1) (y x)) y)
1062 (.let ((x 1)) (.let ((y x)) y))
1065 @node A block of statements
1066 @subsection A block of statements
1069 Sometimes one wishes to have a list of expressions (or statements)
1070 and the context only allows one expression.
1071 This can happen, for example, in the @samp{then} and @samp{else}
1072 clauses of the @code{.if} builtin pmacro.
1073 Use the @code{.begin} builtin pmacro for these situations.
1075 Syntax: @samp{(.begin [expr1 [expr2 ...]])}
1077 Each expression is evaluated in turn and the result is the result
1078 of the last expression.
1080 @node Testing if something is a pmacro
1081 @subsection Testing if something is a pmacro
1084 Sometimes one wishes to know if an argument is a pmacro or not.
1085 This is useful when one is writing a pmacro that has a parameter
1086 that is either a pmacro or is not (e.g., it could be an rtl function
1087 instead). When the parameter is a pmacro one might like to @samp{.apply}
1088 it to another argument, and if not one might like to simply construct
1089 a list of it and the other argument.
1091 Syntax: @samp{(.pmacro? arg)}
1096 (define-pmacro (compare a b) (if (eq a b) 1 0))
1097 (define-pmacro (maybe-apply fun args)
1100 (.splice fun (.unsplice args))))
1101 (define-pmacro (gen-semantics semfun args)
1102 (set dest (maybe-apply semfun args)))
1103 (gen-semantics add (op1 op2))) ;; ==> (set dest (add op1 op2))
1104 (gen-semantics compare (op1 op2))) ;; ==> (set dest (if (eq op1 op2) 1 0))
1107 @node Debugging utilities
1108 @section Debugging utilities
1111 * .print:: Printing a diagnostic message
1112 * .dump:: Printing arbitrarily complex objects
1113 * .error:: Signalling an error has occurred
1120 Syntax: @samp{(.print expr1 [...])}
1122 Evaluate and print the supplied expressions.
1123 This is useful for debugging and logging messages.
1125 NOTE: Strings are printed without enclosing quotes.
1126 Use @code{dump} if you want to print strings with enclosing quotes.
1128 The result is the empty list @code{()}.
1134 Syntax: @samp{(.dump expr1 [...])}
1136 Evaluate and print the supplied expressions.
1137 This is useful for debugging and logging messages.
1139 NOTE: Strings are printed with enclosing quotes.
1140 Use @code{print} if you want to print strings without enclosing quotes.
1142 The result is the empty list @code{()}.
1148 Syntax: @samp{(.error expr1 [...])}
1150 Evaluate the supplied expressions and signal an error.
1151 The expressions are typically error messages, often with the
1152 object that caused the error.
1155 @section Comparisons
1157 Builtin macros for comparing objects.
1159 In CGEN ``true'' is represented by @code{#t}
1160 and ``false'' is represented by @code{#f}.
1163 * .equal?:: Deep comparison
1167 * .eq:: Shallow comparison
1168 * .ne:: Shallow comparison
1170 * .gt:: Greater than
1171 * .le:: Less than or equal to
1172 * .ge:: Greater than or equal to
1179 Syntax: @samp{(.equal? x y)}
1181 Return #t if @code{x} is equal to @code{y}, otherwise #f.
1183 A ``deep'' comparison is used.
1184 I.e., if @code{x} and @code{y} are lists, list elements
1185 are recursively compared
1190 (.equal? "abc" "abc") --> #t
1191 (.equal? symbol1 symbol1) --> #t
1192 (.equal? ((1 2 3) (4 5 6)) ((1 2 3) (4 5 6))) --> #t
1199 Syntax: @samp{(.andif [expr1 [expr2 ...]])}
1201 Each expression is evaluated in turn.
1202 If an expression evaluates to false (@code{#f}) then
1203 evaluation stops and the result is @code{#f}.
1204 If all expressions evaluate to non-@code{#f}, then
1205 the value of the last expression is returned.
1207 Note that this is a special form.
1208 Just like @code{&&} in C, evaluation of subsequent
1209 expressions is not done once an expression is found
1210 that evaluates to ``false''.
1215 (.andif 1 #f 2) --> #f
1216 (.andif 1 2 3) --> 3
1224 Syntax: @samp{(.orif [expr1 [expr2 ...]])}
1226 Each expression is evaluated in turn.
1227 If an expression evaluates to non-false (@code{#f}) then
1228 evaluation stops and the result is the value of the first
1229 non-@code{#f} expression.
1230 If all expressions evaluate to @code{#f}, then
1231 the result is @code{#f}.
1233 Note that this is a special form.
1234 Just like @code{||} in C, evaluation of subsequent
1235 expressions is not done once an expression is found
1236 that evaluates to non-``false''.
1242 (.orif #f #f #f) --> #f
1250 Syntax: @samp{(.not expr)}
1252 If @code{expr} is @code{#f} return @code{#t}.
1253 If @code{expr} is non-@code{#f} return @code{#f}.
1255 @emph{Note that (.not 0) is not 1, it is #f!}.
1261 Syntax: @samp{(.eq x y)}
1263 Return ``true'' if @code{x} equals @code{y}, otherwise ``false''.
1265 Note that this does @emph{not} do a deep comparison,
1266 and can only be used with symbols, strings, and numbers.
1267 Both @code{x} and @code{y} must be the same type.
1274 (.eq 0 one) -> error
1276 (.eq zero one) -> #f
1277 (.eq "abc" "abc") -> #t
1278 (.eq "abc" "def") -> #f
1285 Syntax: @samp{(.ne x y)}
1287 Return ``true'' if @code{x} does not equal @code{y}, otherwise ``false''.
1289 Note that this does @emph{not} do a deep comparison,
1290 and can only be used with symbols, strings, and numbers.
1291 Both @code{x} and @code{y} must be the same type.
1298 (.ne 0 one) -> error
1300 (.ne zero one) -> #t
1301 (.ne "abc" "abc") -> #f
1302 (.ne "abc" "def") -> #t
1309 Syntax: @samp{(.lt x y)}
1311 Return ``true'' if @code{x} is less than @code{y}, otherwise ``false''.
1313 Both @code{x} and @code{y} must be numbers.
1319 Syntax: @samp{(.gt x y)}
1321 Return ``true'' if @code{x} is greater than @code{y}, otherwise ``false''.
1323 Both @code{x} and @code{y} must be numbers.
1329 Syntax: @samp{(.le x y)}
1331 Return ``true'' if @code{x} is less than or equal to @code{y},
1332 otherwise ``false''.
1334 Both @code{x} and @code{y} must be numbers.
1340 Syntax: @samp{(.ge x y)}
1342 Return ``true'' if @code{x} is greater than or equal to @code{y},
1343 otherwise ``false''.
1345 Both @code{x} and @code{y} must be numbers.
1347 @node Arithmetic functions
1348 @section Arithmetic functions
1352 * .sub:: Subtraction
1353 * .mul:: Multiplication
1354 * .div:: Integer division
1355 * .rem:: Integer remainder
1362 Syntax: @samp{(.add x y)}
1364 Return @code{x} + @code{y}.
1370 Syntax: @samp{(.sub x y)}
1372 Return @code{x} - @code{y}.
1378 Syntax: @samp{(.mul x y)}
1380 Return @code{x} * @code{y}.
1386 Syntax: @samp{(.div x y)}
1388 Return the quotient of @code{x} divided by @code{y}.
1390 Only integer division is supported,
1391 both @code{x} and @code{y} must be integers.
1397 Syntax: @samp{(.rem x y)}
1399 Return the remainder of @code{x} divided by @code{y}.
1401 Only integer division is supported,
1402 both @code{x} and @code{y} must be integers.
1404 @c Need to define and document behaviour for negative numbers.
1406 @node Logical functions
1407 @section Logical functions
1409 Builtin macros for shifts and bitwise functions.
1412 * .sll:: Shift left logical
1413 * .srl:: Shift right logical
1414 * .sra:: Shift right arithmetic
1415 * .and:: Bitwise and
1417 * .xor:: Bitwise exclusive-or
1418 * .inv:: Bitwise inversion
1425 Syntax: @samp{(.sll x n)}
1427 Shift @code{x} left by @code{n} bits.
1428 Zeroes are shifted into the low-order bits.
1430 @code{n} must be a non-negative integer.
1436 Syntax: @samp{(.srl x n)}
1438 Shift @code{x} right by @code{n} bits.
1440 @code{x} @emph{must} be a non-negative integer.
1441 Numbers at the pmacro level have ``infinite precision'',
1442 and shifting zeroes into the high-order bits of
1443 infinite-precision negative numbers is undefined.
1445 @code{n} must be a non-negative integer.
1451 Syntax: @samp{(.sra x n)}
1453 Shift @code{x} right arithmetically by @code{n} bits.
1454 The sign bit of @code{x} is shifted into the high-order bits.
1456 @code{n} must be a non-negative integer.
1462 Syntax: @samp{(.and x y)}
1464 Return the bitwise @code{and} of @code{x} and @code{y}.
1470 Syntax: @samp{(.or x y)}
1472 Return the bitwise @code{or} of @code{x} and @code{y}.
1478 Syntax: @samp{(.xor x y)}
1480 Return the bitwise @code{exclusive-or} of @code{x} and @code{y}.
1486 Syntax: @samp{(.inv x)}
1488 Return the bitwise @code{inversion} of @code{x}.
1490 @node Internal use pmacros
1491 @section Internal use pmacros
1493 This section documents pmacros that are for internal use only.
1494 Today there's only one, @samp{.internal-test}, and it is used
1497 @subsection .internal-test
1498 @cindex .internal-test
1500 Syntax: @samp{(.internal-test expr)}
1502 Execute @samp{expr} as a Scheme expression and return #f if the
1503 expression returns #f and return #t if the expression returns non-#f.
1505 This is for use in the CGEN testsuite only.
1506 See the testsuite for usage examples.