Presentation is loading. Please wait.

Presentation is loading. Please wait.

Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.

Similar presentations

Presentation on theme: "Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently."— Presentation transcript:

1 Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently.

2 m4 macro processor a programming language with a quite different feel than C, Java, etc.: textual substitution (replacement) rather than numeric data processing function evaluation rather than variety of control structures recursion rather than iteration (you can build iteration out of recursion)

3 m4 macro processor define(name,definition) -- macro definition - instructs m4 to replace each occurrence of "name" by its definition (textual substitution, a.k.a. expansion). You can define a macro once and use it several times. Leading unquoted blanks, tabs, and newlines are ignored. $1,$2,... inside "definition" -- macro parameters (based on position in call) for parameterized expansion ($0 = macro name, $* = all arguments $1 and above) name(arg1,arg2) -- macro call with two parameters (arguments) include(file) -- read in text of "file"

4 m4 macro processor `text' -- delay expansion of text but strip quotes (note open quote and close quote difference) changequote(^,!) -- change quote characters to other characters eval(expression) -- evaluate an arithmetic expression incr(arg) / decr(arg) -- returns the value incremented or decremented [ shortcuts for eval(arg+1), eval(arg-1) ] ifdef(arg1,arg2,arg3) -- if the first parameter is defined, return the second, otherwise return the third ifelse(arg1,arg2,arg3,arg4) -- if the first parameter is the same string as the second, return the third parameter, otherwise return the fourth (with a provision for nested ifelse evaluation using parameters 4,5,6, and 7)

5 m4 macro processor index(arg1,arg2) -- returns position within the first parameter where the second parameter begins (0- origin), or -1 if the second parameter is not a substring within the first parameter len(arg) -- returns the number of characters in the first parameter (i.e., the string length) substr(arg1,arg2,arg3) -- substring(string,start,length) where start is 0-origin position translit(arg1,arg2,arg3) -- transliterate first string using match characters of second parameter with substitute characters of third parameter divert(2) -- send output to second stream...

6 m4 macro processor undivert -- print all streams dnl -- delete rest of line, including newline debugmode(V) -- turns on tracing and debugging output

7 m4 macro processor typical use in HLL define(N,10) for(i = 0; i < N; i++) for(i = 0; i < 10; i++)

8 m4 macro processor example use of positional parameters define(swap,$2 $1) swap(3,4) 4 3 swap(a,b) b a

9 m4 macro processor example use of built-in macro define(tr,`translit($1,abcde,ABCDE)') tr(computer) ComputEr

10 m4 macro processor example of the need to quote macro name for redefinition define(fn,3) fn 3 define(fn,4) fn 3 define(`fn',4) fn 4

11 m4 macro processor parameter passing examples – spaces, commas are important.use eval() to interpret string as arithmetic expression define(`show',` ') show(a) show( b ) define(`fn',`eval($1*$2)') fn(3) m4: Bad expression in eval: 3* fn(3 4) m4: Bad expression in eval (excess input): 3 4* fn (3,4) m4: Bad expression in eval: * (3,4) fn(3,4) 12 fn(3,4,5) 12

12 m4 macro processor counting in m4 % cat m4.script `define(loc,1) ' define(loc,1) `loc ' loc `define(loc,2) ' define(loc,2) `loc ' loc `define(`loc',3) ' define(`loc',3) `loc ' loc `loc+1 ' loc+1 `eval(loc+1) ' eval(loc+1) `define(loc,loc+1) ' define(loc,loc+1) `loc ' loc `define(`loc',loc+1) ' define(`loc',loc+1) `loc ' loc `define(`loc',eval(loc+1))' define(`loc',eval(loc+1)) `loc ' loc `define(`loc',eval(loc+1))' define(`loc',eval(loc+1)) `loc ' loc

13 m4 macro processor counting in m4 % m4 m4.script define(loc,1) loc 1 define(loc,2) loc 1 define(`loc',3) loc 3 loc+1 3+1 eval(loc+1) 4 define(loc,loc+1) loc 3 define(`loc',loc+1) loc 3+1 define(`loc',eval(loc+1)) loc 5 define(`loc',eval(loc+1)) loc 6

14 m4 macro processor multiply-accumulate (mac) for accumulator machine define(mac,`load($2) mul($3) add($1)') mac(a,b,c) load(b) mul(c) add(a)

15 Macros in ARM The ARM assembler will replace the macro name with its definition. Macros may contain calls to other macros, nested up to 255 levels Two directives define a macro, MACRO and MEND. MACRO {$label} macroname {$parameter1} {,$parameter2}{,$parameter3}.....code... MEND

16 Macro A macro prototype statement must appear on the first line following the MACRO directive. The prototype tells the assembler the name of the macro, macroname, and its parameters. A label is optional, but is useful if the macro defines internal labels. Any number of parameters can be used; each must begin with $ to distinguish it from ordinary program symbols. Within the macro body, $label, $parameter, and so on, can be used in the same way as any other variables.

17 ARM Macros The $label parameter is simply treated as another parameter to the macro. The macro itself describes which labels are defined where. The label does not represent the first instruction in the macro expansion. For example, it is useful in a macro that uses several internal labels, such as a loop, to define each internal label as the base label with a different suffix. Sometimes, a value appends a macro parameter or label. Separate the appended value by a dot. After the assembler recognizes the end of the parameter and label, the assembler ignores the dot.

18 ARM Macros For example: $label.1 $label.loop $label.$count Default values can be set for parameters by following them with an equals sign and the default value. If the default has a leading or trailing space, the whole value should appear in quotes, as shown in the following code example....{$parameter="default value"}

19 ARM Macros The MEND directive signifies the end of the macro definition. If the macro contains WHILE/WEND loops, or contains conditional assembly, the WHILE/WEND loop must close before execution reaches the MEND directive. You can also terminate macro expansion with the MEXIT directive, used in conjunction with WHILE/WEND or conditional assembly.

20 ARM Macros multiply-accumulate (addMul) for ARM MACRO $Label_1: addMul $v1, $v2, $v3 $Label_1 add $v1, $v2, $v3 @ add two terms add $v1, $v1, #6 @ add 6 to the sum lsl $v1, $v1, #3 @ multyply by 8 In source code example: addMul r0, r1, r1 … The assembler makes the necessary substitutions add r0, r1, r2 add r0, r0, #6 lsl r0, r0, #3 …

Download ppt "Macro simple idea of textual substitution useful when you need a group of instructions or directives frequently."

Similar presentations

Ads by Google